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

todo_button_spec.js « sidebar « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01958a144ed076d6d764026eb7327752cf079a4c (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
import { GlButton } from '@gitlab/ui';
import { shallowMount, mount } from '@vue/test-utils';
import TodoButton from '~/vue_shared/components/sidebar/todo_toggle/todo_button.vue';

describe('Todo Button', () => {
  let wrapper;
  let dispatchEventSpy;

  const createComponent = (props = {}, mountFn = shallowMount) => {
    wrapper = mountFn(TodoButton, {
      propsData: {
        ...props,
      },
    });
  };

  beforeEach(() => {
    dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
    jest.spyOn(document, 'querySelector').mockReturnValue({
      innerText: 2,
    });
  });

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

  it('renders GlButton', () => {
    createComponent();

    expect(wrapper.findComponent(GlButton).exists()).toBe(true);
  });

  it('emits click event when clicked', () => {
    createComponent({}, mount);
    wrapper.findComponent(GlButton).trigger('click');

    expect(wrapper.emitted().click).toHaveLength(1);
  });

  it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => {
    createComponent({}, mount);
    wrapper.findComponent(GlButton).trigger('click');
    const dispatchedEvent = dispatchEventSpy.mock.calls[0][0];

    expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
    expect(dispatchedEvent.detail).toEqual({ count: 1 });
    expect(dispatchedEvent.type).toBe('todo:toggle');
  });

  it.each`
    label             | isTodo
    ${'Mark as done'} | ${true}
    ${'Add a to do'}  | ${false}
  `('sets correct label when isTodo is $isTodo', ({ label, isTodo }) => {
    createComponent({ isTodo });

    expect(wrapper.findComponent(GlButton).text()).toBe(label);
  });

  it('binds additional props to GlButton', () => {
    createComponent({ loading: true });

    expect(wrapper.findComponent(GlButton).props('loading')).toBe(true);
  });
});