Skip to content

Commit b9c3d14

Browse files
committed
integration test notes
1 parent 33d9372 commit b9c3d14

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

notes/09-integration-tests.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,130 @@ For our learning purposes, we'll just worry about two scenarios
88

99
- When the component is initially rendered, no user is selected, and the `input[type="submit"]` is disabled
1010
- Once a user is chosen, the `input[type="submit"]` is enabled, and the `<select>` should have a value of the appropriate user's id
11+
12+
Open up the integration test for `<LoginForm />` - [`tests/integration/components/login-form-test.js`](../tests/integration/components/login-form-test.js). It should look like this
13+
14+
```ts
15+
import { module, test } from 'qunit';
16+
import { setupRenderingTest } from 'ember-qunit';
17+
import { render } from '@ember/test-helpers';
18+
import hbs from 'htmlbars-inline-precompile';
19+
20+
module('Integration | Component | login-form', function(hooks) {
21+
setupRenderingTest(hooks);
22+
23+
test('it renders', async function(assert) {
24+
// Set any properties with this.set('myProperty', 'value');
25+
// Handle any actions with this.set('myAction', function(val) { ... });
26+
27+
await render(hbs`<LoginForm />`);
28+
29+
assert.deepEqual(
30+
this.element.textContent
31+
.trim()
32+
.replace(/\s*\n+\s*/g, '\n')
33+
.split('\n'),
34+
['Login', 'Select a user', 'Testy Testerson', 'Sample McData']
35+
);
36+
});
37+
});
38+
```
39+
40+
The `'Integration | Component | login-form'` is the test module name, `'it renders'` is the test name, and `assert.*` are assertions. Think of these as folders, subfolders and files.
41+
42+
```
43+
test module
44+
test
45+
assertion
46+
test
47+
assertion
48+
test
49+
assertion
50+
assertion
51+
assertion
52+
```
53+
54+
If you go to http://localhost:4200/tests?filter=login-form&nolint, you can see this information in the test runner UI
55+
56+
![test-runner](./img/09-integration-tests/test-runner.png)
57+
58+
Let's change the title of the existing test to something more descriptive
59+
60+
```
61+
'initially has no user selected, and "Sign In" button disabled'
62+
```
63+
64+
We'll need to get ahold of two DOM elements to assert against
65+
66+
1. the "Sign In" button
67+
2. the `<select>`
68+
69+
```js
70+
let button = find('input[type="submit"]');
71+
let select = find('select');
72+
```
73+
74+
If you wanted to add some special autocomplete awesomeness, you could give your editor a clue (via [JSDoc comments](http://usejsdoc.org/tags-type.html)) of the types we expect `find` to return in these cases
75+
76+
```js
77+
let button = /** @type {HTMLInputElement} */ (find('input[type="submit"]'));
78+
let select = /** @type {HTMLSelectElement} */ (find('select'));
79+
```
80+
81+
Now we can write two assertions against these
82+
83+
```js
84+
assert.equal(select.value, '', 'Initially, no user is selected');
85+
assert.equal(button.disabled, true, 'Initially the button is disabled');
86+
```
87+
88+
Go back to http://localhost:4200/tests?filter=login-form&nolint and you should see the nice assertion labels showing up
89+
90+
![assertion-labels](./img/09-integration-tests/assertion-labels.png)
91+
92+
Create a new test immediately below the first one, and name it something like
93+
94+
```ts
95+
test('after selecting a user "Sign In" button enabled', async function(assert) {
96+
// ...
97+
});
98+
```
99+
100+
define the test body as follows
101+
102+
```ts
103+
// Render the component
104+
await render(hbs`<LoginForm />`);
105+
106+
// Pluck off the DOM elements we care about
107+
let button = /** @type {HTMLInputElement} */ (find('input[type="submit"]'));
108+
let select = /** @type {HTMLSelectElement} */ (find('select'));
109+
110+
// Select the <option> with value="1"
111+
await fillIn('select', '1');
112+
113+
assert.equal(select.value, '1', '<option value="1"> is currently selected');
114+
115+
assert.equal(button.disabled, false, 'The submit button is no longer disabled');
116+
117+
assert.deepEqual(
118+
this.element.textContent
119+
.trim()
120+
.replace(/\s*\n+\s*/g, '\n')
121+
.split('\n'),
122+
[
123+
'Login',
124+
'Select a user',
125+
'Testy Testerson',
126+
'Sample McData',
127+
'Logging in with userId 1',
128+
],
129+
'validation text now shows up'
130+
);
131+
```
132+
133+
Going back to http://localhost:4200/tests?filter=login-form&nolint, you should see the new test and all of our nice assertion labels in the test runner UI
134+
135+
![second-test](./img/09-integration-tests/another-test.png)
136+
137+
Congrats! we've just written a nice component integration test!
Loading
Loading
472 KB
Loading

0 commit comments

Comments
 (0)