Skip to content

Commit db02bfb

Browse files
authoredNov 24, 2021
Merge pull request #10 from Windvis/disable-polyfill-with-embroider-macros
Disable the polyfill when the method is available in ember-source
2 parents b7e9a54 + df796af commit db02bfb

7 files changed

+193
-138
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Polyfills the RouterService#refresh method as described in [RFC 631](https://emb
77
Compatibility
88
------------------------------------------------------------------------------
99

10-
* Ember.js v3.4 or above
10+
* Ember.js v3.4 or above. The polyfill is disabled in Ember 4.1 and above.
1111
* Ember CLI v3.4 or above
1212
* Embroider safe and optimized
1313
* Node.js v12 or above

‎addon/initializers/setup-router-service-refresh-polyfill.js

+45-37
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
11
/* eslint-disable ember/no-private-routing-service */
22
import { assert } from '@ember/debug';
33
import { getOwner } from '@ember/application';
4+
import { dependencySatisfies, macroCondition } from '@embroider/macros';
5+
6+
export const SHOULD_POLYFILL_ROUTER_SERVICE_REFRESH = !dependencySatisfies(
7+
'ember-source',
8+
'^4.1.0-beta.1' // https://github.com/emberjs/ember.js/blob/master/CHANGELOG.md#v410-beta1-november-19-2021
9+
);
410

511
export function initialize(application) {
6-
const RouterService = application.resolveRegistration('service:router');
7-
8-
RouterService.reopen({
9-
/**
10-
* Refreshes all currently active routes, doing a full transition.
11-
* If a routeName is provided and refers to a currently active route,
12-
* it will refresh only that route and its descendents.
13-
* Returns a promise that will be resolved once the refresh is complete.
14-
* All resetController, beforeModel, model, afterModel, redirect, and setupController
15-
* hooks will be called again. You will get new data from the model hook.
16-
*
17-
* @method refresh
18-
* @param {String} [routeName] the route to refresh (along with all child routes)
19-
* @return Transition
20-
* @public
21-
*/
22-
refresh(routeName) {
23-
if (!routeName) {
24-
return this._router._routerMicrolib.refresh();
25-
}
26-
27-
assert(
28-
`The route "${routeName}" was not found`,
29-
this._router.hasRoute(routeName)
30-
);
31-
32-
assert(
33-
`The route "${routeName}" is currently not active`,
34-
this.isActive(routeName)
35-
);
36-
37-
let owner = getOwner(this);
38-
let pivotRoute = owner.lookup(`route:${routeName}`);
39-
40-
return this._router._routerMicrolib.refresh(pivotRoute);
41-
},
42-
});
12+
if (macroCondition(SHOULD_POLYFILL_ROUTER_SERVICE_REFRESH)) {
13+
const RouterService = application.resolveRegistration('service:router');
14+
15+
RouterService.reopen({
16+
/**
17+
* Refreshes all currently active routes, doing a full transition.
18+
* If a routeName is provided and refers to a currently active route,
19+
* it will refresh only that route and its descendents.
20+
* Returns a promise that will be resolved once the refresh is complete.
21+
* All resetController, beforeModel, model, afterModel, redirect, and setupController
22+
* hooks will be called again. You will get new data from the model hook.
23+
*
24+
* @method refresh
25+
* @param {String} [routeName] the route to refresh (along with all child routes)
26+
* @return Transition
27+
* @public
28+
*/
29+
refresh(routeName) {
30+
if (!routeName) {
31+
return this._router._routerMicrolib.refresh();
32+
}
33+
34+
assert(
35+
`The route "${routeName}" was not found`,
36+
this._router.hasRoute(routeName)
37+
);
38+
39+
assert(
40+
`The route "${routeName}" is currently not active`,
41+
this.isActive(routeName)
42+
);
43+
44+
let owner = getOwner(this);
45+
let pivotRoute = owner.lookup(`route:${routeName}`);
46+
47+
return this._router._routerMicrolib.refresh(pivotRoute);
48+
},
49+
});
50+
}
4351
}
4452

4553
export default {

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"test:ember-compatibility": "ember try:each"
3030
},
3131
"dependencies": {
32+
"@embroider/macros": "^0.47.2",
3233
"ember-cli-babel": "^7.26.6",
3334
"ember-cli-htmlbars": "^6.0.0"
3435
},

‎tests/acceptance/router-service-refresh-test.js

+100-90
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,108 @@
33
import Route from '@ember/routing/route';
44
import { visit } from '@ember/test-helpers';
55
import { setupApplicationTest } from 'ember-qunit';
6-
import { module, test } from 'qunit';
6+
import { module } from 'qunit';
7+
import { testIfPolyfilled } from '../helpers/should-test-polyfill';
78

89
module('Acceptance | RouterService | refresh', function (hooks) {
910
setupApplicationTest(hooks);
1011

11-
test('it can be used to re-run the model hooks of active routes', async function (assert) {
12-
const routerService = this.owner.lookup('service:router');
13-
14-
let parentCounter = 0;
15-
this.owner.register(
16-
'route:parent',
17-
Route.extend({
18-
model() {
19-
++parentCounter;
20-
},
21-
})
22-
);
23-
24-
let childCounter = 0;
25-
this.owner.register(
26-
'route:parent.child',
27-
Route.extend({
28-
model() {
29-
++childCounter;
30-
},
31-
})
32-
);
33-
34-
let sisterCounter = 0;
35-
this.owner.register(
36-
'route:parent.sister',
37-
Route.extend({
38-
model() {
39-
++sisterCounter;
40-
},
41-
})
42-
);
43-
44-
await visit('/');
45-
assert.strictEqual(parentCounter, 1);
46-
assert.strictEqual(childCounter, 0);
47-
assert.strictEqual(sisterCounter, 0);
48-
49-
await routerService.refresh();
50-
assert.strictEqual(parentCounter, 2);
51-
assert.strictEqual(childCounter, 0);
52-
assert.strictEqual(sisterCounter, 0);
53-
54-
await routerService.refresh('application');
55-
assert.strictEqual(parentCounter, 3);
56-
assert.strictEqual(childCounter, 0);
57-
assert.strictEqual(sisterCounter, 0);
58-
59-
await routerService.transitionTo('parent.child');
60-
assert.strictEqual(parentCounter, 3);
61-
assert.strictEqual(childCounter, 1);
62-
assert.strictEqual(sisterCounter, 0);
63-
64-
await routerService.refresh('parent.child');
65-
assert.strictEqual(parentCounter, 3);
66-
assert.strictEqual(childCounter, 2);
67-
assert.strictEqual(sisterCounter, 0);
68-
69-
await routerService.refresh('parent');
70-
assert.strictEqual(parentCounter, 4);
71-
assert.strictEqual(childCounter, 3);
72-
assert.strictEqual(sisterCounter, 0);
73-
74-
await routerService.transitionTo('parent.sister');
75-
assert.strictEqual(parentCounter, 4);
76-
assert.strictEqual(childCounter, 3);
77-
assert.strictEqual(sisterCounter, 1);
78-
79-
await routerService.refresh();
80-
assert.strictEqual(parentCounter, 5);
81-
assert.strictEqual(childCounter, 3);
82-
assert.strictEqual(sisterCounter, 2);
83-
});
84-
85-
test('it verifies that the provided route exists', async function (assert) {
86-
const routerService = this.owner.lookup('service:router');
87-
88-
assert.throws(() => {
89-
routerService.refresh('this-route-does-not-exist');
90-
});
91-
});
92-
93-
test('it verifies that the provided route is active', async function (assert) {
94-
const routerService = this.owner.lookup('service:router');
95-
96-
assert.throws(() => {
97-
routerService.refresh('this-route-does-not-exist');
98-
});
99-
});
12+
testIfPolyfilled(
13+
'it can be used to re-run the model hooks of active routes',
14+
async function (assert) {
15+
const routerService = this.owner.lookup('service:router');
16+
17+
let parentCounter = 0;
18+
this.owner.register(
19+
'route:parent',
20+
Route.extend({
21+
model() {
22+
++parentCounter;
23+
},
24+
})
25+
);
26+
27+
let childCounter = 0;
28+
this.owner.register(
29+
'route:parent.child',
30+
Route.extend({
31+
model() {
32+
++childCounter;
33+
},
34+
})
35+
);
36+
37+
let sisterCounter = 0;
38+
this.owner.register(
39+
'route:parent.sister',
40+
Route.extend({
41+
model() {
42+
++sisterCounter;
43+
},
44+
})
45+
);
46+
47+
await visit('/');
48+
assert.strictEqual(parentCounter, 1);
49+
assert.strictEqual(childCounter, 0);
50+
assert.strictEqual(sisterCounter, 0);
51+
52+
await routerService.refresh();
53+
assert.strictEqual(parentCounter, 2);
54+
assert.strictEqual(childCounter, 0);
55+
assert.strictEqual(sisterCounter, 0);
56+
57+
await routerService.refresh('application');
58+
assert.strictEqual(parentCounter, 3);
59+
assert.strictEqual(childCounter, 0);
60+
assert.strictEqual(sisterCounter, 0);
61+
62+
await routerService.transitionTo('parent.child');
63+
assert.strictEqual(parentCounter, 3);
64+
assert.strictEqual(childCounter, 1);
65+
assert.strictEqual(sisterCounter, 0);
66+
67+
await routerService.refresh('parent.child');
68+
assert.strictEqual(parentCounter, 3);
69+
assert.strictEqual(childCounter, 2);
70+
assert.strictEqual(sisterCounter, 0);
71+
72+
await routerService.refresh('parent');
73+
assert.strictEqual(parentCounter, 4);
74+
assert.strictEqual(childCounter, 3);
75+
assert.strictEqual(sisterCounter, 0);
76+
77+
await routerService.transitionTo('parent.sister');
78+
assert.strictEqual(parentCounter, 4);
79+
assert.strictEqual(childCounter, 3);
80+
assert.strictEqual(sisterCounter, 1);
81+
82+
await routerService.refresh();
83+
assert.strictEqual(parentCounter, 5);
84+
assert.strictEqual(childCounter, 3);
85+
assert.strictEqual(sisterCounter, 2);
86+
}
87+
);
88+
89+
testIfPolyfilled(
90+
'it verifies that the provided route exists',
91+
async function (assert) {
92+
const routerService = this.owner.lookup('service:router');
93+
94+
assert.throws(() => {
95+
routerService.refresh('this-route-does-not-exist');
96+
});
97+
}
98+
);
99+
100+
testIfPolyfilled(
101+
'it verifies that the provided route is active',
102+
async function (assert) {
103+
const routerService = this.owner.lookup('service:router');
104+
105+
assert.throws(() => {
106+
routerService.refresh('this-route-does-not-exist');
107+
});
108+
}
109+
);
100110
});

‎tests/helpers/should-test-polyfill.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test, skip } from 'qunit';
2+
import { SHOULD_POLYFILL_ROUTER_SERVICE_REFRESH } from 'ember-router-service-refresh-polyfill/initializers/setup-router-service-refresh-polyfill';
3+
4+
export const testIfPolyfilled = SHOULD_POLYFILL_ROUTER_SERVICE_REFRESH
5+
? test
6+
: skip;

‎tests/unit/initializers/setup-router-service-refresh-polyfill-test.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import Application from '@ember/application';
22

33
import config from 'dummy/config/environment';
44
import { initialize } from 'dummy/initializers/setup-router-service-refresh-polyfill';
5-
import { module, test } from 'qunit';
5+
import { module } from 'qunit';
66
import Resolver from 'ember-resolver';
77
import { run } from '@ember/runloop';
8+
import { testIfPolyfilled } from '../../helpers/should-test-polyfill';
89

910
module(
1011
'Unit | Initializer | setup-router-service-refresh-polyfill',
@@ -28,12 +29,15 @@ module(
2829
run(this.application, 'destroy');
2930
});
3031

31-
test('it adds the polyfilled refresh method to the RouterService', async function (assert) {
32-
await this.application.boot();
33-
const applicationInstance = this.application.buildInstance();
34-
const routerService = applicationInstance.lookup('service:router');
32+
testIfPolyfilled(
33+
'it adds the polyfilled refresh method to the RouterService',
34+
async function (assert) {
35+
await this.application.boot();
36+
const applicationInstance = this.application.buildInstance();
37+
const routerService = applicationInstance.lookup('service:router');
3538

36-
assert.strictEqual(typeof routerService.refresh, 'function');
37-
});
39+
assert.strictEqual(typeof routerService.refresh, 'function');
40+
}
41+
);
3842
}
3943
);

0 commit comments

Comments
 (0)
Please sign in to comment.