Skip to content

Commit d509c4e

Browse files
authored
Merge pull request #271 from scientist-softserv/251-browse-tests
Browse tests
2 parents 6a6be65 + 223b9e3 commit d509c4e

File tree

3 files changed

+142
-70
lines changed

3 files changed

+142
-70
lines changed

cypress/e2e/browse.cy.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
describe('Browsing', () => {
2+
let wares
3+
let loading
4+
let error
5+
let intercepts = [
6+
{
7+
alias: 'useFilteredWares',
8+
},
9+
{
10+
alias: 'useFilteredWares - blank search',
11+
},
12+
{
13+
alias: 'useFilteredWares - with results',
14+
query: 'test',
15+
},
16+
{
17+
alias: 'useFilteredWares - no results',
18+
query: 'asdfghjk',
19+
emptyFixture: 'services/no-wares.json',
20+
},
21+
]
22+
23+
beforeEach(() => {
24+
// Intercept the responses from the endpoint to view all requests.
25+
// Even though this is to the same endpoint, the call happens on each page twice,
26+
// once when the page loads with all the wares, and again after any search is performed.
27+
// this makes it necessary to create an intercept for each time the call is made.
28+
intercepts.forEach((intercept) => {
29+
cy.customApiIntercept({
30+
action: 'GET',
31+
alias: intercept.alias,
32+
requestURL: `/providers/${Cypress.env('NEXT_PUBLIC_PROVIDER_ID')}/wares.json?q=${intercept.query || ''}`,
33+
data: wares,
34+
defaultFixture: 'services/wares.json',
35+
emptyFixture: intercept.emptyFixture || '',
36+
loading,
37+
error
38+
})
39+
})
40+
})
41+
42+
describe('from the browse page', () => {
43+
beforeEach(() => {
44+
cy.visit('/browse')
45+
})
46+
47+
context('browse page is loading', () => {
48+
before(() => {
49+
loading = true
50+
})
51+
it('should show loading components.', () => {
52+
cy.get(".card-body[data-cy='item-loading']").should('be.visible').then(() => {
53+
cy.log('Loading components display correctly.')
54+
})
55+
})
56+
})
57+
58+
context('error while making a request to the api', () => {
59+
before(() => {
60+
loading = false
61+
error = true
62+
})
63+
it('should show an error message.', () => {
64+
cy.get("div[role='alert']").should('be.visible').then(() => {
65+
cy.log('Successfully hits an error.')
66+
})
67+
})
68+
})
69+
70+
// filtering by query is handled in the api and not the webstore
71+
// since we are stubbing that response, our tests will not return actual filtered wares based on on test query.
72+
// the purpose of these tests is just to show that the correct components appear in the UI in each case.
73+
context('a search is completed successfully and', () => {
74+
before(() => {
75+
wares = true
76+
error = false
77+
})
78+
it('has a blank query', () => {
79+
cy.get('button.search-button').click()
80+
cy.url().should('include', '/browse')
81+
cy.url().should('not.include', '?')
82+
cy.get('input.search-bar').should('have.value', '')
83+
cy.get(".card[data-cy='item-card']").should('be.visible')
84+
})
85+
86+
it('has a query term', () => {
87+
cy.get('input.search-bar').type('test')
88+
cy.get('button.search-button').click()
89+
cy.url().should('include', '/browse?q=test')
90+
cy.get('input.search-bar').should('have.value', 'test')
91+
cy.get(".card[data-cy='item-card']").should('be.visible')
92+
})
93+
94+
before(() => {
95+
wares = false
96+
})
97+
it('has a query term, but that term has no results', () => {
98+
cy.get('input.search-bar').type('asdfghjk')
99+
cy.get('button.search-button').click()
100+
cy.url().should('include', '/browse?q=asdfghjk')
101+
cy.get('input.search-bar').should('have.value', 'asdfghjk')
102+
cy.get("p[data-cy='no-results']").should('contain', 'Your search for asdfghjk returned no results')
103+
})
104+
})
105+
})
106+
107+
describe('from the home page', () => {
108+
beforeEach(() => {
109+
wares = true
110+
// Intercept the api call being made on the homepage
111+
cy.customApiIntercept({
112+
action: 'GET',
113+
alias: 'useAllWares',
114+
requestURL: `/providers/${Cypress.env('NEXT_PUBLIC_PROVIDER_ID')}/wares.json`,
115+
data: wares,
116+
defaultFixture: 'services/wares.json',
117+
loading,
118+
error
119+
})
120+
cy.visit('/')
121+
})
122+
123+
context('a search is completed successfully and', () => {
124+
it('navigates to "/browse" with a blank query', () => {
125+
cy.get('button.search-button').click()
126+
cy.url().should('include', '/browse')
127+
cy.url().should('not.include', '?')
128+
cy.get('input.search-bar').should('have.value', '')
129+
cy.get(".card[data-cy='item-card']").should('be.visible')
130+
})
131+
132+
it('navigates to "/browse" with a query term', () => {
133+
cy.get('input.search-bar').type('test')
134+
cy.get('button.search-button').click()
135+
cy.url().should('include', '/browse?q=test')
136+
cy.get('input.search-bar').should('have.value', 'test')
137+
cy.get(".card[data-cy='item-card']").should('be.visible')
138+
})
139+
})
140+
})
141+
})

cypress/e2e/browsing.cy.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

pages/browse/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const Browse = () => {
7676
))}
7777
</>
7878
) : (
79-
<p>
79+
<p data-cy='no-results'>
8080
Your search for <b>{query}</b> returned no results. Please try another search term.
8181
</p>
8282
)}

0 commit comments

Comments
 (0)