2022-05-13 08:08:49 -04:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2020-09-08 05:08:31 -04:00
|
|
|
import initAlertHandler from '~/alert_handler';
|
|
|
|
|
|
|
|
describe('Alert Handler', () => {
|
2020-10-01 11:10:05 -04:00
|
|
|
const ALERT_CLASS = 'gl-alert';
|
|
|
|
const BANNER_CLASS = 'gl-banner';
|
|
|
|
const DISMISS_CLASS = 'gl-alert-dismiss';
|
|
|
|
const DISMISS_LABEL = 'Dismiss';
|
2020-09-08 05:08:31 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const generateHtml = (parentClass) =>
|
2020-10-01 11:10:05 -04:00
|
|
|
`<div class="${parentClass}">
|
|
|
|
<button aria-label="${DISMISS_LABEL}">Dismiss</button>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
const findFirstAlert = () => document.querySelector(`.${ALERT_CLASS}`);
|
|
|
|
const findFirstBanner = () => document.querySelector(`.${BANNER_CLASS}`);
|
|
|
|
const findAllAlerts = () => document.querySelectorAll(`.${ALERT_CLASS}`);
|
|
|
|
const findFirstDismissButton = () => document.querySelector(`[aria-label="${DISMISS_LABEL}"]`);
|
|
|
|
const findFirstDismissButtonByClass = () => document.querySelector(`.${DISMISS_CLASS}`);
|
2020-09-08 05:08:31 -04:00
|
|
|
|
|
|
|
describe('initAlertHandler', () => {
|
|
|
|
describe('with one alert', () => {
|
|
|
|
beforeEach(() => {
|
2020-10-01 11:10:05 -04:00
|
|
|
setHTMLFixture(generateHtml(ALERT_CLASS));
|
2020-09-08 05:08:31 -04:00
|
|
|
initAlertHandler();
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-09-08 05:08:31 -04:00
|
|
|
it('should render the alert', () => {
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstAlert()).not.toBe(null);
|
2020-09-08 05:08:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should dismiss the alert on click', () => {
|
2020-10-01 11:10:05 -04:00
|
|
|
findFirstDismissButton().click();
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstAlert()).toBe(null);
|
2020-09-08 05:08:31 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with two alerts', () => {
|
|
|
|
beforeEach(() => {
|
2020-10-01 11:10:05 -04:00
|
|
|
setHTMLFixture(generateHtml(ALERT_CLASS) + generateHtml(ALERT_CLASS));
|
2020-09-08 05:08:31 -04:00
|
|
|
initAlertHandler();
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-09-08 05:08:31 -04:00
|
|
|
it('should render two alerts', () => {
|
|
|
|
expect(findAllAlerts()).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dismiss only one alert on click', () => {
|
2020-10-01 11:10:05 -04:00
|
|
|
findFirstDismissButton().click();
|
2020-09-08 05:08:31 -04:00
|
|
|
expect(findAllAlerts()).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
2020-10-01 11:10:05 -04:00
|
|
|
|
|
|
|
describe('with a dismissible banner', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(generateHtml(BANNER_CLASS));
|
|
|
|
initAlertHandler();
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-10-01 11:10:05 -04:00
|
|
|
it('should render the banner', () => {
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstBanner()).not.toBe(null);
|
2020-10-01 11:10:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should dismiss the banner on click', () => {
|
|
|
|
findFirstDismissButton().click();
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstBanner()).toBe(null);
|
2020-10-01 11:10:05 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Dismiss buttons *should* have the correct aria labels, but some of them won't
|
|
|
|
// because legacy code isn't always a11y compliant.
|
|
|
|
// This tests that the fallback for the incorrectly labelled buttons works.
|
|
|
|
describe('with a mislabelled dismiss button', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setHTMLFixture(`<div class="${ALERT_CLASS}">
|
|
|
|
<button class="${DISMISS_CLASS}">Dismiss</button>
|
|
|
|
</div>`);
|
|
|
|
initAlertHandler();
|
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-10-01 11:10:05 -04:00
|
|
|
it('should render the banner', () => {
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstAlert()).not.toBe(null);
|
2020-10-01 11:10:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should dismiss the banner on click', () => {
|
|
|
|
findFirstDismissButtonByClass().click();
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findFirstAlert()).toBe(null);
|
2020-10-01 11:10:05 -04:00
|
|
|
});
|
|
|
|
});
|
2020-09-08 05:08:31 -04:00
|
|
|
});
|
|
|
|
});
|