From 2b9c1e45ef3b74983cde56a6df5e2c59d36fdfd0 Mon Sep 17 00:00:00 2001 From: Lemmy Adams <103187526+lemmyadams@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:38:15 +0100 Subject: [PATCH] Fix: Update tests to meet new standards (fixes #149) (#150) * Added accordion e2e test * Update test/e2e/accordion.cy.js Co-authored-by: Cahir O'Doherty <41006337+cahirodoherty-learningpool@users.noreply.github.com> * Corrected test * Updated test --------- Co-authored-by: Cahir O'Doherty <41006337+cahirodoherty-learningpool@users.noreply.github.com> --- test/e2e/accordion.cy.js | 48 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/test/e2e/accordion.cy.js b/test/e2e/accordion.cy.js index d7c8bec..65b3c72 100644 --- a/test/e2e/accordion.cy.js +++ b/test/e2e/accordion.cy.js @@ -1,45 +1,43 @@ describe('Accordion', function () { + const stripHtml = cy.helpers.stripHtml; + function loopThroughAccordion(accordionComponent) { - const items = accordionComponent._items - cy.get('.accordion-item').should('have.length', items.length) + const items = accordionComponent._items; + cy.get('.accordion-item').should('have.length', items.length); // Check each accordion item items.forEach((item, index) => { - const bodyWithoutHtml = item.body.replace(/<[^>]*>/g, '') // Check within the correct item so it doesn't detect visibility from other items cy.get('.accordion-item').eq(index).within(() => { - cy.get('.accordion-item__btn.is-visited').should('not.exist') - cy.get('.accordion-item__body-inner').should('not.be.visible') - cy.get('.accordion-item__title').should('contain', item.title).click() - cy.get('.accordion-item__btn.is-visited').should('exist') + cy.get('.accordion-item__btn.is-visited').should('not.exist'); + cy.get('.accordion-item__body-inner').should('not.be.visible'); + cy.get('.accordion-item__title').should('contain', item.title).click(); + cy.get('.accordion-item__btn.is-visited').should('exist'); cy.get('.accordion-item__body-inner') .should('be.visible') - .should('contain', bodyWithoutHtml) - cy.get('.accordion-item__title').click() - cy.get('.accordion-item__body-inner').should('not.be.visible') - }) - }) - } + .should('contain', stripHtml(item.body)); + cy.get('.accordion-item__title').click(); + cy.get('.accordion-item__body-inner').should('not.be.visible'); + }); + }); + }; beforeEach(function () { - cy.getData() + cy.getData(); }); it('should display the accordion component', function () { - const accordionComponents = this.data.components.filter((component) => component._component === 'accordion') - accordionComponents.forEach((accordionComponent) => { + const accordionComponents = this.data.components.filter(component => component._component === 'accordion'); + accordionComponents.forEach(accordionComponent => { cy.visit(`/#/preview/${accordionComponent._id}`); - const bodyWithoutHtml = accordionComponent.body.replace(/<[^>]*>/g, '') - - // Test basic accordion component - cy.testContainsOrNotExists('.accordion__title', accordionComponent.displayTitle) - cy.testContainsOrNotExists('.accordion__body', bodyWithoutHtml) - cy.testContainsOrNotExists('.accordion__instruction', accordionComponent.instruction) - + cy.testContainsOrNotExists('.accordion__body', stripHtml(accordionComponent.body)); + cy.testContainsOrNotExists('.accordion__title', stripHtml(accordionComponent.displayTitle)); + cy.testContainsOrNotExists('.accordion__instruction', stripHtml(accordionComponent.instruction)); + // Test accordion items - loopThroughAccordion(accordionComponent) + loopThroughAccordion(accordionComponent); // Allow the component to load and run external custom tests - cy.wait(1000) + cy.wait(1000); }) }); });