From db24e73157130b6ea38d091078b9d678651d3938 Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:09:29 +0000 Subject: [PATCH 1/7] tests: Split up unit tests by page/section --- contentful/export-processor/data-mapper.js | 4 + .../e2e/dynamic-page-validator-readme.md | 8 +- .../cypress/e2e/dynamic-page-validator.cy.js | 119 ++++++++---------- .../cypress/helpers/contentful-data-loader.js | 10 ++ .../cypress/{e2e => helpers}/contentful.js | 0 5 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful-data-loader.js rename tests/Dfe.PlanTech.Web.E2ETests/cypress/{e2e => helpers}/contentful.js (100%) diff --git a/contentful/export-processor/data-mapper.js b/contentful/export-processor/data-mapper.js index ef4fc2563..59a5a5a47 100644 --- a/contentful/export-processor/data-mapper.js +++ b/contentful/export-processor/data-mapper.js @@ -110,6 +110,10 @@ export default class DataMapper { * @returns {IterableIterator
} Iterator for mapped sections */ *sectionsToClasses(sections) { + if (!sections || sections.length == 0) { + return; + } + for (const [id, section] of sections) { yield new Section(section); } diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator-readme.md b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator-readme.md index 7f5c1ce20..c40c9ad9f 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator-readme.md +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator-readme.md @@ -5,9 +5,11 @@ 1. Extract Contentful content using CLI 2. Rename file to `contentful.js`, and make the JSON an object called Contentful. E.g. `const contentful = { ... JSON}`; 3. Export that object -4. Place the `contentful.js` in the same folder as `dynamic-page-validator.cy.js` +4. Place the `contentful.js` in `helpers` folder in the parent folder. E.g. `Dfe.PlanTech.Web.E2ETests\cypress\helpers\contentful.js` 5. Remove the `skip` command on line 14. +_Note: Will look into automating the Contentful export for ease_ + ### Components content validated - [x] Answer @@ -43,8 +45,8 @@ ### Other to do -- [ ] Do not fail if one error; run entire tests for issues. -- [ ] Split tests up; not just one `it` function +- [x] Do not fail if one error; run entire tests for issues. +- [x] Split tests up; not just one `it` function - [ ] Complete documentation: - [ ] Merge with documentation in `contentful-helpers` - [x] Make a Node project for the `contentful-helpers` to allow re-use, and prevent the fact that the code is copied and pasted into two places diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index 280aca1ea..7c6b1b50b 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -1,61 +1,33 @@ -import DataMapper from "../../../../contentful/export-processor/data-mapper"; -import { contentful } from "./contentful"; +import ContentfulData from "../helpers/contentful-data-loader"; import ValidatePage from "../helpers/content-validators/page-validator"; import { selfAssessmentSlug } from "../helpers/page-slugs"; import ValidateContent from "../helpers/content-validators/content-validator"; describe("Pages should have content", () => { - let dataMapper; + before(() => {}); - before(() => { - if (contentful && contentful.entries && contentful.entries.length > 0) { - dataMapper = new DataMapper(contentful); - } - }); - - it.skip("Should render navigation links", () => { - if (dataMapper?.pages == null) { - console.log("Datamapper has not processed data correctly"); + it("Should render navigation links", () => { + const navigationLinks = ContentfulData.contents["navigationLink"]; + if (!dataLoaded(navigationLinks)) { return; } - const navigationLinks = dataMapper.contents["navigationLink"]; + cy.visit("/"); - const indexPage = cy.visit("/"); - - for (const [id, navigationLink] of navigationLinks) { + for (const [_, navigationLink] of navigationLinks) { ValidateContent(navigationLink); } }); - it.skip("Should work for unauthorised pages", () => { - if (dataMapper?.pages == null) { - console.log("Datamapper has not processed data correctly"); - return; - } - - for (const [_, page] of dataMapper.pages) { - if (page.fields.requiresAuthorisation) { - continue; - } - - const slug = `/${page.fields.slug.replace("/", "")}`; - cy.visit(slug); - ValidatePage(slug, page); - } - }); - - it.skip("Should validate self-assessment page", () => { - if (dataMapper.pages == null) { - console.log("Datamapper has not processed data correctly"); + it("Should validate self-assessment page", () => { + if (!dataLoaded(ContentfulData.pages)) { return; } - cy.loginWithEnv(`${selfAssessmentSlug}`); const slug = selfAssessmentSlug.replace("/", ""); const selfAssessmentPage = FindPageForSlug({ slug, - dataMapper, + dataMapper: ContentfulData, }); if (!selfAssessmentPage) { @@ -67,47 +39,61 @@ describe("Pages should have content", () => { ValidatePage(slug, selfAssessmentPage); }); - it.skip("Should navigate through every question", () => { - if (dataMapper.pages == null) { - console.log("Datamapper has not processed data correctly"); - return; - } - - cy.loginWithEnv(`${selfAssessmentSlug}`); + Object.values(ContentfulData?.pages ?? []).forEach((page) => { + it( + "Should have correct content on non-authorised pages. Testing " + + page.fields.internalName, + () => { + const slug = `/${page.fields.slug.replace("/", "")}`; + cy.visit(slug); + ValidatePage(slug, page); + } + ); + }); - const sections = dataMapper.mappedSections; + Object.values(ContentfulData?.mappedSections ?? []).forEach((section) => { + it(`${section.name} should have every question with correct content`, () => { + cy.loginWithEnv(`${selfAssessmentSlug}`); - for (const section of Object.values(sections)) { validateSections( section, section.minimumPathsToNavigateQuestions, - dataMapper + ContentfulData ); - } - }); - - it.skip("Should retrieve correct recommendations for maturity", () => { - if (dataMapper.pages == null) { - console.log("Datamapper has not processed data correctly"); - return; - } - - cy.loginWithEnv(`${selfAssessmentSlug}`); + }); - const sections = dataMapper.mappedSections; + Object.entries(section.minimumPathsForRecommendations).forEach( + ([maturity, path]) => { + it(`${section.name} should retrieve correct recommendation for ${maturity} maturity, and all content is valid`, () => { + cy.loginWithEnv(`${selfAssessmentSlug}`); - for (const section of Object.values(sections)) { - for (const [maturity, path] of Object.entries( - section.minimumPathsForRecommendations - )) { - validateSections(section, [path], dataMapper, () => { - validateRecommendationForMaturity(section, maturity); + validateSections(section, [path], ContentfulData, () => { + validateRecommendationForMaturity(section, maturity); + }); }); } - } + ); }); }); +/** + * Check if data has been loaded and log a message if not. + * + * @param {Array | Map} contentMap - the map of content + * @return {boolean} haveContent - whether data has been loaded + */ +function dataLoaded(contentMap) { + const haveContent = + contentMap && (contentMap.length > 0 || contentMap.size > 0); + + if (!haveContent) { + console.log("Data has not been loaded"); + console.log(contentMap); + } + + return haveContent; +} + /** * Validates a recommendation for a specific maturity level. * @@ -253,6 +239,7 @@ function validateAnswers(matchingQuestion) { } function FindPageForSlug({ slug, dataMapper }) { + console.log(dataMapper); for (const [id, page] of dataMapper.pages) { if (page.fields.slug == slug) { return page; diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful-data-loader.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful-data-loader.js new file mode 100644 index 000000000..5bb359621 --- /dev/null +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful-data-loader.js @@ -0,0 +1,10 @@ +import { contentful } from "./contentful"; +import DataMapper from "../../../../contentful/export-processor/data-mapper"; + +const getContentfulData = () => { + return new DataMapper(contentful); +}; + +const contentfulData = getContentfulData(); + +export default contentfulData; diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/contentful.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful.js similarity index 100% rename from tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/contentful.js rename to tests/Dfe.PlanTech.Web.E2ETests/cypress/helpers/contentful.js From 3c6e5bb78eef0a5d508b57e29f6f03a783bfb3c5 Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:14:35 +0000 Subject: [PATCH 2/7] fix: unauthorised pages --- .../cypress/e2e/dynamic-page-validator.cy.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index 7c6b1b50b..2e1490f73 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -4,6 +4,8 @@ import { selfAssessmentSlug } from "../helpers/page-slugs"; import ValidateContent from "../helpers/content-validators/content-validator"; describe("Pages should have content", () => { + const pages = Object.values(ContentfulData?.pages ?? []); + before(() => {}); it("Should render navigation links", () => { @@ -39,17 +41,19 @@ describe("Pages should have content", () => { ValidatePage(slug, selfAssessmentPage); }); - Object.values(ContentfulData?.pages ?? []).forEach((page) => { - it( - "Should have correct content on non-authorised pages. Testing " + - page.fields.internalName, - () => { - const slug = `/${page.fields.slug.replace("/", "")}`; - cy.visit(slug); - ValidatePage(slug, page); - } - ); - }); + Array.from(ContentfulData?.pages ?? []) + .map(([_, page]) => page) + .forEach((page) => { + it( + "Should have correct content on non-authorised pages. Testing " + + page.fields.internalName, + () => { + const slug = `/${page.fields.slug.replace("/", "")}`; + cy.visit(slug); + ValidatePage(slug, page); + } + ); + }); Object.values(ContentfulData?.mappedSections ?? []).forEach((section) => { it(`${section.name} should have every question with correct content`, () => { From 031ccfe03c3ce668c17f2b72bbcddacd85010f8d Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:19:03 +0000 Subject: [PATCH 3/7] fix: only test unauthorised pages again --- .../cypress/e2e/dynamic-page-validator.cy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index 2e1490f73..3053592fd 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -43,6 +43,7 @@ describe("Pages should have content", () => { Array.from(ContentfulData?.pages ?? []) .map(([_, page]) => page) + .filter((page) => !page.fields.requiresAuthorisation) .forEach((page) => { it( "Should have correct content on non-authorised pages. Testing " + From 6f9bf729ee91a8d27713bb61960641ad53bd1f12 Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:39:37 +0000 Subject: [PATCH 4/7] chore: remove unused var --- .../cypress/e2e/dynamic-page-validator.cy.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index 3053592fd..296a63a98 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -4,8 +4,6 @@ import { selfAssessmentSlug } from "../helpers/page-slugs"; import ValidateContent from "../helpers/content-validators/content-validator"; describe("Pages should have content", () => { - const pages = Object.values(ContentfulData?.pages ?? []); - before(() => {}); it("Should render navigation links", () => { From 4f4098daf3746a7e18219a2b8f50c9d52b988256 Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:39:51 +0000 Subject: [PATCH 5/7] chore: remove unused before --- .../cypress/e2e/dynamic-page-validator.cy.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index 296a63a98..ccf30f0a1 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -4,8 +4,6 @@ import { selfAssessmentSlug } from "../helpers/page-slugs"; import ValidateContent from "../helpers/content-validators/content-validator"; describe("Pages should have content", () => { - before(() => {}); - it("Should render navigation links", () => { const navigationLinks = ContentfulData.contents["navigationLink"]; if (!dataLoaded(navigationLinks)) { From 45fecca993accdbd0efe1c0676a3dfc9fb18f8ed Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:42:48 +0000 Subject: [PATCH 6/7] chore: remove log command --- .../cypress/e2e/dynamic-page-validator.cy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index ccf30f0a1..ee113cec8 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -21,6 +21,7 @@ describe("Pages should have content", () => { if (!dataLoaded(ContentfulData.pages)) { return; } + cy.loginWithEnv(`${selfAssessmentSlug}`); const slug = selfAssessmentSlug.replace("/", ""); const selfAssessmentPage = FindPageForSlug({ @@ -240,7 +241,6 @@ function validateAnswers(matchingQuestion) { } function FindPageForSlug({ slug, dataMapper }) { - console.log(dataMapper); for (const [id, page] of dataMapper.pages) { if (page.fields.slug == slug) { return page; From c67a7315be12b1cdf31beccf234148c074965f7f Mon Sep 17 00:00:00 2001 From: Jim Washbrook Date: Thu, 8 Feb 2024 14:43:07 +0000 Subject: [PATCH 7/7] chore: remove log command --- .../cypress/e2e/dynamic-page-validator.cy.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js index ee113cec8..932937790 100644 --- a/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js +++ b/tests/Dfe.PlanTech.Web.E2ETests/cypress/e2e/dynamic-page-validator.cy.js @@ -90,7 +90,6 @@ function dataLoaded(contentMap) { if (!haveContent) { console.log("Data has not been loaded"); - console.log(contentMap); } return haveContent;