Skip to content

Commit de4fbf7

Browse files
Update tutorial to use assert.strictEqual in place of assert.equal to pass eslint-plugin-qunit (#197)
* Update tutorial to use `assert.strictEqual` in place of `assert.equal` to pass eslint-plugin-qunit Co-authored-by: Godfrey Chan <godfreykfc@gmail.com>
1 parent 39bb205 commit de4fbf7

File tree

6 files changed

+40
-42
lines changed

6 files changed

+40
-42
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
fail-fast: false
1919
matrix:
2020
channel:
21-
- release
21+
# - release TODO: Re-enable once 4.0 is released
2222
- beta
2323
steps:
2424
- name: Set up Git

src/markdown/tutorial/part-1/03-automated-testing.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ Let's open the generated test file and replace the boilerplate test with our own
6868
- test('visiting /super-rentals', async function (assert) {
6969
- await visit('/super-rentals');
7070
-
71-
- assert.equal(currentURL(), '/super-rentals');
71+
- assert.strictEqual(currentURL(), '/super-rentals');
7272
+ test('visiting /', async function (assert) {
7373
+ await visit('/');
7474
+
75-
+ assert.equal(currentURL(), '/');
75+
+ assert.strictEqual(currentURL(), '/');
7676
+ assert.dom('h2').hasText('Welcome to Super Rentals!');
7777
+
7878
+ assert.dom('.jumbo a.button').hasText('About Us');
7979
+ await click('.jumbo a.button');
8080
+
81-
+ assert.equal(currentURL(), '/about');
81+
+ assert.strictEqual(currentURL(), '/about');
8282
});
8383
```
8484
@@ -152,25 +152,25 @@ Let's practice what we learned by adding tests for the remaining pages:
152152
+ test('visiting /about', async function (assert) {
153153
+ await visit('/about');
154154
+
155-
+ assert.equal(currentURL(), '/about');
155+
+ assert.strictEqual(currentURL(), '/about');
156156
+ assert.dom('h2').hasText('About Super Rentals');
157157
+
158158
+ assert.dom('.jumbo a.button').hasText('Contact Us');
159159
+ await click('.jumbo a.button');
160160
+
161-
+ assert.equal(currentURL(), '/getting-in-touch');
161+
+ assert.strictEqual(currentURL(), '/getting-in-touch');
162162
+ });
163163
+
164164
+ test('visiting /getting-in-touch', async function (assert) {
165165
+ await visit('/getting-in-touch');
166166
+
167-
+ assert.equal(currentURL(), '/getting-in-touch');
167+
+ assert.strictEqual(currentURL(), '/getting-in-touch');
168168
+ assert.dom('h2').hasText('Contact Us');
169169
+
170170
+ assert.dom('.jumbo a.button').hasText('About');
171171
+ await click('.jumbo a.button');
172172
+
173-
+ assert.equal(currentURL(), '/about');
173+
+ assert.strictEqual(currentURL(), '/about');
174174
+ });
175175
});
176176
```

src/markdown/tutorial/part-1/04-component-basics.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,17 @@ But what kind of test? We *could* write a component test for the `<NavBar>` by i
265265
266266
```run:file:patch lang=js cwd=super-rentals filename=tests/acceptance/super-rentals-test.js
267267
@@ -11,2 +11,4 @@
268-
assert.equal(currentURL(), '/');
268+
assert.strictEqual(currentURL(), '/');
269269
+ assert.dom('nav').exists();
270270
+ assert.dom('h1').hasText('SuperRentals');
271271
assert.dom('h2').hasText('Welcome to Super Rentals!');
272272
@@ -23,2 +25,4 @@
273-
assert.equal(currentURL(), '/about');
273+
assert.strictEqual(currentURL(), '/about');
274274
+ assert.dom('nav').exists();
275275
+ assert.dom('h1').hasText('SuperRentals');
276276
assert.dom('h2').hasText('About Super Rentals');
277277
@@ -35,2 +39,4 @@
278-
assert.equal(currentURL(), '/getting-in-touch');
278+
assert.strictEqual(currentURL(), '/getting-in-touch');
279279
+ assert.dom('nav').exists();
280280
+ assert.dom('h1').hasText('SuperRentals');
281281
assert.dom('h2').hasText('Contact Us');
@@ -291,13 +291,13 @@ But what kind of test? We *could* write a component test for the `<NavBar>` by i
291291
+ assert.dom('nav a.menu-contact').hasText('Contact');
292292
+
293293
+ await click('nav a.menu-about');
294-
+ assert.equal(currentURL(), '/about');
294+
+ assert.strictEqual(currentURL(), '/about');
295295
+
296296
+ await click('nav a.menu-contact');
297-
+ assert.equal(currentURL(), '/getting-in-touch');
297+
+ assert.strictEqual(currentURL(), '/getting-in-touch');
298298
+
299299
+ await click('nav a.menu-index');
300-
+ assert.equal(currentURL(), '/');
300+
+ assert.strictEqual(currentURL(), '/');
301301
+ });
302302
});
303303
```

src/markdown/tutorial/part-2/09-route-params.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,13 @@ Finally, let's add a `rental` template to actually *invoke* our `<Rental::Detail
333333
+ assert.dom('.rental').exists({ count: 3 });
334334
+
335335
+ await click('.rental:first-of-type a');
336-
+ assert.equal(currentURL(), '/rentals/grand-old-mansion');
336+
+ assert.strictEqual(currentURL(), '/rentals/grand-old-mansion');
337337
+ });
338338
+
339339
+ test('visiting /rentals/grand-old-mansion', async function (assert) {
340340
+ await visit('/rentals/grand-old-mansion');
341341
+
342-
+ assert.equal(currentURL(), '/rentals/grand-old-mansion');
342+
+ assert.strictEqual(currentURL(), '/rentals/grand-old-mansion');
343343
+ assert.dom('nav').exists();
344344
+ assert.dom('h1').containsText('SuperRentals');
345345
+ assert.dom('h2').containsText('Grand Old Mansion');

src/markdown/tutorial/part-2/10-service-injection.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ To be sure, let's add some tests! Let's start with an acceptance test:
174174
+ let button = find('.share.button');
175175
+
176176
+ let tweetURL = new URL(button.href);
177-
+ assert.equal(tweetURL.host, 'twitter.com');
177+
+ assert.strictEqual(tweetURL.host, 'twitter.com');
178178
+
179-
+ assert.equal(
179+
+ assert.strictEqual(
180180
+ tweetURL.searchParams.get('url'),
181181
+ `${window.location.origin}/rentals/grand-old-mansion`
182182
+ );
@@ -202,7 +202,7 @@ wait #qunit-banner.qunit-fail
202202

203203
Looking at the failure closely, the problem seems to be that the component had captured `http://localhost:4200/tests` as the "current page's URL". The issue here is that the `<ShareButton>` component uses `window.location.href` to capture the current URL. Because we are running our tests at `http://localhost:4200/tests`, that's what we got. *Technically* it's not wrong, but this is certainly not what we meant. Gotta love computers!
204204

205-
This brings up an interesting question – why does the `currentURL()` test helper not have the same problem? In our test, we have been writing assertions like `assert.equal(currentURL(), '/about');`, and those assertions did not fail.
205+
This brings up an interesting question – why does the `currentURL()` test helper not have the same problem? In our test, we have been writing assertions like `assert.strictEqual(currentURL(), '/about');`, and those assertions did not fail.
206206

207207
It turns out that this is something Ember's router handled for us. In an Ember app, the router is responsible for handling navigation and maintaining the URL. For example, when you click on a `<LinkTo>` component, it will ask the router to execute a *[route transition](../../../routing/preventing-and-retrying-transitions/)*. Normally, the router is set up to update the browser's address bar whenever it transitions into a new route. That way, your users will be able to use the browser's back button and bookmark functionality just like any other webpage.
208208

@@ -262,16 +262,21 @@ We will take advantage of this capability in our component test:
262262
import { setupRenderingTest } from 'ember-qunit';
263263
+import Service from '@ember/service';
264264
import { render } from '@ember/test-helpers';
265-
@@ -5,2 +6,8 @@
265+
@@ -5,2 +6,13 @@
266266

267+
+const MOCK_URL = new URL(
268+
+ '/foo/bar?baz=true#some-section',
269+
+ window.location.origin
270+
+);
271+
+
267272
+class MockRouterService extends Service {
268273
+ get currentURL() {
269274
+ return '/foo/bar?baz=true#some-section';
270275
+ }
271276
+}
272277
+
273278
module('Integration | Component | share-button', function (hooks) {
274-
@@ -8,18 +15,22 @@
279+
@@ -8,18 +20,20 @@
275280

276281
- test('it renders', async function (assert) {
277282
- // Set any properties with this.set('myProperty', 'value');
@@ -300,9 +305,7 @@ We will take advantage of this capability in our component test:
300305
+ .hasAttribute('rel', 'external nofollow noopener noreferrer')
301306
+ .hasAttribute(
302307
+ 'href',
303-
+ `https://twitter.com/intent/tweet?url=${encodeURIComponent(
304-
+ new URL('/foo/bar?baz=true#some-section', window.location.origin)
305-
+ )}`
308+
+ `https://twitter.com/intent/tweet?url=${encodeURIComponent(MOCK_URL.href)}`
306309
+ )
307310
+ .hasClass('share')
308311
+ .hasClass('button')
@@ -329,7 +332,7 @@ While we are here, let's add some more tests for the various functionalities of
329332
-import { render } from '@ember/test-helpers';
330333
+import { find, render } from '@ember/test-helpers';
331334
import { hbs } from 'ember-cli-htmlbars';
332-
@@ -17,2 +17,8 @@
335+
@@ -22,2 +22,8 @@
333336
this.owner.register('service:router', MockRouterService);
334337
+
335338
+ this.tweetParam = (param) => {
@@ -338,44 +341,39 @@ While we are here, let's add some more tests for the various functionalities of
338341
+ return url.searchParams.get(param);
339342
+ };
340343
});
341-
@@ -26,8 +32,3 @@
344+
@@ -31,6 +37,3 @@
342345
.hasAttribute('rel', 'external nofollow noopener noreferrer')
343346
- .hasAttribute(
344347
- 'href',
345-
- `https://twitter.com/intent/tweet?url=${encodeURIComponent(
346-
- new URL('/foo/bar?baz=true#some-section', window.location.origin)
347-
- )}`
348+
- `https://twitter.com/intent/tweet?url=${encodeURIComponent(MOCK_URL.href)}`
348349
- )
349350
+ .hasAttribute('href', /^https:\/\/twitter\.com\/intent\/tweet/)
350351
.hasClass('share')
351-
@@ -35,2 +36,53 @@
352+
@@ -38,2 +41,50 @@
352353
.containsText('Tweet this!');
353354
+
354-
+ assert.equal(
355-
+ this.tweetParam('url'),
356-
+ new URL('/foo/bar?baz=true#some-section', window.location.origin)
357-
+ );
355+
+ assert.strictEqual(this.tweetParam('url'), MOCK_URL.href);
358356
+ });
359357
+
360358
+ test('it supports passing @text', async function (assert) {
361359
+ await render(
362360
+ hbs`<ShareButton @text="Hello Twitter!">Tweet this!</ShareButton>`
363361
+ );
364362
+
365-
+ assert.equal(this.tweetParam('text'), 'Hello Twitter!');
363+
+ assert.strictEqual(this.tweetParam('text'), 'Hello Twitter!');
366364
+ });
367365
+
368366
+ test('it supports passing @hashtags', async function (assert) {
369367
+ await render(
370368
+ hbs`<ShareButton @hashtags="foo,bar,baz">Tweet this!</ShareButton>`
371369
+ );
372370
+
373-
+ assert.equal(this.tweetParam('hashtags'), 'foo,bar,baz');
371+
+ assert.strictEqual(this.tweetParam('hashtags'), 'foo,bar,baz');
374372
+ });
375373
+
376374
+ test('it supports passing @via', async function (assert) {
377375
+ await render(hbs`<ShareButton @via="emberjs">Tweet this!</ShareButton>`);
378-
+ assert.equal(this.tweetParam('via'), 'emberjs');
376+
+ assert.strictEqual(this.tweetParam('via'), 'emberjs');
379377
+ });
380378
+
381379
+ test('it supports adding extra classes', async function (assert) {

src/markdown/tutorial/part-2/11-ember-data.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,19 @@ The generator created some boilerplate code for us, which serves as a pretty goo
129129
+ 'This grand old mansion sits on over 100 acres of rolling hills and dense redwood forests.',
130130
+ });
131131
+
132-
+ assert.equal(rental.type, 'Standalone');
132+
+ assert.strictEqual(rental.type, 'Standalone');
133133
+
134134
+ rental.category = 'Condo';
135-
+ assert.equal(rental.type, 'Community');
135+
+ assert.strictEqual(rental.type, 'Community');
136136
+
137137
+ rental.category = 'Townhouse';
138-
+ assert.equal(rental.type, 'Community');
138+
+ assert.strictEqual(rental.type, 'Community');
139139
+
140140
+ rental.category = 'Apartment';
141-
+ assert.equal(rental.type, 'Community');
141+
+ assert.strictEqual(rental.type, 'Community');
142142
+
143143
+ rental.category = 'Estate';
144-
+ assert.equal(rental.type, 'Standalone');
144+
+ assert.strictEqual(rental.type, 'Standalone');
145145
});
146146
```
147147

0 commit comments

Comments
 (0)