Skip to content

Commit 627ac6d

Browse files
committed
tests/helpers/setup-mirage: Extract authenticateAs() helper method
1 parent 7d34eec commit 627ac6d

File tree

6 files changed

+13
-21
lines changed

6 files changed

+13
-21
lines changed

tests/acceptance/api-token-test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { currentURL, findAll, click, fillIn } from '@ember/test-helpers';
4-
import window, { setupWindowMock } from 'ember-window-mock';
54
import { Response } from 'ember-cli-mirage';
65
import { percySnapshot } from 'ember-percy';
76

@@ -10,7 +9,6 @@ import { visit } from '../helpers/visit-ignoring-abort';
109

1110
module('Acceptance | api-tokens', function (hooks) {
1211
setupApplicationTest(hooks);
13-
setupWindowMock(hooks);
1412
setupMirage(hooks);
1513

1614
function prepare(context) {
@@ -21,9 +19,6 @@ module('Acceptance | api-tokens', function (hooks) {
2119
avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',
2220
});
2321

24-
context.server.create('mirage-session', { user });
25-
window.localStorage.setItem('isLoggedIn', '1');
26-
2722
context.server.get('/api/v1/me/tokens', {
2823
api_tokens: [
2924
{ id: 2, name: 'BAR', created_at: new Date('2017-11-19T17:59:22').toISOString(), last_used_at: null },
@@ -35,6 +30,8 @@ module('Acceptance | api-tokens', function (hooks) {
3530
},
3631
],
3732
});
33+
34+
context.authenticateAs(user);
3835
}
3936

4037
test('/me is showing the list of active API tokens', async function (assert) {

tests/acceptance/crate-following-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { visit, waitFor, settled, click } from '@ember/test-helpers';
4-
import window, { setupWindowMock } from 'ember-window-mock';
54
import { defer } from 'rsvp';
65

76
import setupMirage from '../helpers/setup-mirage';
87

98
module('Acceptance | Crate following', function (hooks) {
109
setupApplicationTest(hooks);
11-
setupWindowMock(hooks);
1210
setupMirage(hooks);
1311

1412
function prepare(context, { loggedIn = true } = {}) {
@@ -19,8 +17,7 @@ module('Acceptance | Crate following', function (hooks) {
1917

2018
if (loggedIn) {
2119
let user = server.create('user');
22-
server.create('mirage-session', { user });
23-
window.localStorage.setItem('isLoggedIn', '1');
20+
context.authenticateAs(user);
2421
}
2522
}
2623

tests/acceptance/dashboard-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { currentURL } from '@ember/test-helpers';
4-
import window, { setupWindowMock } from 'ember-window-mock';
54
import { percySnapshot } from 'ember-percy';
65

76
import setupMirage from '../helpers/setup-mirage';
87
import { visit } from '../helpers/visit-ignoring-abort';
98

109
module('Acceptance | Dashboard', function (hooks) {
1110
setupApplicationTest(hooks);
12-
setupWindowMock(hooks);
1311
setupMirage(hooks);
1412

1513
test('redirects to / when not logged in', async function (assert) {
@@ -26,8 +24,7 @@ module('Acceptance | Dashboard', function (hooks) {
2624
avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',
2725
});
2826

29-
this.server.create('mirage-session', { user });
30-
window.localStorage.setItem('isLoggedIn', '1');
27+
this.authenticateAs(user);
3128

3229
{
3330
let crate = this.server.create('crate', { name: 'rand' });

tests/acceptance/email-confirmation-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { currentURL } from '@ember/test-helpers';
4-
import window, { setupWindowMock } from 'ember-window-mock';
54

65
import { visit } from '../helpers/visit-ignoring-abort';
76
import setupMirage from '../helpers/setup-mirage';
87

98
module('Acceptance | Email Confirmation', function (hooks) {
109
setupApplicationTest(hooks);
11-
setupWindowMock(hooks);
1210
setupMirage(hooks);
1311

1412
test('unauthenticated happy path', async function (assert) {
@@ -27,8 +25,7 @@ module('Acceptance | Email Confirmation', function (hooks) {
2725
let user = this.server.create('user', { emailVerificationToken: 'badc0ffee' });
2826
assert.strictEqual(user.emailVerified, false);
2927

30-
this.server.create('mirage-session', { user });
31-
window.localStorage.setItem('isLoggedIn', '1');
28+
this.authenticateAs(user);
3229

3330
await visit('/confirm/badc0ffee');
3431
assert.equal(currentURL(), '/confirm/badc0ffee');

tests/acceptance/invites-test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { currentURL, click } from '@ember/test-helpers';
4-
import window, { setupWindowMock } from 'ember-window-mock';
54
import { percySnapshot } from 'ember-percy';
65
import Response from 'ember-cli-mirage/response';
76

@@ -10,13 +9,11 @@ import { visit } from '../helpers/visit-ignoring-abort';
109

1110
module('Acceptance | /me/pending-invites', function (hooks) {
1211
setupApplicationTest(hooks);
13-
setupWindowMock(hooks);
1412
setupMirage(hooks);
1513

1614
function prepare(context) {
1715
let user = context.server.create('user');
18-
context.server.create('mirage-session', { user });
19-
window.localStorage.setItem('isLoggedIn', '1');
16+
context.authenticateAs(user);
2017

2118
context.server.get('/api/v1/me/crate_owner_invitations', {
2219
crate_owner_invitations: [

tests/helpers/setup-mirage.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
22
import timekeeper from 'timekeeper';
3+
import window, { setupWindowMock } from 'ember-window-mock';
34

45
export default function (hooks) {
56
setupMirage(hooks);
7+
setupWindowMock(hooks);
68

79
// To have deterministic visual tests, the seed has to be constant
810
hooks.beforeEach(function () {
911
timekeeper.freeze(new Date('11/20/2017 12:00'));
12+
13+
this.authenticateAs = user => {
14+
this.server.create('mirage-session', { user });
15+
window.localStorage.setItem('isLoggedIn', '1');
16+
};
1017
});
1118

1219
hooks.afterEach(function () {

0 commit comments

Comments
 (0)