Welcome to mirror list, hosted at ThFree Co, Russian Federation.

edit_form_buttons_spec.js « confidential « sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15493d3087f95f796f01e9b8ff4a4eba69ae2541 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { shallowMount } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui';
import EditFormButtons from '~/sidebar/components/confidential/edit_form_buttons.vue';
import eventHub from '~/sidebar/event_hub';
import createStore from '~/notes/stores';
import waitForPromises from 'helpers/wait_for_promises';
import flash from '~/flash';

jest.mock('~/sidebar/event_hub', () => ({ $emit: jest.fn() }));
jest.mock('~/flash');

describe('Edit Form Buttons', () => {
  let wrapper;
  let store;
  const findConfidentialToggle = () => wrapper.find('[data-testid="confidential-toggle"]');

  const createComponent = ({
    props = {},
    data = {},
    confidentialApolloSidebar = false,
    resolved = true,
  }) => {
    store = createStore();
    if (resolved) {
      jest.spyOn(store, 'dispatch').mockResolvedValue();
    } else {
      jest.spyOn(store, 'dispatch').mockRejectedValue();
    }

    wrapper = shallowMount(EditFormButtons, {
      propsData: {
        fullPath: '',
        ...props,
      },
      data() {
        return {
          isLoading: true,
          ...data,
        };
      },
      provide: {
        glFeatures: {
          confidentialApolloSidebar,
        },
      },
      store,
    });
  };

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('when isLoading', () => {
    beforeEach(() => {
      createComponent({});

      wrapper.vm.$store.state.noteableData.confidential = false;
    });

    it('renders "Applying" in the toggle button', () => {
      expect(findConfidentialToggle().text()).toBe('Applying');
    });

    it('disables the toggle button', () => {
      expect(findConfidentialToggle().attributes('disabled')).toBe('disabled');
    });

    it('finds the GlLoadingIcon', () => {
      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });
  });

  describe('when not confidential', () => {
    it('renders Turn On in the toggle button', () => {
      createComponent({
        data: {
          isLoading: false,
        },
      });

      expect(findConfidentialToggle().text()).toBe('Turn On');
    });
  });

  describe('when confidential', () => {
    beforeEach(() => {
      createComponent({
        data: {
          isLoading: false,
        },
      });

      wrapper.vm.$store.state.noteableData.confidential = true;
    });

    it('renders on or off text based on confidentiality', () => {
      expect(findConfidentialToggle().text()).toBe('Turn Off');
    });

    describe('when clicking on the confidential toggle', () => {
      it('emits updateConfidentialAttribute', () => {
        findConfidentialToggle().trigger('click');

        expect(eventHub.$emit).toHaveBeenCalledWith('updateConfidentialAttribute');
      });
    });
  });

  describe('when confidentialApolloSidebar is turned on', () => {
    const isConfidential = true;

    describe('when succeeds', () => {
      beforeEach(() => {
        createComponent({ data: { isLoading: false }, confidentialApolloSidebar: true });
        wrapper.vm.$store.state.noteableData.confidential = isConfidential;
        findConfidentialToggle().trigger('click');
      });

      it('dispatches the correct action', () => {
        expect(store.dispatch).toHaveBeenCalledWith('updateConfidentialityOnIssue', {
          confidential: !isConfidential,
          fullPath: '',
        });
      });

      it('resets loading', () => {
        return waitForPromises().then(() => {
          expect(wrapper.find(GlLoadingIcon).exists()).toBe(false);
        });
      });

      it('emits close form', () => {
        return waitForPromises().then(() => {
          expect(eventHub.$emit).toHaveBeenCalledWith('closeConfidentialityForm');
        });
      });
    });

    describe('when fails', () => {
      beforeEach(() => {
        createComponent({
          data: { isLoading: false },
          confidentialApolloSidebar: true,
          resolved: false,
        });
        wrapper.vm.$store.state.noteableData.confidential = isConfidential;
        findConfidentialToggle().trigger('click');
      });

      it('calls flash with the correct message', () => {
        expect(flash).toHaveBeenCalledWith(
          'Something went wrong trying to change the confidentiality of this issue',
        );
      });
    });
  });
});