summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/todo_spec.js
blob: 5bbb42d402d8bbfc3bfe16d46d033288618f7f94 (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
import { shallowMount } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui';

import SidebarTodos from '~/sidebar/components/todo_toggle/todo.vue';
import Icon from '~/vue_shared/components/icon.vue';

const defaultProps = {
  issuableId: 1,
  issuableType: 'epic',
};

describe('SidebarTodo', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(SidebarTodos, {
      sync: false,
      propsData: {
        ...defaultProps,
        ...props,
      },
    });
  };

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

  it.each`
    state    | classes
    ${false} | ${['btn', 'btn-default', 'btn-todo', 'issuable-header-btn', 'float-right']}
    ${true}  | ${['btn-blank', 'btn-todo', 'sidebar-collapsed-icon', 'dont-change-state']}
  `('returns todo button classes for when `collapsed` prop is `$state`', ({ state, classes }) => {
    createComponent({ collapsed: state });
    expect(wrapper.find('button').classes()).toStrictEqual(classes);
  });

  it.each`
    isTodo   | iconClass        | label             | icon
    ${false} | ${''}            | ${'Add a To Do'}  | ${'todo-add'}
    ${true}  | ${'todo-undone'} | ${'Mark as done'} | ${'todo-done'}
  `(
    'renders proper button when `isTodo` prop is `$isTodo`',
    ({ isTodo, iconClass, label, icon }) => {
      createComponent({ isTodo });

      expect(
        wrapper
          .find(Icon)
          .classes()
          .join(' '),
      ).toStrictEqual(iconClass);
      expect(wrapper.find(Icon).props('name')).toStrictEqual(icon);
      expect(wrapper.find('button').text()).toBe(label);
    },
  );

  describe('template', () => {
    it('emits `toggleTodo` event when clicked on button', () => {
      createComponent();
      wrapper.find('button').trigger('click');

      expect(wrapper.emitted().toggleTodo).toBeTruthy();
    });

    it('renders component container element with proper data attributes', () => {
      createComponent({
        issuableId: 1,
        issuableType: 'epic',
      });

      expect(wrapper.element).toMatchSnapshot();
    });

    it('renders button label element when `collapsed` prop is `false`', () => {
      createComponent({ collapsed: false });

      expect(wrapper.find('span.issuable-todo-inner').text()).toBe('Mark as done');
    });

    it('renders button icon when `collapsed` prop is `true`', () => {
      createComponent({ collapsed: true });

      expect(wrapper.find(Icon).props('name')).toBe('todo-done');
    });

    it('renders loading icon when `isActionActive` prop is true', () => {
      createComponent({ isActionActive: true });

      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });

    it('hides button icon when `isActionActive` prop is true', () => {
      createComponent({ collapsed: true, isActionActive: true });

      expect(wrapper.find(Icon).isVisible()).toBe(false);
    });
  });
});