From a884c568288f9929d35b2f3faa652fc96523b259 Mon Sep 17 00:00:00 2001 From: Suzanne Aitchison Date: Wed, 11 Aug 2021 11:50:49 +0100 Subject: [PATCH] Reinstate skip link on search page (#14463) * reinstate skip link on search page * actually commit the tests this time * use optional chaining * Revert "use optional chaining" This reverts commit 921677f425c2e90f272a6e21dc8a8d37f916e0da. --- app/assets/javascripts/base.js.erb | 7 +---- .../searchFlows/skipToContent.spec.js | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/base.js.erb b/app/assets/javascripts/base.js.erb index 8688a5814..a4a56931e 100644 --- a/app/assets/javascripts/base.js.erb +++ b/app/assets/javascripts/base.js.erb @@ -123,15 +123,10 @@ var instantClick prog.classList.remove("showing"); if (newUrl) { - // We skip showing the "Skip to content" button for the search page, see - // https://github.com/forem/forem/issues/13876 - var path = new URL(newUrl, window.location.origin).pathname; - if (path != '/search') { - const skipLink = document.querySelector('.skip-content-link'); + const skipLink = document.querySelector('.skip-content-link'); if (skipLink) { skipLink.focus(); } - } document.getElementById('page-route-change').textContent = title; history.pushState(null, null, newUrl.replace("?samepage=true","").replace("&samepage=true","")) diff --git a/cypress/integration/seededFlows/searchFlows/skipToContent.spec.js b/cypress/integration/seededFlows/searchFlows/skipToContent.spec.js index e8331569f..0d22522b4 100644 --- a/cypress/integration/seededFlows/searchFlows/skipToContent.spec.js +++ b/cypress/integration/seededFlows/searchFlows/skipToContent.spec.js @@ -1,14 +1,33 @@ -// Regression test for https://github.com/forem/forem/issues/13876 describe('Search on homepage', () => { - it('Does not show the "Skip to content" link', () => { + beforeEach(() => { + cy.testSetup(); cy.visit('/'); + }); - cy.get('#header-search') - .get('form') - .findByPlaceholderText('Search...') + it('Does not show the "Skip to content" link if button is pressed', () => { + cy.findByRole('search') + .findByLabelText('Search term') + .type('admin mcadmin'); + + cy.findByRole('button', { name: 'Search' }).click(); + + cy.url().should('include', '/search'); + + // Make sure the page has loaded before assertion + cy.findByRole('heading', { name: 'Test article' }); + cy.get('.skip-content-link').should('not.be.visible'); + }); + + it('Shows "Skip to content" if search is submitted by Enter press', () => { + cy.findByRole('search') + .findByLabelText('Search term') .type('admin mcadmin{enter}'); cy.url().should('include', '/search'); - cy.get('.skip-content-link').should('not.be.visible'); + // Make sure the page has loaded before assertion + cy.findByRole('heading', { name: 'Test article' }); + + cy.get('.skip-content-link').should('be.visible'); + cy.get('.skip-content-link').should('have.focus'); }); });