summaryrefslogtreecommitdiff
path: root/spec/frontend/user_lists/components/edit_user_list_spec.js
blob: 7cafe5e1f569d639c858209706edfff617ef95e6 (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
import { GlAlert, GlLoadingIcon } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import Api from '~/api';
import { redirectTo } from '~/lib/utils/url_utility';
import EditUserList from '~/user_lists/components/edit_user_list.vue';
import UserListForm from '~/user_lists/components/user_list_form.vue';
import createStore from '~/user_lists/store/edit';
import { userList } from '../../feature_flags/mock_data';

jest.mock('~/api');
jest.mock('~/lib/utils/url_utility');

Vue.use(Vuex);

describe('user_lists/components/edit_user_list', () => {
  let wrapper;

  const setInputValue = (value) => wrapper.find('[data-testid="user-list-name"]').setValue(value);

  const click = (button) => wrapper.find(`[data-testid="${button}"]`).trigger('click');
  const clickSave = () => click('save-user-list');

  const destroy = () => wrapper?.destroy();

  const factory = () => {
    destroy();

    wrapper = mount(EditUserList, {
      store: createStore({ projectId: '1', userListIid: '2' }),
      provide: {
        userListsDocsPath: '/docs/user_lists',
      },
    });
  };

  afterEach(() => {
    destroy();
  });

  describe('loading', () => {
    beforeEach(() => {
      Api.fetchFeatureFlagUserList.mockReturnValue(new Promise(() => {}));
      factory();
    });

    it('should show a loading icon', () => {
      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });
  });

  describe('loading error', () => {
    const message = 'error creating list';
    let alert;

    beforeEach(async () => {
      Api.fetchFeatureFlagUserList.mockRejectedValue({ message });
      factory();
      await waitForPromises();

      alert = wrapper.find(GlAlert);
    });

    it('should show a flash with the error respopnse', () => {
      expect(alert.text()).toContain(message);
    });

    it('should not be dismissible', async () => {
      expect(alert.props('dismissible')).toBe(false);
    });

    it('should not show a user list form', () => {
      expect(wrapper.find(UserListForm).exists()).toBe(false);
    });
  });

  describe('update', () => {
    beforeEach(async () => {
      Api.fetchFeatureFlagUserList.mockResolvedValue({ data: userList });
      factory();

      await nextTick();
    });

    it('should link to the documentation', () => {
      const link = wrapper.find('[data-testid="user-list-docs-link"]');
      expect(link.attributes('href')).toBe('/docs/user_lists');
    });

    it('should link the cancel button to the user list details path', () => {
      const link = wrapper.find('[data-testid="user-list-cancel"]');
      expect(link.attributes('href')).toBe(userList.path);
    });

    it('should show the user list name in the title', () => {
      expect(wrapper.find('[data-testid="user-list-title"]').text()).toBe(`Edit ${userList.name}`);
    });

    describe('success', () => {
      beforeEach(async () => {
        Api.updateFeatureFlagUserList.mockResolvedValue({ data: userList });
        setInputValue('test');
        clickSave();
        await nextTick();
      });

      it('should create a user list with the entered name', () => {
        expect(Api.updateFeatureFlagUserList).toHaveBeenCalledWith('1', {
          name: 'test',
          iid: userList.iid,
        });
      });

      it('should redirect to the feature flag details page', () => {
        expect(redirectTo).toHaveBeenCalledWith(userList.path);
      });
    });

    describe('error', () => {
      let alert;
      let message;

      beforeEach(async () => {
        message = 'error creating list';
        Api.updateFeatureFlagUserList.mockRejectedValue({ message });
        setInputValue('test');
        clickSave();
        await waitForPromises();

        alert = wrapper.find(GlAlert);
      });

      it('should show a flash with the error respopnse', () => {
        expect(alert.text()).toContain(message);
      });

      it('should dismiss the error if dismiss is clicked', async () => {
        alert.find('button').trigger('click');

        await nextTick();

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