summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/environment_delete_spec.js
blob: 057cb9858c4ea251704fe9b8bff6f1a81d96606c (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
import { GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import setEnvironmentToDelete from '~/environments/graphql/mutations/set_environment_to_delete.mutation.graphql';
import DeleteComponent from '~/environments/components/environment_delete.vue';
import eventHub from '~/environments/event_hub';
import createMockApollo from 'helpers/mock_apollo_helper';
import { resolvedEnvironment } from './graphql/mock_data';

describe('External URL Component', () => {
  let wrapper;

  const createWrapper = (props = {}, options = {}) => {
    wrapper = shallowMount(DeleteComponent, {
      ...options,
      propsData: {
        environment: resolvedEnvironment,
        ...props,
      },
    });
  };

  const findDropdownItem = () => wrapper.find(GlDropdownItem);

  describe('event hub', () => {
    beforeEach(() => {
      createWrapper();
    });

    it('should render a dropdown item to delete the environment', () => {
      expect(findDropdownItem().exists()).toBe(true);
      expect(wrapper.text()).toEqual('Delete environment');
      expect(findDropdownItem().attributes('variant')).toBe('danger');
    });

    it('emits requestDeleteEnvironment in the event hub when button is clicked', () => {
      jest.spyOn(eventHub, '$emit');
      findDropdownItem().vm.$emit('click');
      expect(eventHub.$emit).toHaveBeenCalledWith('requestDeleteEnvironment', resolvedEnvironment);
    });
  });

  describe('graphql', () => {
    Vue.use(VueApollo);
    let mockApollo;

    beforeEach(() => {
      mockApollo = createMockApollo();
      createWrapper(
        { graphql: true, environment: resolvedEnvironment },
        { apolloProvider: mockApollo },
      );
    });

    it('should render a dropdown item to delete the environment', () => {
      expect(findDropdownItem().exists()).toBe(true);
      expect(wrapper.text()).toEqual('Delete environment');
      expect(findDropdownItem().attributes('variant')).toBe('danger');
    });

    it('emits requestDeleteEnvironment in the event hub when button is clicked', () => {
      jest.spyOn(mockApollo.defaultClient, 'mutate');
      findDropdownItem().vm.$emit('click');
      expect(mockApollo.defaultClient.mutate).toHaveBeenCalledWith({
        mutation: setEnvironmentToDelete,
        variables: { environment: resolvedEnvironment },
      });
    });
  });
});