summaryrefslogtreecommitdiff
path: root/spec/frontend/alert_handler_spec.js
blob: ba2f4f24aa52c44ec698380173a8fa4e9b6d6f10 (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
import { setHTMLFixture } from 'helpers/fixtures';
import initAlertHandler from '~/alert_handler';

describe('Alert Handler', () => {
  const ALERT_SELECTOR = 'gl-alert';
  const CLOSE_SELECTOR = 'gl-alert-dismiss';
  const ALERT_HTML = `<div class="${ALERT_SELECTOR}"><button class="${CLOSE_SELECTOR}">Dismiss</button></div>`;

  const findFirstAlert = () => document.querySelector(`.${ALERT_SELECTOR}`);
  const findAllAlerts = () => document.querySelectorAll(`.${ALERT_SELECTOR}`);
  const findFirstCloseButton = () => document.querySelector(`.${CLOSE_SELECTOR}`);

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

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

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

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

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

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