From 355947d24d269e80caf242b67ffb83480e3be88e Mon Sep 17 00:00:00 2001 From: spaenleh Date: Fri, 26 Jan 2024 15:51:53 +0100 Subject: [PATCH] fix: use custom interceptor for accessible search --- cypress/e2e/item/home/home.cy.ts | 43 +++++++++++++++++--------------- cypress/support/server.ts | 3 +-- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/cypress/e2e/item/home/home.cy.ts b/cypress/e2e/item/home/home.cy.ts index 1cea9e343..2dd1325a1 100644 --- a/cypress/e2e/item/home/home.cy.ts +++ b/cypress/e2e/item/home/home.cy.ts @@ -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, @@ -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(); @@ -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); }); }); @@ -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(); @@ -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'); + }); }); }); diff --git a/cypress/support/server.ts b/cypress/support/server.ts index 2a9614779..4e2cbe399 100644 --- a/cypress/support/server.ts +++ b/cypress/support/server.ts @@ -99,7 +99,6 @@ const { buildPostShortLinkRoute, buildPatchShortLinkRoute, buildDeleteShortLinkRoute, - buildGetAccessibleItems, } = API_ROUTES; const API_HOST = Cypress.env('API_HOST'); @@ -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;