Skip to content

Commit 779c420

Browse files
authored
Merge pull request #248 from scientist-softserv/refactor-api-intercept-and-use-in-requests
Refactor api intercept function
2 parents 32a3bab + 02983e1 commit 779c420

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

cypress/e2e/requests.cy.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { scientistApiBaseURL } from '../support/e2e'
2+
13
describe('Viewing all requests', () => {
24
describe('as a logged out user', () => {
35
it('should show an error message.', () => {
@@ -10,7 +12,6 @@ describe('Viewing all requests', () => {
1012
})
1113

1214
describe('as a logged in user', () => {
13-
let scientistApiBaseURL = `https://${Cypress.env('NEXT_PUBLIC_PROVIDER_NAME')}.scientist.com/api/v2`
1415
// declare variables that can be used to change how the response is intercepted.
1516
let requestList
1617
let loading
@@ -20,34 +21,23 @@ describe('Viewing all requests', () => {
2021
// Call the custom cypress command to log in
2122
cy.login(Cypress.env('TEST_SCIENTIST_USER'), Cypress.env('TEST_SCIENTIST_PW'))
2223
// Intercept the response from the endpoint to view all requests
23-
// TODO(summer-cook): extract out this base url into the config to use as an environment variable. it was not cooperating before
24-
cy.intercept('GET', `${scientistApiBaseURL}/quote_groups/mine.json`, (req) => {
25-
switch (true) {
26-
// reply with an empty response: both data and error will be undefined.
27-
case loading: req.reply()
28-
break
29-
30-
// error will be defined
31-
case error: req.reply({ statusCode: 500 })
32-
break
33-
34-
case requestList: req.reply({ fixture: 'all-requests/requests.json' })
35-
break
36-
37-
// reply with a request body- default status code is 200
38-
case !requestList: req.reply({ fixture: 'all-requests/no-requests.json' })
39-
break
40-
}
24+
cy.customApiIntercept({
25+
action: 'GET',
26+
alias: 'useAllRequests',
27+
requestURL: `/quote_groups/mine.json`,
28+
data: requestList,
29+
defaultFixture: 'all-requests/requests.json',
30+
emptyFixture: 'all-requests/no-requests.json',
31+
loading,
32+
error
4133
})
4234
// Intercept the response from the endpoint that gets the default ware ID
43-
cy.intercept('GET', `${scientistApiBaseURL}/wares.json?q=make-a-request`, (req) => {
44-
switch (true) {
45-
case error: req.reply({ statusCode: 500 })
46-
break
47-
48-
default: req.reply({ fixture: 'all-requests/make-a-request.json' })
49-
break
50-
}
35+
cy.customApiIntercept({
36+
action: 'GET',
37+
alias: 'useDefaultWare',
38+
requestURL: `/wares.json?q=make-a-request`,
39+
defaultFixture: 'all-requests/make-a-request.json',
40+
error
5141
})
5242
cy.visit('/requests')
5343
})

cypress/support/commands.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//
2424
// -- This will overwrite an existing command --
2525
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
26-
26+
import { scientistApiBaseURL } from './e2e'
2727

2828
// add a command to login that uses a session, so the user will remain logged in throughout the test file vs. needing to log in before each example.
2929
// source: https://github.com/nextauthjs/next-auth/discussions/2053#discussioncomment-1191016
@@ -36,4 +36,34 @@ Cypress.Commands.add('login', (username, password) => {
3636
// This cookie also may need to be refreshed intermittently if it expires
3737
cy.setCookie("next-auth.session-token", Cypress.env('TEST_SESSION_COOKIE'));
3838
})
39+
})
40+
41+
// intercepts requests and creates potential cases for loading, error, data, and empty data
42+
// required params are action, defaultFixture, requestURL
43+
// optional params such as data, loading, and error can be passed depending on the creation of test cases that are related to that specific api call
44+
Cypress.Commands.add('customApiIntercept', ({
45+
action, alias, data, defaultFixture, emptyFixture, error, errorCaseStatusCode, loading, requestURL
46+
}) => {
47+
cy.intercept(action, scientistApiBaseURL + requestURL, (req) => {
48+
switch (true) {
49+
// reply with an empty response: both data and error will be undefined.
50+
case loading: req.reply()
51+
break
52+
53+
// error will be defined
54+
case error: req.reply({ statusCode: errorCaseStatusCode || 500 })
55+
break
56+
57+
// reply with a request body- default status code is 200
58+
case data: req.reply({ fixture: defaultFixture })
59+
break
60+
61+
// reply with the empty fixture is there is one, and the default as a backup. Allows us to isolate one api call at a time that may potentially respond with empty data.
62+
case !data: req.reply({ fixture: emptyFixture || defaultFixture })
63+
break
64+
65+
default: req.reply({ fixture: defaultFixture })
66+
break
67+
}
68+
}).as(alias || 'customIntercept')
3969
})

cypress/support/e2e.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
// Import commands.js using ES2015 syntax:
1717
import './commands'
1818

19-
// Alternatively you can use CommonJS syntax:
20-
// require('./commands')
19+
export const scientistApiBaseURL = `https://${Cypress.env('NEXT_PUBLIC_PROVIDER_NAME')}.scientist.com/api/v2`

0 commit comments

Comments
 (0)