2022-01-06 21:01:55 +00:00
|
|
|
/* eslint-disable ember-a11y-testing/a11y-audit-called */
|
2021-12-14 17:56:03 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { render, click, waitFor } from '@ember/test-helpers';
|
|
|
|
import { hbs } from 'ember-cli-htmlbars';
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
module('Integration | Component | trigger', function (hooks) {
|
2021-12-14 17:56:03 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
module('Synchronous Interactions', function () {
|
|
|
|
test('it can trigger a synchronous action', async function (assert) {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set('name', 'Tomster');
|
|
|
|
this.set('changeName', () => this.set('name', 'Zoey'));
|
|
|
|
await render(hbs`
|
|
|
|
<Trigger @do={{this.changeName}} as |trigger|>
|
|
|
|
<h2 data-test-name>{{this.name}}</h2>
|
|
|
|
<button data-test-button type="button" {{on "click" trigger.fns.do}}>Change my name.</button>
|
|
|
|
</Trigger>
|
|
|
|
`);
|
2022-01-20 15:39:02 +00:00
|
|
|
assert
|
|
|
|
.dom('[data-test-name]')
|
|
|
|
.hasText('Tomster', 'Initial state renders correctly.');
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-name]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.hasText(
|
|
|
|
'Zoey',
|
|
|
|
'The name property changes when the button is clicked'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
});
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
test('it sets the result of the action', async function (assert) {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set('tomster', () => 'Tomster');
|
|
|
|
await render(hbs`
|
|
|
|
<Trigger @do={{this.tomster}} as |trigger|>
|
|
|
|
{{#if trigger.data.result}}
|
|
|
|
<h2 data-test-name>{{trigger.data.result}}</h2>
|
|
|
|
{{/if}}
|
|
|
|
<button data-test-button {{on "click" trigger.fns.do}}>Generate</button>
|
|
|
|
</Trigger>
|
|
|
|
`);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-name]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'Initial state does not render because there is no result yet.'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-name]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.hasText(
|
|
|
|
'Tomster',
|
|
|
|
'The result state updates after the triggered action'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
module('Asynchronous Interactions', function () {
|
|
|
|
test('it can trigger an asynchronous action', async function (assert) {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set(
|
|
|
|
'onTrigger',
|
|
|
|
() =>
|
2022-01-20 15:39:02 +00:00
|
|
|
new Promise((resolve) => {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set('resolve', resolve);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<Trigger @do={{this.onTrigger}} as |trigger|>
|
|
|
|
{{#if trigger.data.isBusy}}
|
|
|
|
<div data-test-div-loading>...Loading</div>
|
|
|
|
{{/if}}
|
|
|
|
{{#if trigger.data.isSuccess}}
|
|
|
|
<div data-test-div>Success!</div>
|
|
|
|
{{/if}}
|
|
|
|
<button data-test-button {{on "click" trigger.fns.do}}>Click Me</button>
|
|
|
|
</Trigger>
|
|
|
|
`);
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'The div does not render until after the action dispatches successfully'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div-loading]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.exists(
|
|
|
|
'Loading state is displayed when the action hasnt resolved yet'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'Success message does not display until after promise resolves'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
this.resolve();
|
|
|
|
await waitFor('[data-test-div]');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div-loading]')
|
|
|
|
.doesNotExist(
|
|
|
|
'Loading state is no longer rendered after state changes from busy to success'
|
|
|
|
);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.exists(
|
|
|
|
'Action has dispatched successfully after the promise resolves'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'Aftering clicking the button, again, the state is reset'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
assert
|
|
|
|
.dom('[data-test-div-loading]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.exists(
|
|
|
|
'After clicking the button, again, we are back in the loading state'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
this.resolve();
|
|
|
|
await waitFor('[data-test-div]');
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
|
|
|
.exists(
|
|
|
|
'An new action and new promise resolve after clicking the button for the second time'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
test('it handles the success state', async function (assert) {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set(
|
|
|
|
'onTrigger',
|
|
|
|
() =>
|
2022-01-20 15:39:02 +00:00
|
|
|
new Promise((resolve) => {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set('resolve', resolve);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
this.set('onSuccess', () => assert.step('On success happened'));
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<Trigger @do={{this.onTrigger}} @onSuccess={{this.onSuccess}} as |trigger|>
|
|
|
|
{{#if trigger.data.isSuccess}}
|
|
|
|
<span data-test-div>Success!</span>
|
|
|
|
{{/if}}
|
|
|
|
<button data-test-button {{on "click" trigger.fns.do}}>Click Me</button>
|
|
|
|
</Trigger>
|
|
|
|
`);
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'No text should appear until after the onSuccess callback is fired'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
await click('[data-test-button]');
|
|
|
|
this.resolve();
|
|
|
|
await waitFor('[data-test-div]');
|
|
|
|
assert.verifySteps(['On success happened']);
|
|
|
|
});
|
|
|
|
|
2022-01-20 15:39:02 +00:00
|
|
|
test('it handles the error state', async function (assert) {
|
2021-12-14 17:56:03 +00:00
|
|
|
this.set(
|
|
|
|
'onTrigger',
|
|
|
|
() =>
|
|
|
|
new Promise((_, reject) => {
|
|
|
|
this.set('reject', reject);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
this.set('onError', () => {
|
|
|
|
assert.step('On error happened');
|
|
|
|
});
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<Trigger @do={{this.onTrigger}} @onError={{this.onError}} as |trigger|>
|
|
|
|
{{#if trigger.data.isBusy}}
|
|
|
|
<div data-test-div-loading>...Loading</div>
|
|
|
|
{{/if}}
|
|
|
|
{{#if trigger.data.isError}}
|
|
|
|
<span data-test-span>Error!</span>
|
|
|
|
{{/if}}
|
|
|
|
<button data-test-button {{on "click" trigger.fns.do}}>Click Me</button>
|
|
|
|
</Trigger>
|
|
|
|
`);
|
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div-loading]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.exists(
|
|
|
|
'Loading state is displayed when the action hasnt resolved yet'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.doesNotExist(
|
|
|
|
'No text should appear until after the onError callback is fired'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
this.reject();
|
|
|
|
await waitFor('[data-test-span]');
|
|
|
|
assert.verifySteps(['On error happened']);
|
|
|
|
|
|
|
|
await click('[data-test-button]');
|
|
|
|
|
|
|
|
assert
|
|
|
|
.dom('[data-test-div-loading]')
|
2022-01-20 15:39:02 +00:00
|
|
|
.exists(
|
|
|
|
'The previous error state was cleared and we show loading, again.'
|
|
|
|
);
|
2021-12-14 17:56:03 +00:00
|
|
|
|
|
|
|
assert.dom('[data-test-div]').doesNotExist('The error state is cleared');
|
|
|
|
|
|
|
|
this.reject();
|
|
|
|
await waitFor('[data-test-span]');
|
|
|
|
assert.verifySteps(['On error happened'], 'The error dispatches');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|