summaryrefslogtreecommitdiff
path: root/spec/frontend/deploy_keys/components/app_spec.js
blob: 291502c9ed7758cfa5fee901ffa980c9c939704a (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
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'spec/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import eventHub from '~/deploy_keys/eventhub';
import deployKeysApp from '~/deploy_keys/components/app.vue';

const TEST_ENDPOINT = `${TEST_HOST}/dummy/`;

describe('Deploy keys app component', () => {
  const data = getJSONFixture('deploy_keys/keys.json');
  let wrapper;
  let mock;

  const mountComponent = () => {
    wrapper = mount(deployKeysApp, {
      propsData: {
        endpoint: TEST_ENDPOINT,
        projectId: '8',
      },
    });

    return waitForPromises();
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
    mock.onGet(TEST_ENDPOINT).reply(200, data);
  });

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

  const findLoadingIcon = () => wrapper.find('.gl-spinner');
  const findKeyPanels = () => wrapper.findAll('.deploy-keys .nav-links li');

  it('renders loading icon while waiting for request', () => {
    mock.onGet(TEST_ENDPOINT).reply(() => new Promise());

    mountComponent();

    return wrapper.vm.$nextTick().then(() => {
      expect(findLoadingIcon().exists()).toBe(true);
    });
  });

  it('renders keys panels', () => {
    return mountComponent().then(() => {
      expect(findKeyPanels().length).toBe(3);
    });
  });

  it.each`
    selector                                       | label                                 | count
    ${'.js-deployKeys-tab-enabled_keys'}           | ${'Enabled deploy keys'}              | ${1}
    ${'.js-deployKeys-tab-available_project_keys'} | ${'Privately accessible deploy keys'} | ${0}
    ${'.js-deployKeys-tab-public_keys'}            | ${'Publicly accessible deploy keys'}  | ${1}
  `('$selector title is $label with keys count equal to $count', ({ selector, label, count }) => {
    return mountComponent().then(() => {
      const element = wrapper.find(selector);
      expect(element.exists()).toBe(true);
      expect(element.text().trim()).toContain(label);

      expect(
        element
          .find('.badge')
          .text()
          .trim(),
      ).toBe(count.toString());
    });
  });

  it('does not render key panels when keys object is empty', () => {
    mock.onGet(TEST_ENDPOINT).reply(200, []);

    return mountComponent().then(() => {
      expect(findKeyPanels().length).toBe(0);
    });
  });

  it('re-fetches deploy keys when enabling a key', () => {
    const key = data.public_keys[0];
    return mountComponent()
      .then(() => {
        jest.spyOn(wrapper.vm.service, 'getKeys').mockImplementation(() => {});
        jest.spyOn(wrapper.vm.service, 'enableKey').mockImplementation(() => Promise.resolve());

        eventHub.$emit('enable.key', key);

        return wrapper.vm.$nextTick();
      })
      .then(() => {
        expect(wrapper.vm.service.enableKey).toHaveBeenCalledWith(key.id);
        expect(wrapper.vm.service.getKeys).toHaveBeenCalled();
      });
  });

  it('re-fetches deploy keys when disabling a key', () => {
    const key = data.public_keys[0];
    return mountComponent()
      .then(() => {
        jest.spyOn(window, 'confirm').mockReturnValue(true);
        jest.spyOn(wrapper.vm.service, 'getKeys').mockImplementation(() => {});
        jest.spyOn(wrapper.vm.service, 'disableKey').mockImplementation(() => Promise.resolve());

        eventHub.$emit('disable.key', key);

        return wrapper.vm.$nextTick();
      })
      .then(() => {
        expect(wrapper.vm.service.disableKey).toHaveBeenCalledWith(key.id);
        expect(wrapper.vm.service.getKeys).toHaveBeenCalled();
      });
  });

  it('calls disableKey when removing a key', () => {
    const key = data.public_keys[0];
    return mountComponent()
      .then(() => {
        jest.spyOn(window, 'confirm').mockReturnValue(true);
        jest.spyOn(wrapper.vm.service, 'getKeys').mockImplementation(() => {});
        jest.spyOn(wrapper.vm.service, 'disableKey').mockImplementation(() => Promise.resolve());

        eventHub.$emit('remove.key', key);

        return wrapper.vm.$nextTick();
      })
      .then(() => {
        expect(wrapper.vm.service.disableKey).toHaveBeenCalledWith(key.id);
        expect(wrapper.vm.service.getKeys).toHaveBeenCalled();
      });
  });

  it('hasKeys returns true when there are keys', () => {
    return mountComponent().then(() => {
      expect(wrapper.vm.hasKeys).toEqual(3);
    });
  });
});