Skip to content

Commit

Permalink
tests: E2E tests for every possible recommendation page (#500)
Browse files Browse the repository at this point in the history
* feat: add warning validator

* chore: add missing fields

* feat: Test every question page for every sub-topic

- Test question
- Test answers
- Test check answers matches
- Uses path(s) that navigates through each question in a sub-topic

* chore: add skip back

* fix: correct name variable

* feat: validate every recommendation for every maturity

* feat: validate recommendation pages

* chore: revert testing things
  • Loading branch information
jimwashbrook authored Feb 7, 2024
1 parent baada83 commit 1bd6d12
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- [ ] InsetText
- [ ] NavigationLink
- [x] Question
- [ ] RecommendationPage
- [x] RecommendationPage
- [x] Section *
- [x] TextBody *
- [x] Titles
Expand All @@ -35,8 +35,8 @@

### Page routing validated

- [ ] Sub-topic routing
- [ ] Recommendation routing
- [x] Sub-topic routing
- [x] Recommendation routing

### Other validated

Expand All @@ -49,7 +49,7 @@
- [ ] 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

- [ ] Clear section status before each run

#### Integrate with CI/CD Pipeline

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,86 @@ describe("Pages should have content", () => {
validateSections(section, section.minimumPathsToNavigateQuestions);
}
});

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;

for (const section of Object.values(sections)) {
for (const [maturity, path] of Object.entries(
section.minimumPathsForRecommendations
)) {
validateSections(section, [path], () => {
validateRecommendationForMaturity(section, maturity);
});
}
}
});
});

function validateSections(section, paths) {
/**
* Validates a recommendation for a specific maturity level.
*
* @param {Object} section - the section containing the recommendations
* @param {string} maturity - the maturity level to validate
* @throws {Error} if matching recommendation is not found
*/
function validateRecommendationForMaturity(section, maturity) {
//Validate recommendation banner
cy.get(
"div.govuk-notification-banner.govuk-notification-banner--success h3.govuk-notification-banner__heading"
).contains(`You have one new recommendation for ${section.name}`);

const matchingRecommendation = section.recommendations.find(
(recommendation) => recommendation.maturity == maturity
);

if (!matchingRecommendation)
throw new Error(
`Couldn't find a recommendation for maturity ${maturity} in ${section.name}`,
section
);

const expectedPath =
`/${section.name.trim()}/recommendation/${matchingRecommendation.page.fields.slug.trim()}`
.toLowerCase()
.replace(/ /g, "-");

cy.get(
"ul.app-task-list__items li.app-task-list__item span.app-task-list__task-name a.govuk-link"
)
.contains(matchingRecommendation.displayName.trim())
.should("have.attr", "href")
.and("include", expectedPath);

cy.get(
"ul.app-task-list__items li.app-task-list__item span.app-task-list__task-name a.govuk-link"
)
.contains(matchingRecommendation.displayName.trim())
.click();

ValidatePage(
matchingRecommendation.page.fields.slug,
matchingRecommendation.page
);
}

/**
* Validates sections using the given paths
*
* @param {Object} section - the section to validate
* @param {Array} paths - the paths to navigate
* @param {Function} validator - optional validation function to call at the end of every path
*/
function validateSections(section, paths, validator) {
cy.visit(`/${selfAssessmentSlug}`);

for (const path of paths) {
//Navigate through interstitial page
cy.get("ul.app-task-list__items > li a").contains(section.name).click();
Expand All @@ -79,6 +155,10 @@ function validateSections(section, paths) {
validateCheckAnswersPage(path, section);

cy.url().should("include", selfAssessmentSlug);

if (validator) {
validator();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function ValidateHeader(content) {

return cy
.get(`header-component ${tag}`)
.contains(expectedText)
.contains(expectedText.trim())
.should("have.class", expectedClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ function ValidatePage(slug, page) {
ValidateTitle(page.fields.title);
}

for (const content of page.fields.beforeTitleContent) {
ValidateContent(content);
if (page.fields.beforeTitleContent) {
for (const content of page.fields.beforeTitleContent) {
ValidateContent(content);
}
}

for (const content of page.fields.content) {
ValidateContent(content);
if (page.fields.content) {
for (const content of page.fields.content) {
ValidateContent(content);
}
}
}

Expand Down

0 comments on commit 1bd6d12

Please sign in to comment.