summaryrefslogtreecommitdiff
path: root/spec/frontend/boards/components/board_new_item_spec.js
blob: 86cebc8a7192eb0a82f32cd5f840ae5f208d4738 (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
import { GlForm, GlFormInput, GlButton } from '@gitlab/ui';
import { nextTick } from 'vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';

import BoardNewItem from '~/boards/components/board_new_item.vue';
import eventHub from '~/boards/eventhub';

import { mockList } from '../mock_data';

const createComponent = ({
  list = mockList,
  formEventPrefix = 'toggle-issue-form-',
  disabledSubmit = false,
  submitButtonTitle = 'Create item',
} = {}) =>
  mountExtended(BoardNewItem, {
    propsData: {
      list,
      formEventPrefix,
      disabledSubmit,
      submitButtonTitle,
    },
    slots: {
      default: '<div id="default-slot"></div>',
    },
    stubs: {
      GlForm,
    },
  });

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

  beforeEach(() => {
    wrapper = createComponent();
  });

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

  describe('template', () => {
    describe('when the user provides a valid input', () => {
      it('finds an enabled create button', async () => {
        expect(wrapper.findByTestId('create-button').props('disabled')).toBe(true);

        wrapper.find(GlFormInput).vm.$emit('input', 'hello');
        await nextTick();

        expect(wrapper.findByTestId('create-button').props('disabled')).toBe(false);
      });
    });

    describe('when the user types in a string with only spaces', () => {
      it('disables the Create Issue button', async () => {
        wrapper.find(GlFormInput).vm.$emit('input', '    ');

        await nextTick();

        expect(wrapper.findByTestId('create-button').props('disabled')).toBe(true);
      });
    });

    it('renders gl-form component', () => {
      expect(wrapper.findComponent(GlForm).exists()).toBe(true);
    });

    it('renders field label', () => {
      expect(wrapper.find('label').exists()).toBe(true);
      expect(wrapper.find('label').text()).toBe('Title');
    });

    it('renders gl-form-input field', () => {
      expect(wrapper.findComponent(GlFormInput).exists()).toBe(true);
    });

    it('renders default slot contents', () => {
      expect(wrapper.find('#default-slot').exists()).toBe(true);
    });

    it('renders submit and cancel buttons', () => {
      const buttons = wrapper.findAllComponents(GlButton);
      expect(buttons).toHaveLength(2);
      expect(buttons.at(0).text()).toBe('Create item');
      expect(buttons.at(1).text()).toBe('Cancel');
    });

    describe('events', () => {
      const glForm = () => wrapper.findComponent(GlForm);
      const titleInput = () => wrapper.find('input[name="issue_title"]');

      it('emits `form-submit` event with title value when `submit` is triggered on gl-form', async () => {
        titleInput().setValue('Foo');
        await glForm().trigger('submit');

        expect(wrapper.emitted('form-submit')).toBeTruthy();
        expect(wrapper.emitted('form-submit')[0]).toEqual([
          {
            title: 'Foo',
            list: mockList,
          },
        ]);
      });

      it('emits `form-submit` event with trimmed title', async () => {
        titleInput().setValue(' Foo   ');

        await glForm().trigger('submit');

        expect(wrapper.emitted('form-submit')[0]).toEqual([
          {
            title: 'Foo',
            list: mockList,
          },
        ]);
      });

      it('emits `scroll-board-list-` event with list.id on eventHub when `submit` is triggered on gl-form', async () => {
        jest.spyOn(eventHub, '$emit').mockImplementation();
        await glForm().trigger('submit');

        expect(eventHub.$emit).toHaveBeenCalledWith(`scroll-board-list-${mockList.id}`);
      });

      it('emits `form-cancel` event and clears title value when `reset` is triggered on gl-form', async () => {
        titleInput().setValue('Foo');

        await nextTick();
        expect(titleInput().element.value).toBe('Foo');

        await glForm().trigger('reset');

        expect(titleInput().element.value).toBe('');
        expect(wrapper.emitted('form-cancel')).toBeTruthy();
      });
    });
  });
});