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

ci_variable_modal_spec.js « components « ci_variable_list « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70edd36669b2b0b30695a95f4a9dc527ad4a83b1 (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
import Vuex from 'vuex';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import { GlDeprecatedButton } from '@gitlab/ui';
import CiVariableModal from '~/ci_variable_list/components/ci_variable_modal.vue';
import createStore from '~/ci_variable_list/store';
import mockData from '../services/mock_data';
import ModalStub from '../stubs';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Ci variable modal', () => {
  let wrapper;
  let store;

  const createComponent = () => {
    store = createStore();
    wrapper = shallowMount(CiVariableModal, {
      stubs: {
        GlModal: ModalStub,
      },
      localVue,
      store,
    });
  };

  const findModal = () => wrapper.find(ModalStub);
  const addOrUpdateButton = index =>
    findModal()
      .findAll(GlDeprecatedButton)
      .at(index);
  const deleteVariableButton = () =>
    findModal()
      .findAll(GlDeprecatedButton)
      .at(1);

  beforeEach(() => {
    createComponent();
    jest.spyOn(store, 'dispatch').mockImplementation();
  });

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

  it('button is disabled when no key/value pair are present', () => {
    expect(addOrUpdateButton(1).attributes('disabled')).toBeTruthy();
  });

  describe('Adding a new variable', () => {
    beforeEach(() => {
      const [variable] = mockData.mockVariables;
      store.state.variable = variable;
    });

    it('button is enabled when key/value pair are present', () => {
      expect(addOrUpdateButton(1).attributes('disabled')).toBeFalsy();
    });

    it('Add variable button dispatches addVariable action', () => {
      addOrUpdateButton(1).vm.$emit('click');
      expect(store.dispatch).toHaveBeenCalledWith('addVariable');
    });

    it('Clears the modal state once modal is hidden', () => {
      findModal().vm.$emit('hidden');
      expect(store.dispatch).toHaveBeenCalledWith('clearModal');
    });
  });

  describe('Editing a variable', () => {
    beforeEach(() => {
      const [variable] = mockData.mockVariables;
      store.state.variableBeingEdited = variable;
    });

    it('button text is Update variable when updating', () => {
      expect(addOrUpdateButton(2).text()).toBe('Update variable');
    });

    it('Update variable button dispatches updateVariable with correct variable', () => {
      addOrUpdateButton(2).vm.$emit('click');
      expect(store.dispatch).toHaveBeenCalledWith(
        'updateVariable',
        store.state.variableBeingEdited,
      );
    });

    it('Resets the editing state once modal is hidden', () => {
      findModal().vm.$emit('hidden');
      expect(store.dispatch).toHaveBeenCalledWith('resetEditing');
    });

    it('dispatches deleteVariable with correct variable to delete', () => {
      deleteVariableButton().vm.$emit('click');
      expect(store.dispatch).toHaveBeenCalledWith('deleteVariable', mockData.mockVariables[0]);
    });
  });
});