Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

239 requests page tests #241

Merged
merged 7 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 71 additions & 19 deletions cypress/e2e/requests.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,102 @@ describe('Viewing all requests', () => {
describe('as a logged out user', () => {
it('should show an error message.', () => {
// Visit a protected route in order to allow cypress to set the cookie and mock the login
cy.visit("/requests")
cy.visit('/requests')
cy.get('div.alert-heading').contains('Unauthorized').then(() => {
cy.log("A logged out user is not able to view requests.")
cy.log('A logged out user is not able to view requests.')
})
})
})

describe('as a logged in user', () => {
let scientistApiBaseURL = `https://${Cypress.env('NEXT_PUBLIC_PROVIDER_NAME')}.scientist.com/api/v2`
// declare variables that can be used to change how the response is intercepted.
let requestList
let loading
let error

beforeEach(() => {
// Call the custom cypress command to log in
cy.login(Cypress.env('TEST_SCIENTIST_USER'), Cypress.env('TEST_SCIENTIST_PW'))

// Intercept the response from the endpoint to view all requests
// TODO(summer-cook): extract out this base url into the config to use as an environment variable. it was not cooperating before
cy.intercept('GET', `${scientistApiBaseURL}/quote_groups/mine.json`, (req) => {
if (requestList === true) {
req.reply({fixture: 'all-requests/requests.json'})
} else {
req.reply({fixture: 'all-requests/no-requests.json'})
if ((requestList === undefined) && (loading === true)) {
// reply with an empty response: both data and error will be undefined.
req.reply()
} else if ((requestList === undefined) && (loading === undefined) && (error === true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does loading equal undefined instead of false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these variables are doing are switching between the different intercept cases, so it could be either undefined or false, it wouldn't really matter as long as its not true. But, it does make it easier to read to be false so i just changed it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.
yes, the test passes either way but I was thinking from the POV of someone who is unfamiliar with this code. just like we check the api specs to see how things work, someone may check these specs. so it's better, imo, to keep the tests in line with what the actual pages are demonstrating.

// error will be defined
req.reply({ statusCode: 404 })
} else if (requestList === true) {
// reply with a request body- default status code is 200
req.reply({ fixture: 'all-requests/requests.json' })
} else if (requestList === false) {
req.reply({ fixture: 'all-requests/no-requests.json' })
}
}).as('useAllRequests')
cy.visit("/requests")
})
// Intercept the response from the endpoint that gets the default ware ID
cy.intercept('GET', `${scientistApiBaseURL}/wares.json?q=make-a-request`, { fixture: 'all-requests/make-a-request.json' })
cy.visit('/requests')
})

context('has requests', () => {

context('request list is loading', () => {
before(() => {
requestList = true
loading = true
})
it("shows the user's request list.", () => {
cy.get('article.request-item').should('exist').then(() => {
cy.log('Successfully viewing request list.')
it('should show a loading spinner.', () => {
cy.get("[aria-label='tail-spin-loading']").should('be.visible').then(() => {
cy.log('Loading spinner displays correctly.')
})
})
})

context('has 0 requests', () => {
context('error while making a request to the api', () => {
before(() => {
requestList = false
requestList = undefined
loading = undefined
error = true
})
it('should show an error message.', () => {
cy.get("div[role='alert']").should('be.visible').then(() => {
cy.log('Successfully hits an error.')
})
})
it("shows a message notifying the user they don't have any requests.", () => {
cy.get('p.no-requests').contains('You do not have any requests yet.').then(() => {
cy.log('Successfully viewing request page with no requests.')
})

describe('request components are loading successfully, &', () => {
context('the user has requests', () => {
before(() => {
requestList = true
})
it("should show the user's request list.", () => {
cy.get('article.request-item').should('exist').then(() => {
cy.log('Successfully viewing request list.')
})
})
})

context('the user has 0 requests', () => {
before(() => {
requestList = false
})
it("should show a message notifying the user they don't have any requests.", () => {
cy.get('p.no-requests').contains('You do not have any requests yet.').then(() => {
cy.log('Successfully viewing request page with no requests.')
})
})
})

context('the user can see the <LinkedButton /> component', () => {
[true, false].forEach( (value)=>{
before(() => {
requestList = value
})
it(`should show a button that links to the initialize request page for the default ware ${value ? 'with a request list' : 'with 0 requests'}.`, () => {
cy.get("a[data-cy='linked-button']").should('have.attr', 'href', `/requests/new/make-a-request?id=123`).then(() => {
cy.log('The <LinkedButton /> component displays correctly')
})
})
})
})
})
Expand Down
7 changes: 7 additions & 0 deletions cypress/fixtures/all-requests/make-a-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ware_refs": [
{
"id": 123
}
]
}
2 changes: 1 addition & 1 deletion cypress/fixtures/all-requests/requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"uuid": "596127b7-2356-45aa-aec4-a4f8608ae755",
"name": "Example Request 3",
"description": "<p>Here is a 3rd request</p>",
"status": "Compliance Required",
"status": "SOW Submitted",
"billing_same_as_shipping": false,
"proposed_deadline": null,
"updated_at": "2023-02-23T07:12:44.086Z",
Expand Down
2 changes: 1 addition & 1 deletion utils/api/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const configureRequests = ({ data, path }) => {
export const configureErrors = (errors) => {
const env = process.env.NODE_ENV
const remainingErrors = errors
.filter(error => Object.keys(error).length)
.filter(error => error && Object.keys(error).length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(error => error && Object.keys(error).length)
.filter(error => Object.keys(error).length)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have to explicitly check for the truthiness of an error here because Object.keys is handling it.

[{}, false, { a: 'a' }].filter(error => Object.keys(error).length)
=> Array [ { a: "a" } ]

Copy link
Contributor Author

@summer-cook summer-cook Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what if the array ends up with an item that is undefined? like
[{}, false, { a: 'a' }, undefined]

That is what I was running into in the test that prompted me to change this. idk if its possible for this array of errors to contain one that is undefined, but it was happening in the test. so I figured adding a check there to make sure the error was not undefined was basically some TDD.

Copy link
Contributor Author

@summer-cook summer-cook Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when an error was undefined, Object.keys(error) was v angry

.map(error => ({
...error,
message: `${error.message} (${error.response?.data?.message})`,
Expand Down