summaryrefslogtreecommitdiff
path: root/spec/frontend/alert_handler_spec.js
blob: e4cd38a7799239bb117729f7cf3abe02da74e96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { setHTMLFixture } from 'helpers/fixtures';
import initAlertHandler from '~/alert_handler';

describe('Alert Handler', () => {
  const ALERT_CLASS = 'gl-alert';
  const BANNER_CLASS = 'gl-banner';
  const DISMISS_CLASS = 'gl-alert-dismiss';
  const DISMISS_LABEL = 'Dismiss';

  const generateHtml = (parentClass) =>
    `<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}`);

  describe('initAlertHandler', () => {
    describe('with one alert', () => {
      beforeEach(() => {
        setHTMLFixture(generateHtml(ALERT_CLASS));
        initAlertHandler();
      });

      it('should render the alert', () => {
        expect(findFirstAlert()).toExist();
      });

      it('should dismiss the alert on click', () => {
        findFirstDismissButton().click();
        expect(findFirstAlert()).not.toExist();
      });
    });

    describe('with two alerts', () => {
      beforeEach(() => {
        setHTMLFixture(generateHtml(ALERT_CLASS) + generateHtml(ALERT_CLASS));
        initAlertHandler();
      });

      it('should render two alerts', () => {
        expect(findAllAlerts()).toHaveLength(2);
      });

      it('should dismiss only one alert on click', () => {
        findFirstDismissButton().click();
        expect(findAllAlerts()).toHaveLength(1);
      });
    });

    describe('with a dismissible banner', () => {
      beforeEach(() => {
        setHTMLFixture(generateHtml(BANNER_CLASS));
        initAlertHandler();
      });

      it('should render the banner', () => {
        expect(findFirstBanner()).toExist();
      });

      it('should dismiss the banner on click', () => {
        findFirstDismissButton().click();
        expect(findFirstBanner()).not.toExist();
      });
    });

    // 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();
      });

      it('should render the banner', () => {
        expect(findFirstAlert()).toExist();
      });

      it('should dismiss the banner on click', () => {
        findFirstDismissButtonByClass().click();
        expect(findFirstAlert()).not.toExist();
      });
    });
  });
});