Skip to content

Commit 808b838

Browse files
committed
all 7 home specs are passing
also further simplified the `customApiIntercept` function.
1 parent fd52f07 commit 808b838

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

cypress/e2e/home.cy.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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
3+
let data = 'services/wares.json'
44
let error
5-
let loading
65

76
beforeEach(() => {
87
cy.customApiIntercept({
9-
action: 'GET',
108
alias: 'useAllWares',
11-
data: 'services/wares.json',
9+
data,
1210
error,
13-
loading,
14-
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}`,
11+
requestURL: '/wares.json?per_page=2000',
1512
})
1613

1714
cy.visit('/')
@@ -27,20 +24,17 @@ describe('Navigating to the home page', () => {
2724
context('able to navigate to "/browse"', () => {
2825
const testSetup = ({ data, defaultFixture, requestURL }) => {
2926
cy.customApiIntercept({
30-
action: 'GET',
3127
alias: 'useFilteredWares',
3228
data,
33-
defaultFixture,
34-
emptyFixture: 'services/no-wares.json',
29+
error,
3530
requestURL,
3631
})
3732
}
3833

3934
it('with a blank query', () => {
4035
testSetup({
41-
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}&q=`,
42-
data: true,
43-
defaultFixture: 'services/wares.json',
36+
data: 'services/wares.json',
37+
requestURL: '/wares.json?per_page=2000&q=',
4438
})
4539

4640
cy.get('button.search-button').click()
@@ -52,9 +46,8 @@ describe('Navigating to the home page', () => {
5246

5347
it('with a valid query term', () => {
5448
testSetup({
55-
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}&q=${Cypress.env('CYPRESS_SEARCH_QUERY')}`,
56-
data: true,
57-
defaultFixture: 'services/filtered-wares.json',
49+
data: 'services/filtered-wares.json',
50+
requestURL: `/wares.json?per_page=2000&q=${Cypress.env('CYPRESS_SEARCH_QUERY')}`,
5851
})
5952

6053
cy.get('input.search-bar').type(Cypress.env('CYPRESS_SEARCH_QUERY'))
@@ -67,7 +60,8 @@ describe('Navigating to the home page', () => {
6760
it('with an invalid query term', () => {
6861
const invalidQuery = 'asdfghjk'
6962
testSetup({
70-
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}&q=${invalidQuery}`,
63+
data: 'services/no-wares.json',
64+
requestURL: `/wares.json?per_page=2000&q=${invalidQuery}`,
7165
})
7266

7367
cy.get('input.search-bar').type(invalidQuery)
@@ -92,12 +86,10 @@ describe('Navigating to the home page', () => {
9286
before(() => {
9387
data = undefined
9488
error = {
95-
response: {
96-
data: {
97-
message: 'No access token provided.',
98-
},
99-
status: 403,
89+
body: {
90+
message: 'No access token provided.',
10091
},
92+
statusCode: 403,
10193
}
10294
})
10395

@@ -110,8 +102,6 @@ describe('Navigating to the home page', () => {
110102
})
111103

112104
context('which when returns no error or data', () => {
113-
before(() => loading = true)
114-
115105
it('shows 3 placeholder cards loading', () => {
116106
cy.get('p.placeholder-glow').should('have.length', 3).then(() => {
117107
cy.log('Loading text displays correctly.')
@@ -120,11 +110,6 @@ describe('Navigating to the home page', () => {
120110
})
121111

122112
context('which when returns data', () => {
123-
before(() => {
124-
featuredServices = true
125-
error = false
126-
})
127-
128113
it('shows the featured services cards', () => {
129114
cy.get("div[data-cy='item-group']").should('exist').then(() => {
130115
cy.log('Status bar renders successfully.')

cypress/support/commands.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,27 @@ Cypress.Commands.add('login', (username, password) => {
3939
})
4040
})
4141

42-
// intercepts requests and creates potential cases for loading, error, data, and empty data
43-
// required params are action, defaultFixture, requestURL
44-
// 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
42+
/**
43+
* This command intercepts requests and returns the given stubbed response
44+
*
45+
* @param {string} alias - the alias to give the intercept (convention is to
46+
* use the function name)
47+
* @param {string} data - the fixture to return as the response data
48+
* @param {object} error - the error object to return as the response error
49+
* @param {string} requestURL - the URL to intercept
50+
*
51+
* @returns {object} - the stubbed response
52+
*/
4553
Cypress.Commands.add('customApiIntercept', ({
46-
action, alias, data, error, loading, requestURL
54+
alias, data, error, requestURL
4755
}) => {
48-
cy.intercept(action, `${scientistApiBaseURL}${requestURL}`, (req) => {
56+
cy.intercept(`${scientistApiBaseURL}${requestURL}`, (req) => {
4957
const response = {
50-
data: { fixture: data },
51-
error: { ...error },
52-
loading: {},
58+
data: data && { fixture: data },
59+
error,
5360
}
54-
console.log({ response })
5561

56-
return req.reply(response[data || error || loading])
62+
// falling back to an empty object mimics the loading state
63+
return req.reply(response.data || response.error || {})
5764
}).as(alias || 'customIntercept')
5865
})

0 commit comments

Comments
 (0)