summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_alerts_spec.js
blob: 1952eea4a01de2e63bc6a27e3ba1e21589673854 (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
import { nextTick } from 'vue';
import { setHTMLFixture } from 'helpers/fixtures';
import { TEST_HOST } from 'helpers/test_constants';
import initVueAlerts from '~/vue_alerts';

describe('VueAlerts', () => {
  const alerts = [
    {
      title: 'Lorem',
      html: 'Lorem <strong>Ipsum</strong>',
      dismissible: true,
      primaryButtonText: 'Okay!',
      primaryButtonLink: `${TEST_HOST}/okay`,
      variant: 'tip',
    },
    {
      title: 'Hello',
      html: 'Hello <strong>World</strong>',
      dismissible: false,
      primaryButtonText: 'No!',
      primaryButtonLink: `${TEST_HOST}/no`,
      variant: 'info',
    },
  ];

  beforeEach(() => {
    setHTMLFixture(
      alerts
        .map(
          (x) => `
    <div class="js-vue-alert"
      data-dismissible="${x.dismissible}"
      data-title="${x.title}"
      data-primary-button-text="${x.primaryButtonText}"
      data-primary-button-link="${x.primaryButtonLink}"
      data-variant="${x.variant}">${x.html}</div>
    `,
        )
        .join('\n'),
    );
  });

  const findJsHooks = () => document.querySelectorAll('.js-vue-alert');
  const findAlerts = () => document.querySelectorAll('.gl-alert');
  const findAlertDismiss = (alert) => alert.querySelector('.gl-dismiss-btn');

  const serializeAlert = (alert) => ({
    title: alert.querySelector('.gl-alert-title').textContent.trim(),
    html: alert.querySelector('.gl-alert-body div').innerHTML,
    dismissible: Boolean(alert.querySelector('.gl-dismiss-btn')),
    primaryButtonText: alert.querySelector('.gl-alert-action').textContent.trim(),
    primaryButtonLink: alert.querySelector('.gl-alert-action').href,
    variant: [...alert.classList]
      .find((x) => x.match(/gl-alert-(?!not-dismissible)/))
      .replace('gl-alert-', ''),
  });

  it('starts with only JsHooks', () => {
    expect(findJsHooks().length).toEqual(alerts.length);
    expect(findAlerts().length).toEqual(0);
  });

  describe('when mounted', () => {
    beforeEach(() => {
      initVueAlerts();
    });

    it('replaces JsHook with GlAlert', () => {
      expect(findJsHooks().length).toEqual(0);
      expect(findAlerts().length).toEqual(alerts.length);
    });

    it('passes along props to gl-alert', () => {
      expect([...findAlerts()].map(serializeAlert)).toEqual(alerts);
    });

    describe('when dismissed', () => {
      beforeEach(async () => {
        findAlertDismiss(findAlerts()[0]).click();
        await nextTick();
      });

      it('hides the alert', () => {
        expect(findAlerts().length).toEqual(alerts.length - 1);
      });
    });
  });
});