summaryrefslogtreecommitdiff
path: root/spec/frontend/add_context_commits_modal/components/add_context_commits_modal_spec.js
blob: bffadbde087fbb7600d0cb48a5aba867db51e485 (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
import { GlModal, GlSearchBoxByType } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import getDiffWithCommit from 'test_fixtures/merge_request_diffs/with_commit.json';
import AddReviewItemsModal from '~/add_context_commits_modal/components/add_context_commits_modal_wrapper.vue';

import * as actions from '~/add_context_commits_modal/store/actions';
import mutations from '~/add_context_commits_modal/store/mutations';
import defaultState from '~/add_context_commits_modal/store/state';

Vue.use(Vuex);

describe('AddContextCommitsModal', () => {
  let wrapper;
  let store;
  const createContextCommits = jest.fn();
  const removeContextCommits = jest.fn();
  const resetModalState = jest.fn();
  const searchCommits = jest.fn();
  const { commit } = getDiffWithCommit;

  const createWrapper = (props = {}) => {
    store = new Vuex.Store({
      mutations,
      state: {
        ...defaultState(),
      },
      actions: {
        ...actions,
        searchCommits,
        createContextCommits,
        removeContextCommits,
        resetModalState,
      },
    });

    wrapper = shallowMount(AddReviewItemsModal, {
      store,
      propsData: {
        contextCommitsPath: '',
        targetBranch: 'main',
        mergeRequestIid: 1,
        projectId: 1,
        ...props,
      },
    });
    return wrapper;
  };

  const findModal = () => wrapper.find(GlModal);
  const findSearch = () => wrapper.find(GlSearchBoxByType);

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

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

  it('renders modal with 2 tabs', () => {
    expect(wrapper.element).toMatchSnapshot();
  });

  it('an ok button labeled "Save changes"', () => {
    expect(findModal().attributes('ok-title')).toEqual('Save changes');
  });

  describe('when in first tab, renders a modal with', () => {
    it('renders the search box component', () => {
      expect(findSearch().exists()).toBe(true);
    });

    it('when user starts entering text in search box, it calls action "searchCommits" after waiting for 500s', () => {
      const searchText = 'abcd';
      findSearch().vm.$emit('input', searchText);
      expect(searchCommits).not.toBeCalled();
      jest.advanceTimersByTime(500);
      expect(searchCommits).toHaveBeenCalledWith(expect.anything(), searchText);
    });

    it('disabled ok button when no row is selected', () => {
      expect(findModal().attributes('ok-disabled')).toBe('true');
    });

    it('enabled ok button when atleast one row is selected', async () => {
      wrapper.vm.$store.state.selectedCommits = [{ ...commit, isSelected: true }];
      await nextTick();
      expect(findModal().attributes('ok-disabled')).toBe(undefined);
    });
  });

  describe('when in second tab, renders a modal with', () => {
    beforeEach(() => {
      wrapper.vm.$store.state.tabIndex = 1;
    });
    it('a disabled ok button when no row is selected', () => {
      expect(findModal().attributes('ok-disabled')).toBe('true');
    });

    it('an enabled ok button when atleast one row is selected', async () => {
      wrapper.vm.$store.state.selectedCommits = [{ ...commit, isSelected: true }];
      await nextTick();
      expect(findModal().attributes('ok-disabled')).toBe(undefined);
    });

    it('a disabled ok button in first tab, when row is selected in second tab', () => {
      createWrapper({ selectedContextCommits: [commit] });
      expect(wrapper.find(GlModal).attributes('ok-disabled')).toBe('true');
    });
  });

  describe('has an ok button when clicked calls action', () => {
    it('"createContextCommits" when only new commits to be added ', async () => {
      wrapper.vm.$store.state.selectedCommits = [{ ...commit, isSelected: true }];
      findModal().vm.$emit('ok');
      await nextTick();
      expect(createContextCommits).toHaveBeenCalledWith(expect.anything(), {
        commits: [{ ...commit, isSelected: true }],
        forceReload: true,
      });
    });
    it('"removeContextCommits" when only added commits are to be removed ', async () => {
      wrapper.vm.$store.state.toRemoveCommits = [commit.short_id];
      findModal().vm.$emit('ok');
      await nextTick();
      expect(removeContextCommits).toHaveBeenCalledWith(expect.anything(), true);
    });
    it('"createContextCommits" and "removeContextCommits" when new commits are to be added and old commits are to be removed', async () => {
      wrapper.vm.$store.state.selectedCommits = [{ ...commit, isSelected: true }];
      wrapper.vm.$store.state.toRemoveCommits = [commit.short_id];
      findModal().vm.$emit('ok');
      await nextTick();
      expect(createContextCommits).toHaveBeenCalledWith(expect.anything(), {
        commits: [{ ...commit, isSelected: true }],
      });
      expect(removeContextCommits).toHaveBeenCalledWith(expect.anything(), undefined);
    });
  });

  describe('has a cancel button when clicked', () => {
    it('does not call "createContextCommits" or "removeContextCommits"', () => {
      findModal().vm.$emit('cancel');
      expect(createContextCommits).not.toHaveBeenCalled();
      expect(removeContextCommits).not.toHaveBeenCalled();
    });
    it('"resetModalState" to reset all the modal state', () => {
      findModal().vm.$emit('cancel');
      expect(resetModalState).toHaveBeenCalledWith(expect.anything(), undefined);
    });
  });

  describe('when model is closed by clicking the "X" button or by pressing "ESC" key', () => {
    it('does not call "createContextCommits" or "removeContextCommits"', () => {
      findModal().vm.$emit('close');
      expect(createContextCommits).not.toHaveBeenCalled();
      expect(removeContextCommits).not.toHaveBeenCalled();
    });
    it('"resetModalState" to reset all the modal state', () => {
      findModal().vm.$emit('close');
      expect(resetModalState).toHaveBeenCalledWith(expect.anything(), undefined);
    });
  });
});