Skip to content

Commit 5b12432

Browse files
committed
refactor to use switch case & fix error test
1 parent f27c973 commit 5b12432

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

cypress/e2e/requests.cy.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,34 @@ describe('Viewing all requests', () => {
2222
// Intercept the response from the endpoint to view all requests
2323
// TODO(summer-cook): extract out this base url into the config to use as an environment variable. it was not cooperating before
2424
cy.intercept('GET', `${scientistApiBaseURL}/quote_groups/mine.json`, (req) => {
25-
if ((requestList === undefined) && (loading === true)) {
25+
switch (true) {
2626
// reply with an empty response: both data and error will be undefined.
27-
req.reply()
28-
} else if ((requestList === undefined) && (loading === false) && (error === true)) {
27+
case loading : req.reply()
28+
break
29+
2930
// error will be defined
30-
req.reply({ statusCode: 500 })
31-
} else if (requestList === true) {
31+
case error : req.reply({ statusCode: 500 })
32+
break
33+
34+
case requestList : req.reply({ fixture: 'all-requests/requests.json' })
35+
break
36+
3237
// reply with a request body- default status code is 200
33-
req.reply({ fixture: 'all-requests/requests.json' })
34-
} else if (requestList === false) {
35-
req.reply({ fixture: 'all-requests/no-requests.json' })
38+
case !requestList : req.reply({ fixture: 'all-requests/no-requests.json' })
39+
break
40+
3641
}
3742
})
3843
// Intercept the response from the endpoint that gets the default ware ID
39-
cy.intercept('GET', `${scientistApiBaseURL}/wares.json?q=make-a-request`, { fixture: 'all-requests/make-a-request.json' })
44+
cy.intercept('GET', `${scientistApiBaseURL}/wares.json?q=make-a-request`, (req) => {
45+
switch (true) {
46+
case error : req.reply({ statusCode: 500 })
47+
break
48+
49+
default : req.reply({ fixture: 'all-requests/make-a-request.json' })
50+
break
51+
}
52+
})
4053
cy.visit('/requests')
4154
})
4255

@@ -52,32 +65,32 @@ describe('Viewing all requests', () => {
5265
})
5366
})
5467

55-
// TODO: uncomment this and try to make the page it not get an undefined error
56-
// context('error while making a request to the api', () => {
57-
// before(() => {
58-
// requestList = undefined
59-
// loading = false
60-
// error = true
61-
// })
62-
// it('should show an error message.', () => {
63-
// cy.get("div[role='alert']").should('be.visible').then(() => {
64-
// cy.log('Successfully hits an error.')
65-
// })
66-
// })
67-
// })
68+
context('error while making a request to the api', () => {
69+
before(() => {
70+
requestList = undefined
71+
loading = false
72+
error = true
73+
})
74+
it('should show an error message.', () => {
75+
cy.get("div[role='alert']").should('be.visible').then(() => {
76+
cy.log('Successfully hits an error.')
77+
})
78+
})
79+
})
6880

6981
describe('request components are loading successfully, &', () => {
7082
context('the user has requests', () => {
7183
before(() => {
7284
requestList = true
85+
error = false
7386
})
7487
it("should show the user's request list.", () => {
7588
cy.get('article.request-item').should('exist').then(() => {
7689
cy.log('Successfully viewing request list.')
7790
})
7891
})
7992
})
80-
93+
8194
context('the user has 0 requests', () => {
8295
before(() => {
8396
requestList = false

0 commit comments

Comments
 (0)