Skip to content

Commit 7569d58

Browse files
committed
Switch from mock server to Cypress interception
1 parent 68c8922 commit 7569d58

File tree

5 files changed

+31
-74
lines changed

5 files changed

+31
-74
lines changed

e2e/fixtures/cities.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"cities": [
3+
{
4+
"_id": "123abc",
5+
"name": "London"
6+
}
7+
]
8+
}

e2e/fixtures/server.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

e2e/integration/journey.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const mockServerURL = 'http://localhost:3001'
22

33
beforeEach(() => {
4-
cy.request('POST', `${mockServerURL}/_reset`)
5-
4+
cy.intercept('GET', `${mockServerURL}/cities`, { fixture: 'cities.json' })
65
cy.visit('/')
76
})
87

98
it('can submit a minimal form', () => {
9+
cy.intercept('POST', `${mockServerURL}/volunteer`, req => {
10+
req.reply({ volunteer: { _id: 'some-new-id', ...req.body } })
11+
}).as('createVolunteer')
1012
const initialData = {
1113
firstName: 'Jane',
1214
lastName: 'Doe',
@@ -41,10 +43,7 @@ it('can submit a minimal form', () => {
4143
cy.findByRole('checkbox', { name: /contact me/i }).check()
4244
cy.findByRole('button', { name: /submit/i }).click()
4345

44-
cy.request(`${mockServerURL}/_calls`).then(response => {
45-
const [{ body: payload, method, path }] = response.body
46-
expect(method).to.equal('POST')
47-
expect(path).to.equal('/volunteer')
46+
cy.wait('@createVolunteer').then(({ request: { body: payload } }) => {
4847
expect(payload).to.deep.eq({
4948
...initialData,
5049
agreeToReceiveCommunication: true,
@@ -65,6 +64,10 @@ it('can submit a minimal form', () => {
6564
})
6665

6766
it('requires employee selection', () => {
67+
cy.intercept('POST', `${mockServerURL}/volunteer`, req => {
68+
req.reply({ volunteer: { _id: 'some-new-id', ...req.body } })
69+
}).as('createVolunteer')
70+
6871
cy.findByRole('textbox', { name: /first name/i }).type('Laura')
6972
cy.findByRole('textbox', { name: /last name/i }).type('Olsen')
7073
cy.findByRole('combobox', { name: /city/i }).select('London')
@@ -88,9 +91,7 @@ it('requires employee selection', () => {
8891
)
8992
cy.findByRole('button', { name: /submit/i }).click()
9093

91-
cy.request(`${mockServerURL}/_calls`).then(({ body }) => {
92-
expect(body).to.have.length(1)
93-
const [{ body: payload }] = body
94+
cy.wait('@createVolunteer').then(({ request: { body: payload } }) => {
9495
expect(payload).to.have.property('hearAboutCYF', 'Employer')
9596
expect(payload).to.have.property('employer', 'Capgemini')
9697
})

e2e/integration/withCode.test.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const mockServerURL = 'http://localhost:3001'
22

33
beforeEach(() => {
4-
cy.request('POST', `${mockServerURL}/_reset`)
4+
cy.intercept('GET', `${mockServerURL}/cities`, { fixture: 'cities.json' })
55
})
66

77
it('shows success message', () => {
@@ -12,6 +12,9 @@ it('shows success message', () => {
1212
it('includes the user ID when resubmitting', () => {
1313
const userId = 'something'
1414
const code = 'does-this-matter'
15+
cy.intercept('POST', `${mockServerURL}/volunteer`, req => {
16+
req.reply({ volunteer: { _id: 'some-new-id', ...req.body } })
17+
}).as('createVolunteer')
1518
cy.visit(`/code/${userId}/${code}`)
1619

1720
cy.findByRole('textbox', { name: /first name/i }).type('Erhard')
@@ -29,16 +32,15 @@ it('includes the user ID when resubmitting', () => {
2932
cy.findByRole('checkbox', { name: /contact me/i }).check()
3033
cy.findByRole('button', { name: /submit/i }).click()
3134

32-
cy.request(`${mockServerURL}/_calls`).then(({ body }) => {
33-
expect(body).to.have.length(1)
34-
const [{ body: payload, method, path }] = body
35-
expect(method).to.equal('POST')
36-
expect(path).to.equal('/volunteer')
35+
cy.wait('@createVolunteer').then(({ request: { body: payload } }) => {
3736
expect(payload).to.have.property('userId', userId)
3837
})
3938
})
4039

4140
it('lets you request a reminder email', () => {
41+
cy.intercept('POST', `${mockServerURL}/volunteer/email/verification`, {
42+
statusCode: 200
43+
}).as('verifyEmail')
4244
const userId = 'whoever'
4345
const code = 'whatever'
4446
const email = '[email protected]'
@@ -52,11 +54,8 @@ it('lets you request a reminder email', () => {
5254
// TODO not accessible by label
5355
cy.findByRole('textbox').type(email)
5456
cy.findByRole('button', { name: /submit/i }).click()
55-
cy.request(`${mockServerURL}/_calls`).then(({ body }) => {
56-
expect(body).to.have.length(1)
57-
const [{ body: payload, method, path }] = body
58-
expect(method).to.equal('POST')
59-
expect(path).to.equal('/volunteer/email/verification')
57+
58+
cy.wait('@verifyEmail').then(({ request: { body: payload } }) => {
6059
expect(payload).to.deep.equal({ email, userId })
6160
})
6261
})

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
},
1919
"scripts": {
2020
"build": "react-scripts build",
21-
"e2e": "concurrently -k -s first -n \"app,mock,e2e\" \"BROWSER=none yarn start\" \"yarn e2e:mock\" \"yarn e2e:run\"",
22-
"e2e:dev": "concurrently -k -n \"app,mock,e2e\" \"yarn start\" \"yarn e2e:mock\" \"cypress open --e2e\"",
23-
"e2e:mock": "node ./e2e/fixtures/server.js",
24-
"e2e:prod": "concurrently -k -s first -n \"app,mock,e2e\" \"yarn serve\" \"yarn e2e:mock\" \"yarn e2e:run\"",
21+
"e2e": "concurrently -k -s first -n \"app,e2e\" \"BROWSER=none yarn start\" \"yarn e2e:run\"",
22+
"e2e:dev": "concurrently -k -n \"app,e2e\" \"yarn start\" \"cypress open --e2e\"",
23+
"e2e:prod": "concurrently -k -s first -n \"app,e2e\" \"yarn serve\" \"yarn e2e:run\"",
2524
"e2e:run": "wait-on -l http-get://localhost:3000 && cypress run",
2625
"eject": "react-scripts eject",
2726
"lint": "yarn lint:eslint && yarn lint:prettier --check",

0 commit comments

Comments
 (0)