Skip to content

Commit fd52f07

Browse files
committed
correctly accounting for errors on the home page spec
we're testing for an invalid access token now. the `customApiIntercept` command is also simplified.
1 parent 6113560 commit fd52f07

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

cypress/e2e/home.cy.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
describe('Navigating to the home page', () => {
22
// declare variables that can be used to change how the response is intercepted.
3+
let data
34
let error
4-
let featuredServices
55
let loading
66

77
beforeEach(() => {
88
cy.customApiIntercept({
99
action: 'GET',
1010
alias: 'useAllWares',
11-
data: featuredServices,
12-
defaultFixture: 'services/wares.json',
13-
emptyFixture: 'services/no-wares.json',
11+
data: 'services/wares.json',
1412
error,
1513
loading,
1614
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}`,
@@ -90,17 +88,24 @@ describe('Navigating to the home page', () => {
9088
})
9189

9290
describe('makes a call to the api', () => {
93-
context('which when returns an error', () => {
91+
context('which when given an invalid access token', () => {
9492
before(() => {
95-
loading = false
96-
error = true
93+
data = undefined
94+
error = {
95+
response: {
96+
data: {
97+
message: 'No access token provided.',
98+
},
99+
status: 403,
100+
},
101+
}
97102
})
98103

99104
it('shows an error message', () => {
100-
// why would we get an error?
101105
cy.get("div[role='alert']").should('be.visible').then(() => {
102106
cy.log('Successfully hits an error.')
103107
})
108+
cy.get("div[role='alert']").contains('No access token provided.')
104109
})
105110
})
106111

cypress/support/commands.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,29 @@ import { scientistApiBaseURL } from './e2e'
3030
// source: https://github.com/nextauthjs/next-auth/discussions/2053#discussioncomment-1191016
3131
Cypress.Commands.add('login', (username, password) => {
3232
cy.session([username, password], () => {
33-
cy.intercept("/api/auth/session", { fixture: "session.json" }).as("session");
33+
cy.intercept('/api/auth/session', { fixture: 'session.json' }).as('session')
3434

35-
// Set the cookie for cypress.
36-
// It has to be a valid cookie so next-auth can decrypt it and confirm its validity.
37-
// This cookie also may need to be refreshed intermittently if it expires
38-
cy.setCookie("next-auth.session-token", Cypress.env('TEST_SESSION_COOKIE'));
35+
// Set the cookie for cypress.
36+
// It has to be a valid cookie so next-auth can decrypt it and confirm its validity.
37+
// This cookie also may need to be refreshed intermittently if it expires
38+
cy.setCookie('next-auth.session-token', Cypress.env('TEST_SESSION_COOKIE'))
3939
})
4040
})
4141

4242
// intercepts requests and creates potential cases for loading, error, data, and empty data
4343
// required params are action, defaultFixture, requestURL
4444
// 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
4545
Cypress.Commands.add('customApiIntercept', ({
46-
action, alias, data, defaultFixture, emptyFixture, error, errorCaseStatusCode, loading, requestURL
46+
action, alias, data, error, loading, requestURL
4747
}) => {
4848
cy.intercept(action, `${scientistApiBaseURL}${requestURL}`, (req) => {
49-
switch (true) {
50-
// reply with an empty response: both data and error will be undefined.
51-
case loading: req.reply({})
52-
break
53-
54-
// error will be defined
55-
case error: req.reply({ statusCode: errorCaseStatusCode || 500 })
56-
break
57-
58-
// reply with a request body- default status code is 200
59-
case data: req.reply({ fixture: defaultFixture })
60-
break
61-
62-
// 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.
63-
case !data: req.reply({ fixture: emptyFixture || defaultFixture })
64-
break
65-
66-
default: req.reply({ fixture: defaultFixture })
67-
break
49+
const response = {
50+
data: { fixture: data },
51+
error: { ...error },
52+
loading: {},
6853
}
54+
console.log({ response })
55+
56+
return req.reply(response[data || error || loading])
6957
}).as(alias || 'customIntercept')
7058
})

0 commit comments

Comments
 (0)