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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlLoadingIcon, GlTabs, GlTab, GlTable, GlPagination, GlBadge } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { stubComponent } from 'helpers/stub_component';
import { useFakeDate } from 'helpers/fake_date';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import KubernetesTabs from '~/environments/components/kubernetes_tabs.vue';
import KubernetesSummary from '~/environments/components/kubernetes_summary.vue';
import { SERVICES_LIMIT_PER_PAGE } from '~/environments/constants';
import { mockKasTunnelUrl } from './mock_data';
import { k8sServicesMock } from './graphql/mock_data';
Vue.use(VueApollo);
describe('~/environments/components/kubernetes_tabs.vue', () => {
let wrapper;
const namespace = 'my-kubernetes-namespace';
const configuration = {
basePath: mockKasTunnelUrl,
baseOptions: {
headers: { 'GitLab-Agent-Id': '1' },
},
};
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findTabs = () => wrapper.findComponent(GlTabs);
const findTab = () => wrapper.findComponent(GlTab);
const findTable = () => wrapper.findComponent(GlTable);
const findPagination = () => wrapper.findComponent(GlPagination);
const findKubernetesSummary = () => wrapper.findComponent(KubernetesSummary);
const createApolloProvider = () => {
const mockResolvers = {
Query: {
k8sServices: jest.fn().mockReturnValue(k8sServicesMock),
},
};
return createMockApollo([], mockResolvers);
};
const createWrapper = (apolloProvider = createApolloProvider()) => {
wrapper = shallowMountExtended(KubernetesTabs, {
propsData: { configuration, namespace },
apolloProvider,
stubs: {
GlTab,
GlTable: stubComponent(GlTable, {
props: ['items', 'per-page'],
}),
GlBadge,
},
});
};
describe('mounted', () => {
it('shows tabs', () => {
createWrapper();
expect(findTabs().exists()).toBe(true);
});
it('renders summary tab', () => {
createWrapper();
expect(findKubernetesSummary().props()).toEqual({ namespace, configuration });
});
it('renders services tab', () => {
createWrapper();
expect(findTab().text()).toMatchInterpolatedText(`${KubernetesTabs.i18n.servicesTitle} 0`);
});
});
describe('services tab', () => {
useFakeDate(2020, 6, 6);
it('shows the loading icon', () => {
createWrapper();
expect(findLoadingIcon().exists()).toBe(true);
});
describe('when services data is loaded', () => {
beforeEach(async () => {
createWrapper();
await waitForPromises();
});
it('hides the loading icon when the list of services loaded', () => {
expect(findLoadingIcon().exists()).toBe(false);
});
it('renders services table when gets services data', () => {
expect(findTable().props('perPage')).toBe(SERVICES_LIMIT_PER_PAGE);
expect(findTable().props('items')).toMatchObject([
{
name: 'my-first-service',
namespace: 'default',
type: 'ClusterIP',
clusterIP: '10.96.0.1',
externalIP: '-',
ports: '443/TCP',
age: '0s',
},
{
name: 'my-second-service',
namespace: 'default',
type: 'NodePort',
clusterIP: '10.105.219.238',
externalIP: '-',
ports: '80:31989/TCP, 443:32679/TCP',
age: '2d',
},
]);
});
it("doesn't render pagination when services are less then SERVICES_LIMIT_PER_PAGE", async () => {
createWrapper();
await waitForPromises();
expect(findPagination().exists()).toBe(false);
});
});
it('shows pagination when services are more then SERVICES_LIMIT_PER_PAGE', async () => {
const createApolloProviderWithPagination = () => {
const mockResolvers = {
Query: {
k8sServices: jest
.fn()
.mockReturnValue(
Array.from({ length: 6 }, () => k8sServicesMock).flatMap((array) => array),
),
},
};
return createMockApollo([], mockResolvers);
};
createWrapper(createApolloProviderWithPagination());
await waitForPromises();
expect(findPagination().exists()).toBe(true);
});
it('emits an error message when gets an error from the cluster_client API', async () => {
const error = new Error('Error from the cluster_client API');
const createErroredApolloProvider = () => {
const mockResolvers = {
Query: {
k8sServices: jest.fn().mockRejectedValueOnce(error),
},
};
return createMockApollo([], mockResolvers);
};
createWrapper(createErroredApolloProvider());
await waitForPromises();
expect(wrapper.emitted('cluster-error')).toEqual([[error]]);
});
});
});
|