summaryrefslogtreecommitdiff
path: root/spec/frontend/user_lists/components/user_lists_table_spec.js
blob: 08eb8ae0843e40ef2509dc8f3cefc278adbe703c (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
import { GlModal } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import * as timeago from 'timeago.js';
import { nextTick } from 'vue';
import UserListsTable from '~/user_lists/components/user_lists_table.vue';
import { userList } from 'jest/feature_flags/mock_data';

jest.mock('timeago.js', () => ({
  format: jest.fn().mockReturnValue('2 weeks ago'),
  register: jest.fn(),
}));

describe('User Lists Table', () => {
  let wrapper;
  let userLists;

  beforeEach(() => {
    userLists = new Array(5).fill(userList).map((x, i) => ({ ...x, id: i }));
    wrapper = mount(UserListsTable, {
      propsData: { userLists },
    });
  });

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

  it('should display the details of a user list', () => {
    expect(wrapper.find('[data-testid="ffUserListName"]').text()).toBe(userList.name);
    expect(wrapper.find('[data-testid="ffUserListIds"]').text()).toBe(
      userList.user_xids.replace(/,/g, ', '),
    );
    expect(wrapper.find('[data-testid="ffUserListTimestamp"]').text()).toBe('created 2 weeks ago');
    expect(timeago.format).toHaveBeenCalledWith(userList.created_at);
  });

  it('should set the title for a tooltip on the created stamp', () => {
    expect(wrapper.find('[data-testid="ffUserListTimestamp"]').attributes('title')).toBe(
      'Feb 4, 2020 8:13am UTC',
    );
  });

  it('should display a user list entry per user list', () => {
    const lists = wrapper.findAll('[data-testid="ffUserList"]');
    expect(lists).toHaveLength(5);
    lists.wrappers.forEach((list) => {
      expect(list.find('[data-testid="ffUserListName"]').exists()).toBe(true);
      expect(list.find('[data-testid="ffUserListIds"]').exists()).toBe(true);
      expect(list.find('[data-testid="ffUserListTimestamp"]').exists()).toBe(true);
    });
  });

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

  describe('delete button', () => {
    it('should display the confirmation modal', async () => {
      const modal = wrapper.find(GlModal);

      wrapper.find('[data-testid="delete-user-list"]').trigger('click');

      await nextTick();
      expect(modal.text()).toContain(`Delete ${userList.name}?`);
      expect(modal.text()).toContain(`User list ${userList.name} will be removed.`);
    });
  });

  describe('confirmation modal', () => {
    let modal;

    beforeEach(async () => {
      modal = wrapper.find(GlModal);

      wrapper.find('button').trigger('click');

      await nextTick();
    });

    it('should emit delete with list on confirmation', async () => {
      modal.find('[data-testid="modal-confirm"]').trigger('click');

      await nextTick();
      expect(wrapper.emitted('delete')).toEqual([[userLists[0]]]);
    });

    it('should not emit delete with list when not confirmed', async () => {
      modal.find('button').trigger('click');

      await nextTick();
      expect(wrapper.emitted('delete')).toBeUndefined();
    });
  });
});