Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Split up unit tests by page/section #502

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contentful/export-processor/data-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export default class DataMapper {
* @returns {IterableIterator<Section>} Iterator for mapped sections
*/
*sectionsToClasses(sections) {
if (!sections || sections.length == 0) {
return;
}

for (const [id, section] of sections) {
yield new Section(section);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,32 @@
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;
}

cy.loginWithEnv(`${selfAssessmentSlug}`);
const slug = selfAssessmentSlug.replace("/", "");
const selfAssessmentPage = FindPageForSlug({
slug,
dataMapper,
dataMapper: ContentfulData,
});

if (!selfAssessmentPage) {
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Loading