Skip to content

Commit

Permalink
fix: use custom interceptor for accessible search
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Jan 26, 2024
1 parent 35cd104 commit 355947d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
43 changes: 23 additions & 20 deletions cypress/e2e/item/home/home.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import i18n, { BUILDER_NAMESPACE } from '../../../../src/config/i18n';
import { HOME_PATH } from '../../../../src/config/paths';
import { HOME_PATH, ITEMS_PATH } from '../../../../src/config/paths';
import {
ACCESSIBLE_ITEMS_NEXT_PAGE_BUTTON_SELECTOR,
ACCESSIBLE_ITEMS_ONLY_ME_ID,
Expand Down Expand Up @@ -65,8 +65,14 @@ describe('Home', () => {
);
});

it('Search on second page should reset page number', () => {
it.only('Search on second page should reset page number', () => {
const searchText = 'mysearch';
// register a custom one time interceptor to listen specifically
// to the request made with the search parameter we want
cy.intercept({
pathname: `/${ITEMS_PATH}/accessible`,
query: { name: searchText },
}).as('getAccessibleSearch');
cy.wait('@getAccessibleItems');
// navigate to seconde page
cy.get(`#${ITEMS_GRID_PAGINATION_ID} > ul > li`).eq(2).click();
Expand All @@ -76,13 +82,13 @@ describe('Home', () => {
});
cy.get(`#${ITEM_SEARCH_INPUT_ID}`).type(searchText);

cy.wait('@getAccessibleItems')
.its('request.url')
.should('contain', 'page=1');
// using our custom interceptor with the search parameter we can distinguish the complete
// search request from possibly other incomplete search requests
cy.wait('@getAccessibleSearch').then(({ request: { query } }) => {
expect(query.name).to.eq(searchText);
expect(query.page).to.eq('1');
});
cy.get(`#${buildItemCard(sampleItems[0].id)}`).should('be.visible');
cy.wait('@getAccessibleItems')
.its('request.url')
.should('contain', searchText);
});
});

Expand Down Expand Up @@ -272,8 +278,12 @@ describe('Home', () => {
);
});

it('Search on second page should reset page number', () => {
it.only('Search on second page should reset page number', () => {
const searchText = 'mysearch';
cy.intercept({
pathname: `/${ITEMS_PATH}/accessible`,
query: { name: searchText },
}).as('getAccessibleSearch');
cy.wait('@getAccessibleItems');
// navigate to second page
cy.get(ACCESSIBLE_ITEMS_NEXT_PAGE_BUTTON_SELECTOR).click();
Expand All @@ -283,17 +293,10 @@ describe('Home', () => {
});
cy.get(`#${ITEM_SEARCH_INPUT_ID}`).type(searchText);

cy.wait(['@getAccessibleItems', '@getAccessibleItems']).then(
([
_unused,
{
request: { url },
},
]) => {
expect(url).to.contain(searchText);
expect(url).to.contain('page=1');
},
);
cy.wait('@getAccessibleSearch').then(({ request: { query } }) => {
expect(query.name).to.eq(searchText);
expect(query.page).to.eq('1');
});
});
});

Expand Down
3 changes: 1 addition & 2 deletions cypress/support/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const {
buildPostShortLinkRoute,
buildPatchShortLinkRoute,
buildDeleteShortLinkRoute,
buildGetAccessibleItems,
} = API_ROUTES;

const API_HOST = Cypress.env('API_HOST');
Expand Down Expand Up @@ -177,7 +176,7 @@ export const mockGetAccessibleItems = (items: ItemForTest[]): void => {
cy.intercept(
{
method: HttpMethod.GET,
url: new RegExp(`${API_HOST}/${buildGetAccessibleItems({}, {})}`),
pathname: `/${ITEMS_ROUTE}/accessible`,
},
({ url, reply }) => {
const params = new URL(url).searchParams;
Expand Down

0 comments on commit 355947d

Please sign in to comment.