Skip to content

Commit

Permalink
all 7 home specs are passing
Browse files Browse the repository at this point in the history
also further simplified the `customApiIntercept` function.
  • Loading branch information
alishaevn committed Feb 12, 2024
1 parent fd52f07 commit 808b838
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
41 changes: 13 additions & 28 deletions cypress/e2e/home.cy.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
describe('Navigating to the home page', () => {
// declare variables that can be used to change how the response is intercepted.
let data
let data = 'services/wares.json'
let error
let loading

beforeEach(() => {
cy.customApiIntercept({
action: 'GET',
alias: 'useAllWares',
data: 'services/wares.json',
data,
error,
loading,
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}`,
requestURL: '/wares.json?per_page=2000',
})

cy.visit('/')
Expand All @@ -27,20 +24,17 @@ describe('Navigating to the home page', () => {
context('able to navigate to "/browse"', () => {
const testSetup = ({ data, defaultFixture, requestURL }) => {
cy.customApiIntercept({
action: 'GET',
alias: 'useFilteredWares',
data,
defaultFixture,
emptyFixture: 'services/no-wares.json',
error,
requestURL,
})
}

it('with a blank query', () => {
testSetup({
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}&q=`,
data: true,
defaultFixture: 'services/wares.json',
data: 'services/wares.json',
requestURL: '/wares.json?per_page=2000&q=',
})

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

it('with a valid query term', () => {
testSetup({
requestURL: `/wares.json?per_page=${Cypress.env('API_PER_PAGE')}&q=${Cypress.env('CYPRESS_SEARCH_QUERY')}`,
data: true,
defaultFixture: 'services/filtered-wares.json',
data: 'services/filtered-wares.json',
requestURL: `/wares.json?per_page=2000&q=${Cypress.env('CYPRESS_SEARCH_QUERY')}`,
})

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

cy.get('input.search-bar').type(invalidQuery)
Expand All @@ -92,12 +86,10 @@ describe('Navigating to the home page', () => {
before(() => {
data = undefined
error = {
response: {
data: {
message: 'No access token provided.',
},
status: 403,
body: {
message: 'No access token provided.',
},
statusCode: 403,
}
})

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

context('which when returns no error or data', () => {
before(() => loading = true)

it('shows 3 placeholder cards loading', () => {
cy.get('p.placeholder-glow').should('have.length', 3).then(() => {
cy.log('Loading text displays correctly.')
Expand All @@ -120,11 +110,6 @@ describe('Navigating to the home page', () => {
})

context('which when returns data', () => {
before(() => {
featuredServices = true
error = false
})

it('shows the featured services cards', () => {
cy.get("div[data-cy='item-group']").should('exist').then(() => {
cy.log('Status bar renders successfully.')
Expand Down
27 changes: 17 additions & 10 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,27 @@ Cypress.Commands.add('login', (username, password) => {
})
})

// intercepts requests and creates potential cases for loading, error, data, and empty data
// required params are action, defaultFixture, requestURL
// 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
/**
* This command intercepts requests and returns the given stubbed response
*
* @param {string} alias - the alias to give the intercept (convention is to
* use the function name)
* @param {string} data - the fixture to return as the response data
* @param {object} error - the error object to return as the response error
* @param {string} requestURL - the URL to intercept
*
* @returns {object} - the stubbed response
*/
Cypress.Commands.add('customApiIntercept', ({
action, alias, data, error, loading, requestURL
alias, data, error, requestURL
}) => {
cy.intercept(action, `${scientistApiBaseURL}${requestURL}`, (req) => {
cy.intercept(`${scientistApiBaseURL}${requestURL}`, (req) => {
const response = {
data: { fixture: data },
error: { ...error },
loading: {},
data: data && { fixture: data },
error,
}
console.log({ response })

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

0 comments on commit 808b838

Please sign in to comment.