Skip to content

Commit b02831c

Browse files
committed
e2e/acceptance/email-change: Migrate from mirage to @crates-io/msw
1 parent e612bbd commit b02831c

File tree

1 file changed

+40
-58
lines changed

1 file changed

+40
-58
lines changed

e2e/acceptance/email-change.spec.ts

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { test, expect } from '@/e2e/helper';
1+
import { expect, test } from '@/e2e/helper';
2+
import { http, HttpResponse } from 'msw';
23

34
test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
4-
test('happy path', async ({ page, mirage }) => {
5-
await mirage.addHook(server => {
6-
let user = server.create('user', { email: '[email protected]' });
7-
globalThis.user = user;
8-
authenticateAs(user);
9-
});
5+
test('happy path', async ({ page, msw }) => {
6+
let user = msw.db.user.create({ email: '[email protected]' });
7+
await msw.authenticateAs(user);
108

119
await page.goto('/settings/profile');
1210
await expect(page).toHaveURL('/settings/profile');
@@ -39,19 +37,15 @@ test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
3937
await expect(emailInput.locator('[data-test-verification-sent]')).toBeVisible();
4038
await expect(emailInput.locator('[data-test-resend-button]')).toBeEnabled();
4139

42-
await page.evaluate(() => globalThis.user.reload());
43-
await page.waitForFunction(expect => globalThis.user.email === expect, '[email protected]');
44-
await page.waitForFunction(expect => globalThis.user.emailVerified === expect, false);
45-
await page.waitForFunction(() => !!globalThis.user.emailVerificationToken);
40+
user = msw.db.user.findFirst({ where: { id: { equals: user.id } } });
41+
await expect(user.email).toBe('[email protected]');
42+
await expect(user.emailVerified).toBe(false);
43+
await expect(user.emailVerificationToken).toBeDefined();
4644
});
4745

48-
test('happy path with `email: null`', async ({ page, mirage }) => {
49-
await mirage.addHook(server => {
50-
let user = server.create('user', { email: undefined });
51-
52-
authenticateAs(user);
53-
globalThis.user = user;
54-
});
46+
test('happy path with `email: null`', async ({ page, msw }) => {
47+
let user = msw.db.user.create({ email: undefined });
48+
await msw.authenticateAs(user);
5549

5650
await page.goto('/settings/profile');
5751
await expect(page).toHaveURL('/settings/profile');
@@ -80,19 +74,15 @@ test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
8074
await expect(emailInput.locator('[data-test-verification-sent]')).toBeVisible();
8175
await expect(emailInput.locator('[data-test-resend-button]')).toBeEnabled();
8276

83-
await page.evaluate(() => globalThis.user.reload());
84-
await page.waitForFunction(expect => globalThis.user.email === expect, '[email protected]');
85-
await page.waitForFunction(expect => globalThis.user.emailVerified === expect, false);
86-
await page.waitForFunction(() => !!globalThis.user.emailVerificationToken);
77+
user = msw.db.user.findFirst({ where: { id: { equals: user.id } } });
78+
await expect(user.email).toBe('[email protected]');
79+
await expect(user.emailVerified).toBe(false);
80+
await expect(user.emailVerificationToken).toBeDefined();
8781
});
8882

89-
test('cancel button', async ({ page, mirage }) => {
90-
await mirage.addHook(server => {
91-
let user = server.create('user', { email: '[email protected]' });
92-
93-
authenticateAs(user);
94-
globalThis.user = user;
95-
});
83+
test('cancel button', async ({ page, msw }) => {
84+
let user = msw.db.user.create({ email: '[email protected]' });
85+
await msw.authenticateAs(user);
9686

9787
await page.goto('/settings/profile');
9888
const emailInput = page.locator('[data-test-email-input]');
@@ -106,21 +96,18 @@ test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
10696
await expect(emailInput.locator('[data-test-not-verified]')).toHaveCount(0);
10797
await expect(emailInput.locator('[data-test-verification-sent]')).toHaveCount(0);
10898

109-
await page.evaluate(() => globalThis.user.reload());
110-
await page.waitForFunction(expect => globalThis.user.email === expect, '[email protected]');
111-
await page.waitForFunction(expect => globalThis.user.emailVerified === expect, true);
112-
await page.waitForFunction(() => !globalThis.user.emailVerificationToken);
99+
user = msw.db.user.findFirst({ where: { id: { equals: user.id } } });
100+
await expect(user.email).toBe('[email protected]');
101+
await expect(user.emailVerified).toBe(true);
102+
await expect(user.emailVerificationToken).toBe(null);
113103
});
114104

115-
test('server error', async ({ page, mirage }) => {
116-
await mirage.addHook(server => {
117-
let user = server.create('user', { email: '[email protected]' });
105+
test('server error', async ({ page, msw }) => {
106+
let user = msw.db.user.create({ email: '[email protected]' });
107+
await msw.authenticateAs(user);
118108

119-
authenticateAs(user);
120-
globalThis.user = user;
121-
122-
server.put('/api/v1/users/:user_id', {}, 500);
123-
});
109+
let error = HttpResponse.json({}, { status: 500 });
110+
await msw.worker.use(http.put('/api/v1/users/:user_id', () => error));
124111

125112
await page.goto('/settings/profile');
126113
const emailInput = page.locator('[data-test-email-input]');
@@ -134,19 +121,16 @@ test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
134121
'Error in saving email: An unknown error occurred while saving this email.',
135122
);
136123

137-
await page.evaluate(() => globalThis.user.reload());
138-
await page.waitForFunction(expect => globalThis.user.email === expect, '[email protected]');
139-
await page.waitForFunction(expect => globalThis.user.emailVerified === expect, true);
140-
await page.waitForFunction(() => !globalThis.user.emailVerificationToken);
124+
user = msw.db.user.findFirst({ where: { id: { equals: user.id } } });
125+
await expect(user.email).toBe('[email protected]');
126+
await expect(user.emailVerified).toBe(true);
127+
await expect(user.emailVerificationToken).toBe(null);
141128
});
142129

143130
test.describe('Resend button', function () {
144-
test('happy path', async ({ page, mirage }) => {
145-
await mirage.addHook(server => {
146-
let user = server.create('user', { email: '[email protected]', emailVerificationToken: 'secret123' });
147-
148-
authenticateAs(user);
149-
});
131+
test('happy path', async ({ page, msw }) => {
132+
let user = msw.db.user.create({ email: '[email protected]', emailVerificationToken: 'secret123' });
133+
await msw.authenticateAs(user);
150134

151135
await page.goto('/settings/profile');
152136
await expect(page).toHaveURL('/settings/profile');
@@ -165,14 +149,12 @@ test.describe('Acceptance | Email Change', { tag: '@acceptance' }, () => {
165149
await expect(button).toHaveText('Sent!');
166150
});
167151

168-
test('server error', async ({ page, mirage }) => {
169-
await mirage.addHook(server => {
170-
let user = server.create('user', { email: '[email protected]', emailVerificationToken: 'secret123' });
171-
172-
authenticateAs(user);
152+
test('server error', async ({ page, msw }) => {
153+
let user = msw.db.user.create({ email: '[email protected]', emailVerificationToken: 'secret123' });
154+
await msw.authenticateAs(user);
173155

174-
server.put('/api/v1/users/:user_id/resend', {}, 500);
175-
});
156+
let error = HttpResponse.json({}, { status: 500 });
157+
await msw.worker.use(http.put('/api/v1/users/:user_id/resend', () => error));
176158

177159
await page.goto('/settings/profile');
178160
await expect(page).toHaveURL('/settings/profile');

0 commit comments

Comments
 (0)