-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathreplaceWith_test.js
181 lines (153 loc) · 4.72 KB
/
replaceWith_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import NoneLocation from '@ember/routing/none-location';
import { RouterTestCase, moduleFor, runLoopSettled } from 'internal-test-helpers';
import { InternalTransition as Transition } from 'router_js';
import Controller from '@ember/controller';
import Route from '@ember/routing/route';
moduleFor(
'Router Service - replaceWith',
class extends RouterTestCase {
constructor() {
super(...arguments);
let testCase = this;
testCase.state = [];
this.add(
'location:test',
NoneLocation.extend({
setURL(path) {
testCase.state.push(path);
this.set('path', path);
},
replaceURL(path) {
testCase.state.splice(testCase.state.length - 1, 1, path);
this.set('path', path);
},
})
);
}
get routerOptions() {
return {
location: 'test',
};
}
['@test RouterService#replaceWith returns a Transition'](assert) {
assert.expect(1);
let transition;
return this.visit('/').then(() => {
transition = this.routerService.replaceWith('parent.child');
assert.ok(transition instanceof Transition);
return transition;
});
}
['@test RouterService#replaceWith with basic route replaces location'](assert) {
assert.expect(1);
return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child');
})
.then(() => {
return this.routerService.transitionTo('parent.sister');
})
.then(() => {
return this.routerService.replaceWith('parent.brother');
})
.then(() => {
assert.deepEqual(this.state, ['/', '/child', '/brother']);
});
}
['@test RouterService#replaceWith with basic route using URLs replaces location'](assert) {
assert.expect(1);
return this.visit('/')
.then(() => {
return this.routerService.transitionTo('/child');
})
.then(() => {
return this.routerService.transitionTo('/sister');
})
.then(() => {
return this.routerService.replaceWith('/brother');
})
.then(() => {
assert.deepEqual(this.state, ['/', '/child', '/brother']);
});
}
['@test RouterService#replaceWith transitioning back to previously visited route replaces location'](
assert
) {
assert.expect(1);
return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child');
})
.then(() => {
return this.routerService.transitionTo('parent.sister');
})
.then(() => {
return this.routerService.transitionTo('parent.brother');
})
.then(() => {
return this.routerService.replaceWith('parent.sister');
})
.then(() => {
assert.deepEqual(this.state, ['/', '/child', '/sister', '/sister']);
});
}
['@test RouterService#replaceWith with basic query params removes query param defaults'](
assert
) {
assert.expect(1);
this.add(
'controller:parent.child',
Controller.extend({
queryParams: ['sort'],
sort: 'ASC',
})
);
let queryParams = this.buildQueryParams({ sort: 'ASC' });
return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.brother');
})
.then(() => {
return this.routerService.replaceWith('parent.sister');
})
.then(() => {
return this.routerService.replaceWith('parent.child', queryParams);
})
.then(() => {
assert.deepEqual(this.state, ['/', '/child']);
});
}
async ['@test RouterService#replaceWith with `refreshModel: true` query param does not always refresh'](
assert
) {
assert.expect(3);
let parentBeforeModelCount = 0;
this.add(
'route:parent',
Route.extend({
beforeModel() {
parentBeforeModelCount++;
},
queryParams: {
foo: {
refreshModel: true,
},
},
})
);
this.add(
'controller:parent',
Controller.extend({
queryParams: ['foo'],
foo: 'default',
})
);
await this.visit('/child');
assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel called once');
this.routerService.replaceWith('parent.sister');
await runLoopSettled();
assert.equal(this.routerService.get('currentURL'), '/sister', 'transitioned');
assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel still called only once');
}
}
);