summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/dismissible_feedback_alert_spec.js
blob: 463fd74f582cf03425f00e9625f7f7bba2f33a62 (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 { GlAlert, GlSprintf } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import Component from '~/vue_shared/components/dismissible_feedback_alert.vue';

describe('Dismissible Feedback Alert', () => {
  useLocalStorageSpy();

  let wrapper;

  const featureName = 'Dependency List';
  const STORAGE_DISMISSAL_KEY = 'dependency_list_feedback_dismissed';

  const createComponent = ({ props, mountFn = shallowMount } = {}) => {
    wrapper = mountFn(Component, {
      propsData: {
        featureName,
        ...props,
      },
      stubs: {
        GlSprintf,
      },
    });
  };

  const createFullComponent = () => createComponent({ mountFn: mount });
  const findAlert = () => wrapper.findComponent(GlAlert);

  describe('with default', () => {
    beforeEach(() => {
      createComponent();
    });

    it('shows alert', () => {
      expect(findAlert().exists()).toBe(true);
    });

    it('should have the storage key set', () => {
      expect(wrapper.vm.storageKey).toBe(STORAGE_DISMISSAL_KEY);
    });
  });

  describe('with other attributes', () => {
    const mockTitle = 'My title';
    const mockVariant = 'warning';

    beforeEach(() => {
      createComponent({
        props: {
          title: mockTitle,
          variant: mockVariant,
        },
      });
    });

    it('passes props to alert', () => {
      expect(findAlert().props()).toMatchObject({
        title: mockTitle,
        variant: mockVariant,
      });
    });
  });

  describe('dismissible', () => {
    describe('after dismissal', () => {
      beforeEach(() => {
        createFullComponent();
        findAlert().vm.$emit('dismiss');
      });

      it('hides the alert', () => {
        expect(findAlert().exists()).toBe(false);
      });

      it('should remember the dismissal state', () => {
        expect(localStorage.setItem).toHaveBeenCalledWith(STORAGE_DISMISSAL_KEY, 'true');
      });
    });

    describe('already dismissed', () => {
      it('should not show the alert once dismissed', async () => {
        localStorage.setItem(STORAGE_DISMISSAL_KEY, 'true');
        createFullComponent();
        await nextTick();

        expect(findAlert().exists()).toBe(false);
      });
    });
  });
});