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

source_editor_toolbar_button_spec.js « components « editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1475d451ab3129d0e31c7901a1fc693c99494139 (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
import { nextTick } from 'vue';
import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import SourceEditorToolbarButton from '~/editor/components/source_editor_toolbar_button.vue';
import { buildButton } from './helpers';

describe('Source Editor Toolbar button', () => {
  let wrapper;
  const defaultBtn = buildButton();

  const findButton = () => wrapper.findComponent(GlButton);

  const createComponent = (props = { button: defaultBtn }) => {
    wrapper = shallowMount(SourceEditorToolbarButton, {
      propsData: {
        ...props,
      },
    });
  };

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

  describe('default', () => {
    const defaultProps = {
      category: 'primary',
      variant: 'default',
    };
    const customProps = {
      category: 'secondary',
      variant: 'info',
    };

    it('does not render the button if the props have not been passed', () => {
      createComponent({});
      expect(findButton().vm).toBeUndefined();
    });

    it('renders a default button without props', async () => {
      createComponent();
      const btn = findButton();
      expect(btn.exists()).toBe(true);
      expect(btn.props()).toMatchObject(defaultProps);
    });

    it('renders a button based on the props passed', async () => {
      createComponent({
        button: customProps,
      });
      const btn = findButton();
      expect(btn.props()).toMatchObject(customProps);
    });
  });

  describe('click handler', () => {
    it('fires the click handler on the button when available', async () => {
      const spy = jest.fn();
      createComponent({
        button: {
          onClick: spy,
        },
      });
      expect(spy).not.toHaveBeenCalled();
      findButton().vm.$emit('click');

      await nextTick();
      expect(spy).toHaveBeenCalled();
    });
    it('emits the "click" event', async () => {
      createComponent();
      jest.spyOn(wrapper.vm, '$emit');
      expect(wrapper.vm.$emit).not.toHaveBeenCalled();

      findButton().vm.$emit('click');
      await nextTick();

      expect(wrapper.vm.$emit).toHaveBeenCalledWith('click');
    });
  });
});