summaryrefslogtreecommitdiff
path: root/spec/frontend/editor/components/source_editor_toolbar_button_spec.js
blob: 5135091af4a458a3bb892eda440b17b7e10f76a5 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import createMockApollo from 'helpers/mock_apollo_helper';
import SourceEditorToolbarButton from '~/editor/components/source_editor_toolbar_button.vue';
import getToolbarItemQuery from '~/editor/graphql/get_item.query.graphql';
import updateToolbarItemMutation from '~/editor/graphql/update_item.mutation.graphql';
import { buildButton } from './helpers';

Vue.use(VueApollo);

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

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

  const createComponentWithApollo = ({ propsData } = {}) => {
    mockApollo = createMockApollo();
    mockApollo.clients.defaultClient.cache.writeQuery({
      query: getToolbarItemQuery,
      variables: { id: defaultBtn.id },
      data: {
        item: {
          ...defaultBtn,
        },
      },
    });

    wrapper = shallowMount(SourceEditorToolbarButton, {
      propsData,
      apolloProvider: mockApollo,
    });
  };

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

  describe('default', () => {
    const defaultProps = {
      category: 'primary',
      variant: 'default',
    };
    const customProps = {
      category: 'secondary',
      variant: 'info',
    };
    it('renders a default button without props', async () => {
      createComponentWithApollo();
      const btn = findButton();
      expect(btn.exists()).toBe(true);
      expect(btn.props()).toMatchObject(defaultProps);
    });

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

  describe('button updates', () => {
    it('it properly updates button on Apollo cache update', async () => {
      const { id } = defaultBtn;

      createComponentWithApollo({
        propsData: {
          button: {
            id,
          },
        },
      });

      expect(findButton().props('selected')).toBe(false);

      mockApollo.clients.defaultClient.cache.writeQuery({
        query: getToolbarItemQuery,
        variables: { id },
        data: {
          item: {
            ...defaultBtn,
            selected: true,
          },
        },
      });

      jest.runOnlyPendingTimers();
      await nextTick();

      expect(findButton().props('selected')).toBe(true);
    });
  });

  describe('click handler', () => {
    it('fires the click handler on the button when available', () => {
      const spy = jest.fn();
      createComponentWithApollo({
        propsData: {
          button: {
            onClick: spy,
          },
        },
      });
      expect(spy).not.toHaveBeenCalled();
      findButton().vm.$emit('click');
      expect(spy).toHaveBeenCalled();
    });
    it('emits the "click" event', () => {
      createComponentWithApollo();
      jest.spyOn(wrapper.vm, '$emit');
      expect(wrapper.vm.$emit).not.toHaveBeenCalled();
      findButton().vm.$emit('click');
      expect(wrapper.vm.$emit).toHaveBeenCalledWith('click');
    });
    it('triggers the mutation exposing the changed "selected" prop', () => {
      const { id } = defaultBtn;
      createComponentWithApollo({
        propsData: {
          button: {
            id,
          },
        },
      });
      jest.spyOn(wrapper.vm.$apollo, 'mutate');
      expect(wrapper.vm.$apollo.mutate).not.toHaveBeenCalled();
      findButton().vm.$emit('click');
      expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
        mutation: updateToolbarItemMutation,
        variables: {
          id,
          propsToUpdate: {
            selected: true,
          },
        },
      });
    });
  });
});