-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test autofocusing behaviour of notification banner
Test if the element focuses and is scrolled into. Also test if there is an error summary on the page, in which case don't focus the notification banner. Also test if another notification banner on the page has already received focus.
- Loading branch information
1 parent
befc839
commit ac4831d
Showing
6 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/views/examples/notification-banner-and-error-summary/index.njk
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{% extends "_generic.njk" %} | ||
|
||
{% from "error-summary/macro.njk" import govukErrorSummary %} | ||
{% from "notification-banner/macro.njk" import govukNotificationBanner %} | ||
|
||
{% set pageTitle = "Notification banner and error summary" %} | ||
{% block pageTitle %}{{ "Error: " if errors }}{{ pageTitle }} - GOV.UK{% endblock %} | ||
|
||
{% block header %} | ||
{% include "../../partials/banner.njk" %} | ||
{{ govukHeader({}) }} | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
|
||
{{ govukErrorSummary({ | ||
titleText: "There is a problem" | ||
})}} | ||
|
||
{{ govukNotificationBanner({ | ||
text: "Invite sent to example@email.com", | ||
type: "success" | ||
})}} | ||
|
||
</div> | ||
</div> | ||
{% endblock %} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% extends "_generic.njk" %} | ||
|
||
{% from "notification-banner/macro.njk" import govukNotificationBanner %} | ||
|
||
{% set pageTitle = "Notification banner" %} | ||
{% block pageTitle %}{{ "Error: " if errors }}{{ pageTitle }} - GOV.UK{% endblock %} | ||
|
||
{% block header %} | ||
{% include "../../partials/banner.njk" %} | ||
{{ govukHeader({}) }} | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
|
||
{{ govukNotificationBanner({ | ||
text: "Invite sent to example@email.com", | ||
type: "success" | ||
})}} | ||
|
||
{{ govukNotificationBanner({ | ||
text: "You need to respond within two days", | ||
type: "success" | ||
})}} | ||
|
||
<h1 class="govuk-heading-xl">{{ pageTitle }}</h1> | ||
|
||
<p class="govuk-body">Please note that it's not recommended to have multiple banners of the same type on the page. This page is an example for testing purposes.</p> | ||
</div> | ||
</div> | ||
{% endblock %} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{% extends "_generic.njk" %} | ||
|
||
{% from "notification-banner/macro.njk" import govukNotificationBanner %} | ||
|
||
{% set pageTitle = "Notification banner" %} | ||
{% block pageTitle %}{{ "Error: " if errors }}{{ pageTitle }} - GOV.UK{% endblock %} | ||
|
||
{% block header %} | ||
{% include "../../partials/banner.njk" %} | ||
{{ govukHeader({}) }} | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
|
||
{{ govukNotificationBanner({ | ||
text: "Invite sent to example@email.com", | ||
type: "success" | ||
})}} | ||
|
||
<h1 class="govuk-heading-xl">{{ pageTitle }}</h1> | ||
|
||
<p class="govuk-body">These paragraphs make the page taller so that we can test the scrolling behaviour.</p> | ||
|
||
<p class="govuk-body">These paragraphs make the page taller so that we can test the scrolling behaviour.</p> | ||
|
||
<p class="govuk-body">These paragraphs make the page taller so that we can test the scrolling behaviour.</p> | ||
|
||
<p class="govuk-body">These paragraphs make the page taller so that we can test the scrolling behaviour.</p> | ||
|
||
<p class="govuk-body">These paragraphs make the page taller so that we can test the scrolling behaviour.</p> | ||
</div> | ||
</div> | ||
{% endblock %} |
79 changes: 79 additions & 0 deletions
79
src/govuk/components/notification-banner/notification-banner.test.js
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-env jest */ | ||
|
||
const configPaths = require('../../../../config/paths.json') | ||
const PORT = configPaths.ports.test | ||
|
||
const baseUrl = 'http://localhost:' + PORT | ||
|
||
describe('/components/notification-banner', () => { | ||
describe('/examples/notification-banner', () => { | ||
describe('when JavaScript is available', () => { | ||
it('focuses the notification banner', async () => { | ||
await page.goto(`${baseUrl}/examples/notification-banner`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).toBe('govuk-notification-banner') | ||
}) | ||
|
||
it('scrolls the notification banner to the top of the screen', async () => { | ||
// Wait until 'domcontentloaded' to have correct coordinates for element position | ||
await page.goto(`${baseUrl}/examples/notification-banner`, { waitUntil: 'domcontentloaded' }) | ||
|
||
const positionFromTopOfViewport = await page.$eval('.govuk-notification-banner', (elem) => { | ||
return elem.getBoundingClientRect().top | ||
}) | ||
|
||
expect(positionFromTopOfViewport).toEqual(0) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('/examples/notification-banner-and-error-summary', () => { | ||
describe('when there is an error summary on the page', () => { | ||
it('the notification banner is not focused and the error summary is', async () => { | ||
await page.goto(`${baseUrl}/examples/notification-banner-and-error-summary`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).toBe('govuk-error-summary') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('/examples/notification-banner-multiple', () => { | ||
describe('when there are multiple notification banners on the page', () => { | ||
it('the first one on the page is focused', async () => { | ||
await page.goto(`${baseUrl}/examples/notification-banner-multiple`, { waitUntil: 'load' }) | ||
|
||
const activeElementTextContent = await page.evaluate(() => document.activeElement.textContent) | ||
|
||
expect(activeElementTextContent).toContain('Invite sent to example@email.com') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('components/notification-banner/auto-focus-as-is-set-to-false,-with-type-as-success', () => { | ||
describe('when auto-focus is set to false', () => { | ||
it('does not focus the notification banner', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/auto-focus-as-is-set-to-false,-with-type-as-success/preview`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).not.toBe('govuk-notification-banner') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('components/notification-banner/data-auto-focus-is-set-without-role=alert/', () => { | ||
describe('when role="alert" is not set', () => { | ||
it('does not focus the notification banner', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/data-auto-focus-is-set-without-role=alert/preview`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).not.toBe('govuk-notification-banner') | ||
}) | ||
}) | ||
}) | ||
}) |
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