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

actions_button_spec.js « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f9a27c6997ae9f35f92373cecbbe0720a69d1f4 (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
import {
  GlDisclosureDropdown,
  GlDisclosureDropdownGroup,
  GlDisclosureDropdownItem,
} from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ActionsButton from '~/vue_shared/components/actions_button.vue';

const TEST_ACTION = {
  key: 'action1',
  text: 'Sample',
  secondaryText: 'Lorem ipsum.',
  href: '/sample',
  attrs: {
    'data-test': '123',
    category: 'secondary',
    href: '/sample',
    variant: 'default',
  },
  handle: jest.fn(),
};
const TEST_ACTION_2 = {
  key: 'action2',
  text: 'Sample 2',
  secondaryText: 'Dolar sit amit.',
  href: '#',
  attrs: { 'data-test': '456' },
  handle: jest.fn(),
};

describe('vue_shared/components/actions_button', () => {
  let wrapper;

  function createComponent({ props = {}, slots = {} } = {}) {
    wrapper = shallowMountExtended(ActionsButton, {
      propsData: { actions: [TEST_ACTION, TEST_ACTION_2], toggleText: 'Edit', ...props },
      stubs: {
        GlDisclosureDropdownItem,
      },
      slots,
    });
  }
  const findDropdown = () => wrapper.findComponent(GlDisclosureDropdown);

  it('dropdown toggle displays provided toggleLabel', () => {
    createComponent();

    expect(findDropdown().props().toggleText).toBe('Edit');
  });

  it('dropdown has a fluid width', () => {
    createComponent();

    expect(findDropdown().props().fluidWidth).toBe(true);
  });

  it('provides a default slot', () => {
    const slotContent = 'default text';

    createComponent({
      slots: {
        default: slotContent,
      },
    });

    expect(findDropdown().text()).toContain(slotContent);
  });

  it('allows customizing variant and category', () => {
    const variant = 'confirm';
    const category = 'secondary';

    createComponent({ props: { variant, category } });

    expect(findDropdown().props()).toMatchObject({ category, variant });
  });

  it('displays a single dropdown group', () => {
    createComponent();

    expect(wrapper.findAllComponents(GlDisclosureDropdownGroup)).toHaveLength(1);
  });

  it('create dropdown items for every action', () => {
    createComponent();

    [TEST_ACTION, TEST_ACTION_2].forEach((action, index) => {
      const dropdownItem = wrapper.findAllComponents(GlDisclosureDropdownItem).at(index);

      expect(dropdownItem.props().item).toBe(action);
      expect(dropdownItem.attributes()).toMatchObject(action.attrs);
      expect(dropdownItem.text()).toContain(action.text);
      expect(dropdownItem.text()).toContain(action.secondaryText);
    });
  });

  describe('when clicking a dropdown item', () => {
    it("invokes the action's handle method", () => {
      createComponent();

      [TEST_ACTION, TEST_ACTION_2].forEach((action, index) => {
        const dropdownItem = wrapper.findAllComponents(GlDisclosureDropdownItem).at(index);

        dropdownItem.vm.$emit('action');

        expect(action.handle).toHaveBeenCalled();
      });
    });
  });

  it.each(['shown', 'hidden'])(
    'bubbles up %s event from the disclosure dropdown component',
    (event) => {
      createComponent();
      findDropdown().vm.$emit(event);
      expect(wrapper.emitted(event)).toHaveLength(1);
    },
  );
});