Skip to content

Commit 594570d

Browse files
committed
Fix unit tests
Signed-off-by: Phillip Rak <[email protected]>
1 parent 68e046e commit 594570d

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

jest.setup.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ jest.mock('@shell/composables/useI18n', () => ({ useI18n: () => (key) => key }))
6868
// eslint-disable-next-line no-console
6969
jest.spyOn(console, 'warn').mockImplementation((warning) => warning.includes('[Vue warn]') ? null : console.log(warning));
7070

71-
jest.mock('@shell/composables/useI18n', () => ({ useI18n: () => (key) => key }));
71+
// jest.mock('@shell/composables/useI18n', () => ({ useI18n: () => (key) => key }));
72+
jest.mock('@shell/composables/useI18n', () => {
73+
return {
74+
useI18n() {
75+
return {
76+
t(key) {
77+
return key;
78+
}
79+
};
80+
}
81+
};
82+
});
7283
// eslint-disable-next-line no-console
7384
jest.spyOn(console, 'warn').mockImplementation((warning) => warning.includes('[Vue warn]') ? null : console.log(warning));
7485

shell/components/form/NameNsDescription.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export default {
306306
namespace.value = metadata?.namespace;
307307
}
308308
309-
if (!namespace.value && !this.noDefaultNamespace) {
309+
if (!namespace.value && !props.noDefaultNamespace) {
310310
namespace.value = store.getters['defaultNamespace'];
311311
if (metadata) {
312312
metadata.namespace = namespace;

shell/components/form/__tests__/NameNsDescription.test.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { mount } from '@vue/test-utils';
22
import NameNsDescription from '@shell/components/form/NameNsDescription.vue';
3+
import { createStore } from 'vuex';
34

45
describe('component: NameNsDescription', () => {
56
// Accessing to computed value due code complexity
67
it('should map namespaces to options', () => {
78
const namespaceName = 'test';
9+
const store = createStore({
10+
getters: {
11+
allowedNamespaces: () => () => ({ [namespaceName]: true }),
12+
currentStore: () => () => 'cluster',
13+
'cluster/schemaFor': () => jest.fn()
14+
}
15+
});
816
const result = [
917
{
1018
label: namespaceName,
@@ -21,15 +29,13 @@ describe('component: NameNsDescription', () => {
2129
cluster: {},
2230
},
2331
global: {
24-
mocks: {
32+
provide: { store },
33+
mocks: {
2534
$store: {
2635
dispatch: jest.fn(),
2736
getters: {
28-
namespaces: jest.fn(),
29-
allowedNamespaces: () => ({ [namespaceName]: true }),
30-
currentStore: () => 'cluster',
31-
'cluster/schemaFor': jest.fn(),
32-
'i18n/t': jest.fn(),
37+
namespaces: jest.fn(),
38+
'i18n/t': jest.fn(),
3339
},
3440
},
3541
},
@@ -41,6 +47,13 @@ describe('component: NameNsDescription', () => {
4147

4248
it('should emit in case of new namespace', () => {
4349
const namespaceName = 'test';
50+
const store = createStore({
51+
getters: {
52+
allowedNamespaces: () => () => ({ [namespaceName]: true }),
53+
currentStore: () => () => 'cluster',
54+
'cluster/schemaFor': () => jest.fn()
55+
}
56+
});
4457
const newNamespaceName = 'bananas';
4558
const wrapper = mount(NameNsDescription, {
4659
props: {
@@ -51,20 +64,18 @@ describe('component: NameNsDescription', () => {
5164
mode: 'create',
5265
},
5366
global: {
54-
mocks: {
67+
provide: { store },
68+
mocks: {
5569
$store: {
5670
dispatch: jest.fn(),
5771
getters: {
5872
namespaces: jest.fn(),
59-
allowedNamespaces: () => ({ [namespaceName]: true }),
6073
'customizations/getPreviewCluster': {
6174
ready: true,
6275
isLocal: false,
6376
badge: {},
6477
},
65-
currentStore: () => 'cluster',
66-
'cluster/schemaFor': jest.fn(),
67-
'i18n/t': jest.fn(),
78+
'i18n/t': jest.fn(),
6879
},
6980
},
7081
},

shell/edit/__tests__/monitoring.coreos.com.prometheusrule.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import { mount } from '@vue/test-utils';
22
import FormValidation from '@shell/mixins/form-validation';
33
import Monitoring from '@shell/edit/monitoring.coreos.com.prometheusrule/index.vue';
44
import { _EDIT } from '@shell/config/query-params';
5+
import { createStore } from 'vuex';
56

67
describe('edit: management.cattle.io.setting should', () => {
78
const MOCKED_ERRORS = ['error1', 'error2', 'error3', 'error4', 'error5'];
89
const ERROR_BANNER_SELECTOR = '[data-testid="banner-close"]';
10+
const store = createStore({
11+
getters: {
12+
namespaces: () => () => ({}),
13+
currentStore: () => () => 'current_store',
14+
'current_store/schemaFor': () => jest.fn()
15+
}
16+
});
917
const requiredSetup = () => ({
1018
// Remove all these mocks after migration to Vue 2.7/3 due mixin logic
1119
global: {
12-
mocks: {
20+
provide: { store },
21+
mocks: {
1322
$store: {
1423
dispatch: jest.fn(),
1524
getters: {

shell/edit/logging.banzaicloud.io.output/__tests__/logging.banzaicloud.io.output.test.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mount } from '@vue/test-utils';
22
import Banzai from '@shell/edit/logging.banzaicloud.io.output/index.vue';
3+
import { createStore } from 'vuex';
34

45
const outputSchema = {
56
id: 'logging.banzaicloud.io.output',
@@ -109,6 +110,13 @@ describe('view: logging.banzaicloud.io.output', () => {
109110
['http://localhost:3100', []],
110111
['not a proper URL', ['logging.loki.urlInvalid']],
111112
])('should validate Loki URL on save', (url, expectation) => {
113+
const store = createStore({
114+
getters: {
115+
namespaces: () => () => ({}),
116+
currentStore: () => () => 'cluster',
117+
'cluster/schemaFor': () => jest.fn()
118+
}
119+
});
112120
const wrapper = mount(Banzai, {
113121
data: () => ({ selectedProvider: 'loki' }),
114122
props: {
@@ -120,7 +128,8 @@ describe('view: logging.banzaicloud.io.output', () => {
120128
}
121129
},
122130
global: {
123-
mocks: {
131+
provide: { store },
132+
mocks: {
124133
$fetchState: { pending: false },
125134
$store: {
126135
dispatch: jest.fn(),
@@ -151,6 +160,13 @@ describe('view: logging.banzaicloud.io.output', () => {
151160
});
152161

153162
it('should load the default YAML data for output buffer config (from schema) in a CREATE scenario', async() => {
163+
const store = createStore({
164+
getters: {
165+
namespaces: () => () => ({}),
166+
currentStore: () => () => 'cluster',
167+
'cluster/schemaFor': () => jest.fn()
168+
}
169+
});
154170
const wrapper = mount(Banzai, {
155171
data: () => ({ selectedProvider: 'awsElasticsearch' }),
156172
props: {
@@ -162,7 +178,8 @@ describe('view: logging.banzaicloud.io.output', () => {
162178
}
163179
},
164180
global: {
165-
mocks: {
181+
provide: { store },
182+
mocks: {
166183
$fetchState: { pending: false },
167184
$store: {
168185
dispatch(arg: any) {
@@ -234,6 +251,13 @@ describe('view: logging.banzaicloud.io.output', () => {
234251
});
235252

236253
it('should load current output buffer config in an EDIT scenario', async() => {
254+
const store = createStore({
255+
getters: {
256+
namespaces: () => () => ({}),
257+
currentStore: () => () => 'cluster',
258+
'cluster/schemaFor': () => jest.fn()
259+
}
260+
});
237261
const wrapper = mount(Banzai, {
238262
data: () => ({ selectedProvider: 'awsElasticsearch' }),
239263
props: {
@@ -245,7 +269,8 @@ describe('view: logging.banzaicloud.io.output', () => {
245269
}
246270
},
247271
global: {
248-
mocks: {
272+
provide: { store },
273+
mocks: {
249274
$fetchState: { pending: false },
250275
$store: {
251276
dispatch(arg: any) {

0 commit comments

Comments
 (0)