Skip to content

Commit

Permalink
fix: Load product list from transfer state in SSR (#18009)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyber authored Nov 13, 2023
1 parent 196305d commit d0bebdf
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <spartacus-team@sap.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

import { SSR_E2E_PLP_SCENARIOS } from '../../helpers/ssr/product-listing-page';

const SEARCH_REQUEST_URL = '**/products/search?**';
const scenarios = SSR_E2E_PLP_SCENARIOS;

describe('SSR - Product Listing Page', () => {
/**
* This tests all the scenarios where SSR should render the page but the search request is
* only made on the initial page load. The server should have rendered the page and return
* the cached page that does NOT need to request the search api again on reload.
*
* Note: When in development, restarting the dev ssr server (npm run dev:ssr) may be required
* to clear the rendering cache.
*/
describe('search request should only be made once and NOT on page reload', () => {
for (let scenario of scenarios) {
// Skip is used in case of going back to a page that would be already cached
// since another search request would NOT be made.
if (!scenario.skipReloadTest) {
it(scenario.case, () => {
cy.intercept(SEARCH_REQUEST_URL).as('search-init');
cy.visit(scenario.url);
cy.wait('@search-init');

cy.intercept(SEARCH_REQUEST_URL, cy.spy().as('search-2nd'));
cy.reload();
cy.get('cx-product-list');
cy.get('@search-2nd').should('not.have.been.called');
});
}
}
});

/**
* This tests that navigation has not broken when navigating options such as paginations and sorts.
*/
describe(
'should be able to navigate through all scenarios and trigger requests for each case',
{ testIsolation: false },
() => {
for (let i = 0; i < scenarios.length; i++) {
const scenario = scenarios[i];
const previous = scenarios[i - 1];
it(scenario.case, () => {
// Visit whenever no next step from previous scenario to begin with new search type.
if (!previous?.navigateToNext) {
cy.visit(scenarios[i].url);
}

cy.get('cx-product-list');
cy.url().should('contain', scenario.url);

// Make sure navigation has happened successfully by checking a search request was made.
if (scenario.navigateToNext) {
cy.intercept(SEARCH_REQUEST_URL).as('search');
scenario.navigateToNext();
cy.wait('@search');
}
});
}
}
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <spartacus-team@sap.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

const CASE_TITLES = {
ALL_BRANDS: 'All Brands',
QUERY_GRIP: 'Search Query "grip"',
DIGITAL_CAMERAS: 'Digital Cameras',
};

const CASE_URLS = {
ALL_BRANDS: '/Brands/all/c/brands',
QUERY_GRIP: '/search/grip',
DIGITAL_CAMERAS: '/Open-Catalogue/Cameras/Digital-Cameras/c/575',
};

const CASE_QUERY_PARTS = {
ALL_BRANDS: '?query=:topRated:allCategories:brands',
QUERY_GRIP: '?query=grip:topRated',
DIGITAL_CAMERAS: '?query=:topRated:allCategories:575',
};

function getStandardCases(key: string) {
return [
{
case: CASE_TITLES[key],
url: CASE_URLS[key],
navigateToNext: () => {
cy.get('cx-pagination a.page[aria-label="page 2"]').first().click();
},
},
{
case: CASE_TITLES[key] + ' (2nd page)',
url: CASE_URLS[key] + '?currentPage=1',
navigateToNext: () => {
cy.get('cx-pagination a.start[aria-label="first page"]')
.first()
.click();
},
},
{
case: CASE_TITLES[key] + ' (back to first page)',
url: CASE_URLS[key],
navigateToNext: () => {
cy.get('cx-sorting .ng-select').first().ngSelect('Top Rated');
},
skipReloadTest: true,
},
{
case: CASE_TITLES[key] + ' (with sort)',
url: CASE_URLS[key] + '?sortCode=topRated',
navigateToNext: () => {
cy.get('cx-pagination a.page[aria-label="page 2"]').first().click();
},
},
{
case: CASE_TITLES[key] + ' (2nd page with sort)',
url: CASE_URLS[key] + '?sortCode=topRated&currentPage=1',
navigateToNext: () => {
cy.get('cx-facet a').contains('Chiba').click();
},
},
{
case: CASE_TITLES[key] + ' (with query and sort)',
url: CASE_URLS[key] + CASE_QUERY_PARTS[key] + ':availableInStores:Chiba',
navigateToNext: () => {
cy.get('cx-pagination a.page[aria-label="page 2"]').first().click();
},
},
{
case: CASE_TITLES[key] + ' (2nd page with query and sort)',
url:
CASE_URLS[key] +
CASE_QUERY_PARTS[key] +
':availableInStores:Chiba&currentPage=1',
navigateToNext: () => {
cy.get('cx-sorting .ng-select').first().ngSelect('Relevance');
},
},
{
case: CASE_TITLES[key] + ' (with query changing sort to default)',
url:
CASE_URLS[key] +
CASE_QUERY_PARTS[key] +
':availableInStores:Chiba&currentPage=1&sortCode=relevance',
},
];
}

export const SSR_E2E_PLP_SCENARIOS = [
...getStandardCases('ALL_BRANDS'),
...getStandardCases('QUERY_GRIP'),
...getStandardCases('DIGITAL_CAMERAS'),
];
2 changes: 1 addition & 1 deletion projects/storefrontapp-e2e-cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"cy:run:ci:ccv2-b2b": "cypress run --config-file cypress.config.ci.ts --config baseUrl=$ENDPOINT_URL_PUBLIC_SPARTACUS --reporter junit --reporter-options mochaFile=results/spartacus-test-results-[hash].xml --env API_URL=$ENDPOINT_URL_PUBLIC_API,BASE_SITE=powertools-spa,OCC_PREFIX_USER_ENDPOINT=orgUsers --spec \"cypress/e2e/cx_ccv2/regression/b2b/**/*.e2e.cy.ts\"",
"cy:run:ci:ccv2-product-configurator": "cypress run --config-file cypress.config.ci.ts --config baseUrl=$ENDPOINT_URL_PUBLIC_SPARTACUS --reporter junit --reporter-options mochaFile=results/spartacus-test-results-[hash].xml --env API_URL=$ENDPOINT_URL_PUBLIC_API,BASE_SITE=$E2E_BASE_SITE,OCC_PREFIX_USER_ENDPOINT=orgUsers --spec $E2ES_TO_RUN",
"cy:run:ci:mcs": "cypress run --config-file cypress.config.ci.ts --config baseUrl=$ENDPOINT_URL_PUBLIC_SPARTACUS --reporter junit --reporter-options mochaFile=results/spartacus-test-results-[hash].xml --env API_URL=$ENDPOINT_URL_PUBLIC_API --spec \"cypress/e2e/cx_mcs/regression/b2c/**/*e2e.cy.ts\"",
"cy:run:ci:ssr": "cypress run --config-file cypress.config.ci.ts --config baseUrl=http://localhost:4000 --record --key $CYPRESS_KEY --tag \"ssr,all\" --parallel --ci-build-id $BUILD_NUMBER --group SSR --spec \"cypress/e2e/ssr/pages.core-e2e.cy.ts\" --reporter junit --reporter-options mochaFile=results/e2e-test-ssr-[hash].xml",
"cy:run:ci:ssr": "cypress run --config-file cypress.config.ci.ts --config baseUrl=http://localhost:4000 --record --key $CYPRESS_KEY --tag \"ssr,all\" --parallel --ci-build-id $BUILD_NUMBER --group SSR --spec \"cypress/e2e/ssr/*.core-e2e.cy.ts\" --reporter junit --reporter-options mochaFile=results/e2e-test-ssr-[hash].xml",
"cy:run:ci:cds": "cypress run --config-file cypress.config.ci.ts --record --key $CYPRESS_KEY --tag \"2011,b2c,all-cds\" --group CDS --spec \"cypress/e2e/vendor/cds/**/*.core-e2e.cy.ts\"",
"cy:run:ci:cdc": "cypress run --config-file cypress.config.ci.ts --env API_URL=https://api.cg79x9wuu9-eccommerc1-s1-public.model-t.myhybris.cloud/ --record --key $CYPRESS_KEY --tag \"2005,cdc\" --group CDC --spec \"cypress/e2e/vendor/cdc/b2c/*.e2e.cy.ts\"",
"cy:run:ci:cdc-b2b": "cypress run --config-file cypress.ci.json --env BASE_SITE=powertools-spa,OCC_PREFIX_USER_ENDPOINT=orgUsers,API_URL=https://api.cg79x9wuu9-eccommerc1-s1-public.model-t.myhybris.cloud/ --record --key $CYPRESS_KEY --tag \"2211,cdc\" --group CDC --spec \"cypress/integration/vendor/cdc/b2b/*.e2e.cy.ts\"",
Expand Down
Loading

0 comments on commit d0bebdf

Please sign in to comment.