-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2532 from alphagov/puppeteer-18-axe
- Loading branch information
Showing
21 changed files
with
1,629 additions
and
881 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,63 @@ | ||
|
||
const { setupPage } = require('../lib/jest-utilities.js') | ||
const configPaths = require('../lib/paths.js') | ||
const PORT = configPaths.testPort | ||
const { goTo, isVisible } = require('../lib/puppeteer-helpers.js') | ||
|
||
let page | ||
const baseUrl = 'http://localhost:' + PORT | ||
describe('Back to top', () => { | ||
let $module | ||
let $backToTopLink | ||
let pageHeight | ||
|
||
beforeAll(async () => { | ||
page = await setupPage() | ||
}) | ||
async function setup (page) { | ||
$module = await page.$('[data-module="app-back-to-top"]') | ||
$backToTopLink = await $module.$('a') | ||
|
||
afterAll(async () => { | ||
await page.close() | ||
}) | ||
// Scrollable height of body | ||
pageHeight = await page.$eval('body', ($element) => $element.scrollHeight) ?? 0 | ||
} | ||
|
||
const BACK_TO_TOP_LINK_SELECTOR = '[data-module="app-back-to-top"] a' | ||
function scrollTo (page, scrollY) { | ||
return page.evaluate((y) => window.scroll(0, y), scrollY) | ||
} | ||
|
||
beforeEach(async () => { | ||
await page.setJavaScriptEnabled(true) | ||
|
||
await goTo(page, '/styles/colour/') | ||
await scrollTo(page, 0) | ||
await setup(page) | ||
}) | ||
|
||
describe('Back to top', () => { | ||
it('is always visible when JavaScript is disabled', async () => { | ||
await page.setJavaScriptEnabled(false) | ||
await page.goto(`${baseUrl}/styles/colour/`, { waitUntil: 'load' }) | ||
const isBackToTopVisible = await page.waitForSelector(BACK_TO_TOP_LINK_SELECTOR, { visible: true }) | ||
expect(isBackToTopVisible).toBeTruthy() | ||
|
||
// Reload page again | ||
await page.reload() | ||
await setup(page) | ||
|
||
// Visible on page | ||
await expect(isVisible($backToTopLink)).resolves.toBe(true) | ||
}) | ||
|
||
it('is hidden when at the top of the page', async () => { | ||
await page.goto(`${baseUrl}/styles/colour/`, { waitUntil: 'load' }) | ||
const isBackToTopHidden = await page.waitForSelector(BACK_TO_TOP_LINK_SELECTOR, { visible: false }) | ||
expect(isBackToTopHidden).toBeTruthy() | ||
await scrollTo(page, 0) | ||
|
||
// Visible on page, hidden from viewport | ||
await expect(isVisible($backToTopLink)).resolves.toBe(true) | ||
await expect($backToTopLink.isIntersectingViewport()).resolves.toBe(false) | ||
}) | ||
|
||
it('is visible when at the bottom of the page', async () => { | ||
await page.goto(`${baseUrl}/styles/colour/`, { waitUntil: 'load' }) | ||
// Scroll to the bottom of the page | ||
await page.evaluate(() => window.scrollBy(0, document.body.scrollHeight)) | ||
const isBackToTopVisible = await page.waitForSelector(BACK_TO_TOP_LINK_SELECTOR, { visible: true }) | ||
expect(isBackToTopVisible).toBeTruthy() | ||
await scrollTo(page, pageHeight) | ||
|
||
// Visible on page, shown in viewport | ||
await expect(isVisible($backToTopLink)).resolves.toBe(true) | ||
await expect($backToTopLink.isIntersectingViewport()).resolves.toBe(true) | ||
}) | ||
|
||
it('goes back to the top of the page when interacted with', async () => { | ||
await page.goto(`${baseUrl}/styles/colour/`, { waitUntil: 'load' }) | ||
// Scroll to the bottom of the page | ||
await page.evaluate(() => window.scrollBy(0, document.body.scrollHeight)) | ||
// Make sure the back to top component is available to click | ||
await page.waitForSelector(BACK_TO_TOP_LINK_SELECTOR, { visible: true }) | ||
await page.click(BACK_TO_TOP_LINK_SELECTOR) | ||
const isAtTopOfPage = await page.evaluate(() => window.scrollY === 0) | ||
expect(isAtTopOfPage).toBeTruthy() | ||
await scrollTo(page, pageHeight) | ||
await $backToTopLink.click() | ||
|
||
// Scrolled to top | ||
await expect(page.evaluate(() => window.scrollY)).resolves.toBe(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.