summaryrefslogtreecommitdiff
path: root/spec/frontend/ci_variable_list/components/legacy_ci_variable_table_spec.js
blob: 310afc8003a99f53e009b6873dc459678b44bd6d (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
import Vue from 'vue';
import Vuex from 'vuex';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import LegacyCiVariableTable from '~/ci_variable_list/components/legacy_ci_variable_table.vue';
import createStore from '~/ci_variable_list/store';
import mockData from '../services/mock_data';

Vue.use(Vuex);

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

  const createComponent = () => {
    store = createStore();
    jest.spyOn(store, 'dispatch').mockImplementation();
    wrapper = mountExtended(LegacyCiVariableTable, {
      attachTo: document.body,
      store,
    });
  };

  const findRevealButton = () => wrapper.findByText('Reveal values');
  const findEditButton = () => wrapper.findByLabelText('Edit');
  const findEmptyVariablesPlaceholder = () => wrapper.findByText('There are no variables yet.');

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

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

  it('dispatches fetchVariables when mounted', () => {
    expect(store.dispatch).toHaveBeenCalledWith('fetchVariables');
  });

  describe('When table is empty', () => {
    beforeEach(() => {
      store.state.variables = [];
    });

    it('displays empty message', () => {
      expect(findEmptyVariablesPlaceholder().exists()).toBe(true);
    });

    it('hides the reveal button', () => {
      expect(findRevealButton().exists()).toBe(false);
    });
  });

  describe('When table has variables', () => {
    beforeEach(() => {
      store.state.variables = mockData.mockVariables;
    });

    it('does not display the empty message', () => {
      expect(findEmptyVariablesPlaceholder().exists()).toBe(false);
    });

    it('displays the reveal button', () => {
      expect(findRevealButton().exists()).toBe(true);
    });

    it('displays the correct amount of variables', async () => {
      expect(wrapper.findAll('.js-ci-variable-row')).toHaveLength(1);
    });
  });

  describe('Table click actions', () => {
    beforeEach(() => {
      store.state.variables = mockData.mockVariables;
    });

    it('reveals secret values when button is clicked', () => {
      findRevealButton().trigger('click');
      expect(store.dispatch).toHaveBeenCalledWith('toggleValues', false);
    });

    it('dispatches editVariable with correct variable to edit', () => {
      findEditButton().trigger('click');
      expect(store.dispatch).toHaveBeenCalledWith('editVariable', mockData.mockVariables[0]);
    });
  });
});