Skip to content

Commit 623ce49

Browse files
authored
feat: add refresh method (#84)
* feat: add refresh method * fix: yarn * fix: typo * fix: add missing component * fix: link to post page * feat: add tests for the refresh method * fix: remove single quotes, add newer prettier config * fix: remove useless assertions * fix: clean up tests
1 parent afc7d40 commit 623ce49

File tree

20 files changed

+18029
-13457
lines changed

20 files changed

+18029
-13457
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ node_modules/
88
# misc
99
npm-debug.log*
1010
yarn-error.log
11-
.DS_Store
11+
/.DS_Store
12+
13+
# yarn
14+
.pnp.*
15+
.yarn/*
16+
!.yarn/patches
17+
!.yarn/plugins
18+
!.yarn/releases
19+
!.yarn/sdks
20+
!.yarn/versions

.yarnrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

ember-engines-router-service/src/services/engine-router-service.js

+18
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ export default class EngineRouterService extends Service.extend(Evented) {
6565
return this._externalRoutes[externalRouteName];
6666
}
6767

68+
refresh(routeName = this.currentRouteName, ...args) {
69+
if (resemblesURL(routeName)) {
70+
return this.externalRouter.refresh(routeName);
71+
}
72+
73+
return this.externalRouter.refresh(
74+
namespaceEngineRouteName(this._mountPoint, routeName),
75+
...args
76+
);
77+
}
78+
79+
refreshExternal(routeName, ...args) {
80+
return this.externalRouter.refresh(
81+
this.getExternalRouteName(routeName),
82+
...args
83+
);
84+
}
85+
6886
transitionTo(routeName, ...args) {
6987
if (resemblesURL(routeName)) {
7088
return this.externalRouter.transitionTo(routeName);
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type RouterService from '@ember/routing/router-service';
22

3-
export default interface EnginesRouterService extends Omit<
4-
RouterService,
5-
"currentRoute" | "recognize" | "recognizeAndLoad"
6-
> {
3+
export default interface EnginesRouterService
4+
extends Omit<
5+
RouterService,
6+
'currentRoute' | 'recognize' | 'recognizeAndLoad'
7+
> {
78
isActiveExternal: RouterService['isActive'];
89
replaceWithExternal: RouterService['replaceWith'];
910
transitionToExternal: RouterService['transitionTo'];
11+
refreshExternal: RouterService['refresh'];
1012
urlForExternal: RouterService['urlFor'];
1113
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"release": "release-it"
1515
},
1616
"devDependencies": {
17-
"release-it": "^15.5.1",
1817
"@release-it-plugins/lerna-changelog": "^5.0.0",
19-
"@release-it-plugins/workspaces": "^3.2.0"
18+
"@release-it-plugins/workspaces": "^3.2.0",
19+
"release-it": "^15.5.1"
2020
},
2121
"publishConfig": {
2222
"registry": "https://registry.npmjs.org"

test-app/.prettierrc.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
22

33
module.exports = {
4-
singleQuote: true,
4+
overrides: [
5+
{
6+
files: '*.{js,ts}',
7+
options: {
8+
singleQuote: true,
9+
},
10+
},
11+
],
512
};

test-app/app/routes/application.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Route from '@ember/routing/route';
2+
3+
let count = 0;
4+
5+
export default class extends Route {
6+
model() {
7+
return {
8+
count: count++,
9+
};
10+
}
11+
}
+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<h1>Ember Engines Demo</h1>
2+
<span class="global-refresh-counter">
3+
{{@model.count}}
4+
</span>
25

3-
<LinkTo @route="routeless-engine-demo" class="routeless-engine">Routeless Engine</LinkTo> |
4-
<LinkTo @route="routable-engine-demo" class="routeable-engine">Routeable Engine</LinkTo> |
6+
<LinkTo @route="routeless-engine-demo" class="routeless-engine">Routeless Engine</LinkTo>
7+
|
8+
<LinkTo @route="routable-engine-demo" class="routeable-engine">Routeable Engine</LinkTo>
9+
|
510
<LinkTo @route="post" @model="1" class="non-blog-post">Non-Blog Post</LinkTo>
611

712
{{outlet}}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h3 class="title">Eager engine</h3>
22

33
<LinkTo @route="index" @current-when="index post" class="current-when-test-link">All Posts</LinkTo> |
4-
<LinkTo @route="post">Post 1</LinkTo>
4+
<LinkTo @route="post" @model={{1}}>Post 1</LinkTo>
55

66
{{outlet}}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1-
<div class="hello-name" ...attributes>
2-
Hello, {{@name}}!
1+
<div class='hello-name' ...attributes>
2+
<span class='greeting'>
3+
Hello,
4+
{{@name}}!
5+
</span>
6+
7+
<button
8+
class='refresh'
9+
type='button'
10+
{{on 'click' this.refresh}}
11+
>
12+
Refresh
13+
</button>
14+
15+
<button
16+
class='refresh-route'
17+
type='button'
18+
{{on 'click' this.refreshRoute}}
19+
>
20+
Refresh Route
21+
</button>
22+
23+
<button
24+
class='refresh-external'
25+
type='button'
26+
{{on 'click' this.refreshExternal}}
27+
>
28+
Refresh External
29+
</button>
330
</div>

test-app/lib/ember-blog/addon/components/hello-name.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { later, cancel } from '@ember/runloop';
21
import Component from '@glimmer/component';
2+
import { later, cancel } from '@ember/runloop';
33
import { tracked } from '@glimmer/tracking';
4+
import { action } from '@ember/object';
5+
import { inject as service } from '@ember/service';
46

57
export default class extends Component {
8+
@service router;
9+
610
@tracked name = this.args.name;
11+
@tracked clickCount = 0;
712

813
constructor() {
914
super(...arguments);
@@ -20,4 +25,23 @@ export default class extends Component {
2025
cancel(this._later);
2126
super.willDestroy(...arguments);
2227
}
28+
29+
@action click() {
30+
this.clickCount++;
31+
}
32+
33+
@action
34+
refresh() {
35+
this.router.refresh();
36+
}
37+
38+
@action
39+
refreshRoute() {
40+
this.router.refresh('new');
41+
}
42+
43+
@action
44+
refreshExternal() {
45+
this.router.refreshExternal('home');
46+
}
2347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { inject as service } from '@ember/service';
2+
import Route from '@ember/routing/route';
3+
4+
let count = 0;
5+
6+
export default class extends Route {
7+
@service exampleService;
8+
9+
model() {
10+
// cause a service to be instantiated, so that our tests can
11+
// confirm that it gets cleaned up
12+
this.exampleService;
13+
14+
return {
15+
name: 'Derek Zoolander',
16+
count: count++,
17+
};
18+
}
19+
}

test-app/lib/ember-blog/addon/templates/new.hbs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<div class="engine">
22
New Route!!
33

4+
<span class="route-refresh-counter">
5+
{{@model.count}}
6+
</span>
7+
48
<HelloWorld class="routable-hello-world" />
59

610
<button class="trigger-transition-to" type="button" {{on "click" this.goAway}}>Go Away!</button>

test-app/lib/ember-blog/addon/templates/post.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</button>
3535

3636
<button class="routable-replace-external-button {{if this.replaceWithExternal "replaced-with-external"}}" type="button" {{on "click" this.replaceWithHomeByService}}>
37-
Go home progammatically (Engine Router Service)
37+
Go home programmatically (Engine Router Service)
3838
</button>
3939

4040

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span class="greeting">Hola</span> from {{@name}}!
2+
<button class="clicker" type="button" {{on "click" this.click}}>Clicker</button>
3+
<span class="click-count">{{this.clickCount}}</span>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Component from '@glimmer/component';
2+
import { tracked } from '@glimmer/tracking';
3+
import { action } from '@ember/object';
4+
5+
export default class extends Component {
6+
@tracked clickCount = 0;
7+
8+
@action click() {
9+
this.clickCount++;
10+
}
11+
}

test-app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"devDependencies": {
3434
"@babel/core": "^7.20.7",
3535
"@babel/eslint-parser": "^7.19.1",
36+
"@ember/legacy-built-in-components": "0.4.1",
3637
"@ember/optional-features": "^2.0.0",
3738
"@ember/test-helpers": "^2.9.3",
3839
"@embroider/test-setup": "^2.0.2",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { module, test } from 'qunit';
2+
import { setupApplicationTest } from 'ember-qunit';
3+
import { visit, find, click } from '@ember/test-helpers';
4+
5+
module('Acceptance | Engine Router Service | Refresh Method', function (hooks) {
6+
setupApplicationTest(hooks);
7+
8+
test('refresh without params triggers refresh with current route', async function (assert) {
9+
await visit('/routable-engine-demo/ember-blog/new');
10+
11+
let counter = await find('.route-refresh-counter').textContent;
12+
await click('.refresh');
13+
14+
counter = parseInt(counter, 10);
15+
counter = ++counter;
16+
counter = counter.toString();
17+
assert.dom('.route-refresh-counter').hasText(counter);
18+
});
19+
20+
test('refresh with params triggers refresh on provided route', async function (assert) {
21+
await visit('/routable-engine-demo/ember-blog/new');
22+
23+
let counter = await find('.route-refresh-counter').textContent;
24+
await click('.refresh-route');
25+
26+
counter = parseInt(counter, 10);
27+
counter = ++counter;
28+
counter = counter.toString();
29+
assert.dom('.route-refresh-counter').hasText(counter);
30+
});
31+
32+
test('refresh external route', async function (assert) {
33+
await visit('/routable-engine-demo/ember-blog/new');
34+
35+
let counter = await find('.route-refresh-counter').textContent;
36+
await click('.refresh-external');
37+
38+
counter = parseInt(counter, 10);
39+
counter = ++counter;
40+
counter = counter.toString();
41+
assert.dom('.global-refresh-counter').hasText(counter);
42+
});
43+
});

0 commit comments

Comments
 (0)