Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests for the refresh method
Browse files Browse the repository at this point in the history
aklkv committed Mar 13, 2023
1 parent e5d38a6 commit 6e79a6a
Showing 9 changed files with 141 additions and 5 deletions.
Binary file removed .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ node_modules/
# misc
npm-debug.log*
yarn-error.log
.DS_Store
/.DS_Store

# yarn
.pnp.*
@@ -17,4 +17,4 @@ yarn-error.log
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
!.yarn/versions
11 changes: 11 additions & 0 deletions test-app/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Route from '@ember/routing/route';

let count = 0;

export default class extends Route {
model() {
return {
count: count++,
};
}
}
3 changes: 3 additions & 0 deletions test-app/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<h1>Ember Engines Demo</h1>
<span class='global-refresh-counter'>
{{@model.count}}
</span>

<LinkTo @route="routeless-engine-demo" class="routeless-engine">Routeless Engine</LinkTo> |
<LinkTo @route="routable-engine-demo" class="routeable-engine">Routeable Engine</LinkTo> |
31 changes: 29 additions & 2 deletions test-app/lib/ember-blog/addon/components/hello-name.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
<div class="hello-name" ...attributes>
Hello, {{@name}}!
<div class='hello-name' ...attributes>
<span class='greeting'>
Hello,
{{@name}}!
</span>

<button
class='refresh'
type='button'
{{on 'click' this.refresh}}
>
Refresh
</button>

<button
class='refresh-route'
type='button'
{{on 'click' this.refreshRoute}}
>
Refresh Route
</button>

<button
class='refresh-external'
type='button'
{{on 'click' this.refreshExternal}}
>
Refresh External
</button>
</div>
26 changes: 25 additions & 1 deletion test-app/lib/ember-blog/addon/components/hello-name.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { later, cancel } from '@ember/runloop';
import Component from '@glimmer/component';
import { later, cancel } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class extends Component {
@service router;

@tracked name = this.args.name;
@tracked clickCount = 0;

constructor() {
super(...arguments);
@@ -20,4 +25,23 @@ export default class extends Component {
cancel(this._later);
super.willDestroy(...arguments);
}

@action click() {
this.clickCount++;
}

@action
refresh() {
this.router.refresh();
}

@action
refreshRoute() {
this.router.refresh('new');
}

@action
refreshExternal() {
this.router.refreshExternal('home');
}
}
19 changes: 19 additions & 0 deletions test-app/lib/ember-blog/addon/routes/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';

let count = 0;

export default class extends Route {
@service exampleService;

model() {
// cause a service to be instantiated, so that our tests can
// confirm that it gets cleaned up
this.exampleService;

return {
name: 'Derek Zoolander',
count: count++,
};
}
}
4 changes: 4 additions & 0 deletions test-app/lib/ember-blog/addon/templates/new.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="engine">
New Route!!

<span class="route-refresh-counter">
{{@model.count}}
</span>

<HelloWorld class="routable-hello-world" />

<button class="trigger-transition-to" type="button" {{on "click" this.goAway}}>Go Away!</button>
48 changes: 48 additions & 0 deletions test-app/tests/acceptance/routeable-engine-demo-refresh-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, find, click } from '@ember/test-helpers';

module('Acceptance | routeless engine demo', function (hooks) {
setupApplicationTest(hooks);

module('Engine Router Service Refresh Method', function () {
test('refresh without params triggers refresh with current route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.route-refresh-counter').hasText(counter);
await click('.refresh');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.route-refresh-counter').hasText(counter);
});

test('refresh with params triggers refresh on provided route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.route-refresh-counter').hasText(counter);
await click('.refresh-route');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.route-refresh-counter').hasText(counter);
});

test('refresh external route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.global-refresh-counter').hasText(counter);
await click('.refresh-external');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.global-refresh-counter').hasText(counter);
});
});
});

0 comments on commit 6e79a6a

Please sign in to comment.