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
The first thing to notice about this file is the filename, we are creating a `.gjs` file for a rendering test because we will be using `<template></template>` to render our Component under test and this only works inside `.gjs` files.
59
+
58
60
Inside your `module` and after setting up the test, we can now start to create our first test case.
59
61
Here, we can use the QUnit's `test` function, and we can give it a descriptive name:
assert.equal(this.element.querySelector('div').getAttribute('style'), 'color: red', 'starts as red');
96
+
assert.strictEqual(this.element.querySelector('div').getAttribute('style'), 'color: red', 'starts as red');
96
97
});
97
98
});
98
99
```
@@ -104,56 +105,97 @@ Also notice, the keyword `await` in front of the call to `render`.
104
105
It allows the test which we marked as `async` earlier to wait for any asynchronous behavior to complete before executing the rest of the code below.
105
106
In this case our first assertion will correctly execute after the component has fully rendered.
106
107
107
-
Next we can test that changing the component's `name` property updates the
108
-
component's `style` attribute and is reflected in the rendered HTML:
108
+
Next we can test to see if changing the component's `name` property updates the
109
+
component's `style` attribute and is reflected in the rendered HTML. Note: we expect this to fail so continue reading after this example if you want to find out why this fails:
assert.equal(this.element.querySelector('div').getAttribute('style'), 'color: red', 'starts as red');
125
+
assert.strictEqual(this.element.querySelector('div').getAttribute('style'), 'color: red', 'starts as red');
127
126
128
-
this.set('colorValue', 'blue');
127
+
colorValue = 'blue';
129
128
130
-
assert.equal(this.element.querySelector('div').getAttribute('style'), 'color: blue', 'updates to blue');
129
+
assert.strictEqual(this.element.querySelector('div').getAttribute('style'), 'color: blue', 'updates to blue');
131
130
});
132
131
});
133
132
```
134
133
135
-
We might also test this component to ensure that the content of its template is being rendered properly:
134
+
This test is now failing with the following error:
135
+
136
+
```
137
+
Expected: "color: blue"
138
+
Result: "color: red"
139
+
```
140
+
141
+
This means that the `name` attribute never updated the template after we update the value in `colorValue`. This happens because we need to mark data as `@tracked` before we can expect it to update templates automatically. You can read more about the tracking system on the [Autotracking In-Depth](../in-depth-topics/autotracking-in-depth/) topic.
142
+
143
+
Also it's worth noting that currently we can only use `@tracked` in the context of a class field, so we need to create an inline class with the data in the test:
assert.strictEqual(this.element.querySelector('div').getAttribute('style'), 'color: red', 'starts as red');
163
+
164
+
data.colorValue = 'blue';
165
+
await rerender();
166
+
167
+
assert.strictEqual(this.element.querySelector('div').getAttribute('style'), 'color: blue', 'updates to blue');
168
+
});
169
+
});
170
+
```
171
+
172
+
We also needed to add a call to `await rerender()` for this to work. This function returns a promise that will resolve when all the template updates have finished excecuting. We can await this promise to wait until all templates have updated before continuing to assert against the DOM.
173
+
174
+
Now that we have data updating correctly in a test, we can start testing other things about this component e.g. we can also test this that the content of its template is being rendered properly:
<!-- eof - needed for pages that end in a code block -->
610
+
Notice that we don't need to call `await rerender()`in this test to make sure the template has updated. This is because the work done in `await rerender()` is fully encapsulated in the `await settled()` so we don't need to call both.
0 commit comments