summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/explorer/components/delete_image_spec.js
blob: 9a0d070e42bfd6f066f6aa3c352da23e7d92b233 (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
import { shallowMount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import component from '~/registry/explorer/components/delete_image.vue';
import { GRAPHQL_PAGE_SIZE } from '~/registry/explorer/constants/index';
import deleteContainerRepositoryMutation from '~/registry/explorer/graphql/mutations/delete_container_repository.mutation.graphql';
import getContainerRepositoryDetailsQuery from '~/registry/explorer/graphql/queries/get_container_repository_details.query.graphql';

describe('Delete Image', () => {
  let wrapper;
  const id = '1';
  const storeMock = {
    readQuery: jest.fn().mockReturnValue({
      containerRepository: {
        status: 'foo',
      },
    }),
    writeQuery: jest.fn(),
  };

  const updatePayload = {
    data: {
      destroyContainerRepository: {
        containerRepository: {
          status: 'baz',
        },
      },
    },
  };

  const findButton = () => wrapper.find('button');

  const mountComponent = ({
    propsData = { id },
    mutate = jest.fn().mockResolvedValue({}),
  } = {}) => {
    wrapper = shallowMount(component, {
      propsData,
      mocks: {
        $apollo: {
          mutate,
        },
      },
      scopedSlots: {
        default: '<button @click="props.doDelete">test</button>',
      },
    });
  };

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

  it('executes apollo mutate on doDelete', () => {
    const mutate = jest.fn().mockResolvedValue({});
    mountComponent({ mutate });

    wrapper.vm.doDelete();

    expect(mutate).toHaveBeenCalledWith({
      mutation: deleteContainerRepositoryMutation,
      variables: {
        id,
      },
      update: undefined,
    });
  });

  it('on success emits the correct events', async () => {
    const mutate = jest.fn().mockResolvedValue({});
    mountComponent({ mutate });

    wrapper.vm.doDelete();

    await waitForPromises();

    expect(wrapper.emitted('start')).toEqual([[]]);
    expect(wrapper.emitted('success')).toEqual([[]]);
    expect(wrapper.emitted('end')).toEqual([[]]);
  });

  it('when a payload contains an error emits an error event', async () => {
    const mutate = jest
      .fn()
      .mockResolvedValue({ data: { destroyContainerRepository: { errors: ['foo'] } } });

    mountComponent({ mutate });
    wrapper.vm.doDelete();

    await waitForPromises();

    expect(wrapper.emitted('error')).toEqual([[['foo']]]);
  });

  it('when the api call errors emits an error event', async () => {
    const mutate = jest.fn().mockRejectedValue('error');

    mountComponent({ mutate });
    wrapper.vm.doDelete();

    await waitForPromises();

    expect(wrapper.emitted('error')).toEqual([[['error']]]);
  });

  it('uses the update function, when the prop is set to true', () => {
    const mutate = jest.fn().mockResolvedValue({});

    mountComponent({ mutate, propsData: { id, useUpdateFn: true } });
    wrapper.vm.doDelete();

    expect(mutate).toHaveBeenCalledWith({
      mutation: deleteContainerRepositoryMutation,
      variables: {
        id,
      },
      update: wrapper.vm.updateImageStatus,
    });
  });

  it('updateImage status reads and write to the cache', () => {
    mountComponent();

    const variables = {
      id,
      first: GRAPHQL_PAGE_SIZE,
    };

    wrapper.vm.updateImageStatus(storeMock, updatePayload);

    expect(storeMock.readQuery).toHaveBeenCalledWith({
      query: getContainerRepositoryDetailsQuery,
      variables,
    });
    expect(storeMock.writeQuery).toHaveBeenCalledWith({
      query: getContainerRepositoryDetailsQuery,
      variables,
      data: {
        containerRepository: {
          status: updatePayload.data.destroyContainerRepository.containerRepository.status,
        },
      },
    });
  });

  it('binds the doDelete function to the default scoped slot', () => {
    const mutate = jest.fn().mockResolvedValue({});
    mountComponent({ mutate });
    findButton().trigger('click');
    expect(mutate).toHaveBeenCalled();
  });
});