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..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 @@ -1,53 +1,24 @@ -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(() => { - 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"]; - - const indexPage = cy.visit("/"); + 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; } @@ -55,7 +26,7 @@ describe("Pages should have content", () => { const slug = selfAssessmentSlug.replace("/", ""); const selfAssessmentPage = FindPageForSlug({ slug, - dataMapper, + dataMapper: ContentfulData, }); if (!selfAssessmentPage) { @@ -67,47 +38,63 @@ 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}`); + Array.from(ContentfulData?.pages ?? []) + .map(([_, page]) => page) + .filter((page) => !page.fields.requiresAuthorisation) + .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"); + } + + return haveContent; +} + /** * Validates a recommendation for a specific maturity level. * 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