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
|
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMount } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui';
import { GlSingleStat } from '@gitlab/ui/dist/charts';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import KubernetesPods from '~/environments/components/kubernetes_pods.vue';
import { mockKasTunnelUrl } from './mock_data';
import { k8sPodsMock } from './graphql/mock_data';
Vue.use(VueApollo);
describe('~/environments/components/kubernetes_pods.vue', () => {
let wrapper;
const namespace = 'my-kubernetes-namespace';
const configuration = {
basePath: mockKasTunnelUrl,
baseOptions: {
headers: { 'GitLab-Agent-Id': '1' },
},
};
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findAllStats = () => wrapper.findAllComponents(GlSingleStat);
const findSingleStat = (at) => findAllStats().at(at);
const createApolloProvider = () => {
const mockResolvers = {
Query: {
k8sPods: jest.fn().mockReturnValue(k8sPodsMock),
},
};
return createMockApollo([], mockResolvers);
};
const createWrapper = (apolloProvider = createApolloProvider()) => {
wrapper = shallowMount(KubernetesPods, {
propsData: { namespace, configuration },
apolloProvider,
});
};
describe('mounted', () => {
it('shows the loading icon', () => {
createWrapper();
expect(findLoadingIcon().exists()).toBe(true);
});
it('hides the loading icon when the list of pods loaded', async () => {
createWrapper();
await waitForPromises();
expect(findLoadingIcon().exists()).toBe(false);
});
});
describe('when gets pods data', () => {
it('renders stats', async () => {
createWrapper();
await waitForPromises();
expect(findAllStats()).toHaveLength(4);
});
it.each`
count | title | index
${2} | ${KubernetesPods.i18n.runningPods} | ${0}
${1} | ${KubernetesPods.i18n.pendingPods} | ${1}
${1} | ${KubernetesPods.i18n.succeededPods} | ${2}
${2} | ${KubernetesPods.i18n.failedPods} | ${3}
`(
'renders stat with title "$title" and count "$count" at index $index',
async ({ count, title, index }) => {
createWrapper();
await waitForPromises();
expect(findSingleStat(index).props()).toMatchObject({
value: count,
title,
});
},
);
});
describe('when gets an error from the cluster_client API', () => {
const error = new Error('Error from the cluster_client API');
const createErroredApolloProvider = () => {
const mockResolvers = {
Query: {
k8sPods: jest.fn().mockRejectedValueOnce(error),
},
};
return createMockApollo([], mockResolvers);
};
beforeEach(async () => {
createWrapper(createErroredApolloProvider());
await waitForPromises();
});
it("doesn't show pods stats", () => {
expect(findAllStats()).toHaveLength(0);
});
it('emits an error message', () => {
expect(wrapper.emitted('cluster-error')).toMatchObject([[error]]);
});
});
});
|