You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/09-integration-tests.md
+127Lines changed: 127 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -8,3 +8,130 @@ For our learning purposes, we'll just worry about two scenarios
8
8
9
9
- When the component is initially rendered, no user is selected, and the `input[type="submit"]` is disabled
10
10
- 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
// Set any properties with this.set('myProperty', 'value');
25
+
// Handle any actions with this.set('myAction', function(val) { ... });
26
+
27
+
awaitrender(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
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
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', asyncfunction(assert) {
96
+
// ...
97
+
});
98
+
```
99
+
100
+
define the test body as follows
101
+
102
+
```ts
103
+
// Render the component
104
+
awaitrender(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
+
awaitfillIn('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
0 commit comments