Skip to content

Commit f9f8b32

Browse files
committed
Add unit tests
1 parent 6a4659f commit f9f8b32

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import { shallowMount } from '@vue/test-utils';
2+
3+
import { KUBEWARDEN } from '@kubewarden/types';
4+
import { VALUES_STATE } from '@kubewarden/types';
5+
import Create from '@kubewarden/components/Policies/Create.vue';
6+
7+
// A minimal VersionInfo-like object whose parsePolicyModule() returns known values
8+
const mockVersionInfo = {
9+
chart: { annotations: { 'kubewarden/registry': 'ghcr.io', 'kubewarden/repository': 'kubewarden/policies/cap-hostname', 'kubewarden/tag': 'v1.0.0' } },
10+
values: {
11+
module: {
12+
repository: 'kubewarden/policies/cap-hostname',
13+
tag: 'v1.0.0',
14+
},
15+
},
16+
};
17+
18+
const mockStoreGetters = {
19+
currentStore: () => 'cluster',
20+
'cluster/schemaFor': () => null,
21+
'cluster/canList': () => false,
22+
'type-map/isSpoofed': () => false,
23+
'catalog/repos': [],
24+
'catalog/charts': [],
25+
'management/byId': () => null,
26+
'kubewarden/airGapped': false,
27+
'kubewarden/hideBannerOfficialRepo': true,
28+
'kubewarden/hideBannerPolicyRepo': true,
29+
'kubewarden/hideBannerAirgapPolicy': true,
30+
'i18n/t': (key: string) => key,
31+
};
32+
33+
const mockStore = {
34+
getters: mockStoreGetters,
35+
dispatch: jest.fn(),
36+
};
37+
38+
function createWrapper(overrides: any = {}) {
39+
return shallowMount(Create, {
40+
props: {
41+
value: {
42+
metadata: { annotations: {} },
43+
spec: {},
44+
},
45+
mode: 'create',
46+
},
47+
global: {
48+
provide: { chartType: KUBEWARDEN.CLUSTER_ADMISSION_POLICY, realMode: 'create' },
49+
mocks: {
50+
$store: mockStore,
51+
$fetchState: { pending: false },
52+
$route: { params: {}, query: {}, hash: '' },
53+
$router: { replace: jest.fn() },
54+
},
55+
stubs: {
56+
Loading: { template: '<span />' },
57+
Wizard: { template: '<span />' },
58+
PolicyTable: { template: '<span />' },
59+
PolicyReadmePanel: { template: '<span />' },
60+
Values: { template: '<span />' },
61+
Banner: { template: '<span />' },
62+
AsyncButton: { template: '<span />' },
63+
},
64+
},
65+
...overrides,
66+
});
67+
}
68+
69+
describe('component: Create', () => {
70+
describe('resolvePolicyModuleInfo()', () => {
71+
it('sets policyModuleInfo with chart defaults when module matches suffix exactly', () => {
72+
const wrapper = createWrapper();
73+
74+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
75+
(wrapper.vm as any).chartValues = { policy: { spec: { module: 'kubewarden/policies/cap-hostname:v1.0.0' } } };
76+
77+
(wrapper.vm as any).resolvePolicyModuleInfo();
78+
79+
expect((wrapper.vm as any).policyModuleInfo).toMatchObject({
80+
registry: '',
81+
repository: 'kubewarden/policies/cap-hostname',
82+
tag: 'v1.0.0',
83+
});
84+
});
85+
86+
it('extracts custom registry when module has a registry prefix matching the suffix', () => {
87+
const wrapper = createWrapper();
88+
89+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
90+
(wrapper.vm as any).chartValues = { policy: { spec: { module: 'myregistry.io/kubewarden/policies/cap-hostname:v1.0.0' } } };
91+
92+
(wrapper.vm as any).resolvePolicyModuleInfo();
93+
94+
expect((wrapper.vm as any).policyModuleInfo).toMatchObject({
95+
registry: 'myregistry.io',
96+
repository: 'kubewarden/policies/cap-hostname',
97+
tag: 'v1.0.0',
98+
});
99+
});
100+
101+
it('extracts registry without dots (e.g. "11111") when repo/tag were also changed', () => {
102+
const wrapper = createWrapper();
103+
104+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
105+
// Registry "11111" has no dot — anchor suffix no longer matches, so falls back to split logic
106+
(wrapper.vm as any).chartValues = { policy: { spec: { module: '11111/22222:333333' } } };
107+
108+
(wrapper.vm as any).resolvePolicyModuleInfo();
109+
110+
expect((wrapper.vm as any).policyModuleInfo).toMatchObject({
111+
registry: '11111',
112+
repository: '22222',
113+
tag: '333333',
114+
});
115+
});
116+
117+
it('does nothing when selectedPolicyDetails is null', () => {
118+
const wrapper = createWrapper();
119+
120+
(wrapper.vm as any).selectedPolicyDetails = null;
121+
(wrapper.vm as any).policyModuleInfo = null;
122+
123+
(wrapper.vm as any).resolvePolicyModuleInfo();
124+
125+
expect((wrapper.vm as any).policyModuleInfo).toBeNull();
126+
});
127+
});
128+
129+
describe('yamlOption watcher (YAML → FORM)', () => {
130+
it('syncs spec.module from yamlValues and re-resolves policyModuleInfo', async() => {
131+
const wrapper = createWrapper();
132+
133+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
134+
(wrapper.vm as any).chartValues = { policy: { spec: { module: 'kubewarden/policies/cap-hostname:v1.0.0' } } };
135+
(wrapper.vm as any).yamlValues = 'spec:\n module: 11111/22222:333333\n';
136+
137+
// Simulate transition from YAML → FORM
138+
(wrapper.vm as any).yamlOption = VALUES_STATE.YAML;
139+
await wrapper.vm.$nextTick();
140+
141+
(wrapper.vm as any).yamlOption = VALUES_STATE.FORM;
142+
await wrapper.vm.$nextTick();
143+
144+
expect((wrapper.vm as any).chartValues.policy.spec.module).toBe('11111/22222:333333');
145+
expect((wrapper.vm as any).policyModuleInfo).toMatchObject({
146+
registry: '11111',
147+
repository: '22222',
148+
tag: '333333',
149+
});
150+
});
151+
152+
it('does not run when transitioning FORM → YAML', async() => {
153+
const wrapper = createWrapper();
154+
155+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
156+
(wrapper.vm as any).policyModuleInfo = { registry: 'original', repository: 'repo', tag: 'tag', source: 'values' };
157+
(wrapper.vm as any).chartValues = { policy: { spec: { module: 'repo:tag' } } };
158+
(wrapper.vm as any).yamlValues = 'spec:\n module: other/repo:newtag\n';
159+
160+
(wrapper.vm as any).yamlOption = VALUES_STATE.YAML;
161+
await wrapper.vm.$nextTick();
162+
163+
// policyModuleInfo should be unchanged
164+
expect((wrapper.vm as any).policyModuleInfo.registry).toBe('original');
165+
});
166+
167+
it('does not run when customPolicy is true', async() => {
168+
const wrapper = createWrapper();
169+
170+
(wrapper.vm as any).selectedPolicyDetails = mockVersionInfo;
171+
(wrapper.vm as any).selectedPolicyChart = 'custom'; // makes customPolicy computed = true
172+
(wrapper.vm as any).policyModuleInfo = { registry: 'original', repository: 'repo', tag: 'tag', source: 'values' };
173+
(wrapper.vm as any).chartValues = { policy: { spec: { module: 'repo:tag' } } };
174+
(wrapper.vm as any).yamlValues = 'spec:\n module: 11111/22222:333333\n';
175+
176+
(wrapper.vm as any).yamlOption = VALUES_STATE.YAML;
177+
await wrapper.vm.$nextTick();
178+
179+
(wrapper.vm as any).yamlOption = VALUES_STATE.FORM;
180+
await wrapper.vm.$nextTick();
181+
182+
expect((wrapper.vm as any).policyModuleInfo.registry).toBe('original');
183+
});
184+
});
185+
});

0 commit comments

Comments
 (0)