summaryrefslogtreecommitdiff
path: root/spec/frontend/issues/show/components/incidents/timeline_events_form_spec.js
blob: d5b199cc7900fb4f7f996cfc0b56db78c014daee (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import VueApollo from 'vue-apollo';
import Vue, { nextTick } from 'vue';
import { GlDatepicker } from '@gitlab/ui';
import { shallowMountExtended, mountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import TimelineEventsForm from '~/issues/show/components/incidents/timeline_events_form.vue';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import { timelineFormI18n } from '~/issues/show/components/incidents/constants';
import { createAlert } from '~/flash';
import { useFakeDate } from 'helpers/fake_date';

Vue.use(VueApollo);

jest.mock('~/flash');

const fakeDate = '2020-07-08T00:00:00.000Z';

const mockInputDate = new Date('2021-08-12');

describe('Timeline events form', () => {
  // July 8 2020
  useFakeDate(fakeDate);
  let wrapper;

  const mountComponent = ({ mountMethod = shallowMountExtended } = {}, props = {}) => {
    wrapper = mountMethod(TimelineEventsForm, {
      propsData: {
        showSaveAndAdd: true,
        isEventProcessed: false,
        ...props,
      },
      stubs: {
        GlButton: true,
      },
    });
  };

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

  const findMarkdownField = () => wrapper.findComponent(MarkdownField);
  const findSubmitButton = () => wrapper.findByText(timelineFormI18n.save);
  const findSubmitAndAddButton = () => wrapper.findByText(timelineFormI18n.saveAndAdd);
  const findCancelButton = () => wrapper.findByText(timelineFormI18n.cancel);
  const findDeleteButton = () => wrapper.findByText(timelineFormI18n.delete);
  const findDatePicker = () => wrapper.findComponent(GlDatepicker);
  const findHourInput = () => wrapper.findByTestId('input-hours');
  const findMinuteInput = () => wrapper.findByTestId('input-minutes');
  const setDatetime = () => {
    findDatePicker().vm.$emit('input', mockInputDate);
    findHourInput().setValue(5);
    findMinuteInput().setValue(45);
  };
  const findTextarea = () => wrapper.findByTestId('input-note');
  const findCountNumeric = (count) => wrapper.findByText(count);
  const findCountVerbose = (count) => wrapper.findByText(`${count} characters remaining`);
  const findCountHint = () => wrapper.findByText(timelineFormI18n.hint);

  const submitForm = async () => {
    findSubmitButton().vm.$emit('click');
    await waitForPromises();
  };
  const submitFormAndAddAnother = async () => {
    findSubmitAndAddButton().vm.$emit('click');
    await waitForPromises();
  };
  const cancelForm = async () => {
    findCancelButton().vm.$emit('click');
    await waitForPromises();
  };
  const deleteForm = () => {
    findDeleteButton().vm.$emit('click');
  };

  it('renders markdown-field component with correct list of toolbar items', () => {
    mountComponent({ mountMethod: mountExtended });

    expect(findMarkdownField().props('restrictedToolBarItems')).toEqual([
      'quote',
      'strikethrough',
      'bullet-list',
      'numbered-list',
      'task-list',
      'collapsible-section',
      'table',
      'attach-file',
      'full-screen',
    ]);
  });

  describe('form button behaviour', () => {
    beforeEach(() => {
      mountComponent({ mountMethod: mountExtended });
    });

    it('should save event on submit', async () => {
      await submitForm();

      expect(wrapper.emitted()).toEqual({
        'save-event': [[{ note: '', occurredAt: fakeDate }, false]],
      });
    });

    it('should save event on "submit and add another"', async () => {
      await submitFormAndAddAnother();
      expect(wrapper.emitted()).toEqual({
        'save-event': [[{ note: '', occurredAt: fakeDate }, true]],
      });
    });

    it('should emit cancel on cancel', async () => {
      await cancelForm();
      expect(wrapper.emitted()).toEqual({ cancel: [[]] });
    });

    it('should clear the form', async () => {
      setDatetime();
      await nextTick();

      expect(findDatePicker().props('value')).toBe(mockInputDate);
      expect(findHourInput().element.value).toBe('5');
      expect(findMinuteInput().element.value).toBe('45');

      wrapper.vm.clear();
      await nextTick();

      expect(findDatePicker().props('value')).toStrictEqual(new Date(fakeDate));
      expect(findHourInput().element.value).toBe('0');
      expect(findMinuteInput().element.value).toBe('0');
    });

    it('should disable the save buttons when event content does not exist', async () => {
      expect(findSubmitButton().props('disabled')).toBe(true);
      expect(findSubmitAndAddButton().props('disabled')).toBe(true);
    });

    it('should enable the save buttons when event content exists', async () => {
      await findTextarea().setValue('hello');

      expect(findSubmitButton().props('disabled')).toBe(false);
      expect(findSubmitAndAddButton().props('disabled')).toBe(false);
    });
  });

  describe('form character limit', () => {
    beforeEach(() => {
      mountComponent({ mountMethod: mountExtended });
    });

    it('sets a character limit hint', () => {
      expect(findCountHint().exists()).toBe(true);
    });

    it('sets a character limit when text is entered', async () => {
      await findTextarea().setValue('hello');

      expect(findCountNumeric('275').text()).toBe('275');
      expect(findCountVerbose('275').text()).toBe('275 characters remaining');
    });

    it('prevents form submission when text is beyond maximum length', async () => {
      // 281 characters long
      const longText =
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in volupte';
      await findTextarea().setValue(longText);

      expect(findSubmitButton().props('disabled')).toBe(true);
      expect(findSubmitAndAddButton().props('disabled')).toBe(true);
    });
  });

  describe('Delete button', () => {
    it('does not show the delete button if showDelete prop is false', () => {
      mountComponent({ mountMethod: mountExtended }, { showDelete: false });

      expect(findDeleteButton().exists()).toBe(false);
    });

    it('shows the delete button if showDelete prop is true', () => {
      mountComponent({ mountMethod: mountExtended }, { showDelete: true });

      expect(findDeleteButton().exists()).toBe(true);
    });

    it('disables the delete button if isEventProcessed prop is true', () => {
      mountComponent({ mountMethod: mountExtended }, { showDelete: true, isEventProcessed: true });

      expect(findDeleteButton().props('disabled')).toBe(true);
    });

    it('does not disable the delete button if isEventProcessed prop is false', () => {
      mountComponent({ mountMethod: mountExtended }, { showDelete: true, isEventProcessed: false });

      expect(findDeleteButton().props('disabled')).toBe(false);
    });

    it('emits delete event on click', () => {
      mountComponent({ mountMethod: mountExtended }, { showDelete: true, isEventProcessed: true });

      deleteForm();

      expect(wrapper.emitted('delete')).toEqual([[]]);
    });
  });
});