Skip to content

Commit 3e4e27a

Browse files
xingzhang-suselsongsuse
authored andcommitted
Add initial unit test for registries overview page
1 parent 5e17a0e commit 3e4e27a

File tree

5 files changed

+107
-23
lines changed

5 files changed

+107
-23
lines changed

pkg/sbombastic-image-vulnerability-scanner/pages/c/_cluster/sbombastic-image-vulnerability-scanner/ImageOverview.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
class="btn role-primary"
2929
aria-label="Download full report"
3030
type="button"
31-
:disabled="disabled"
3231
@click="openAddEditRuleModal()">
3332
<i class="icon icon-download"></i>
3433
{{ t('imageScanner.images.downloadReport') }}

pkg/sbombastic-image-vulnerability-scanner/pages/c/_cluster/sbombastic-image-vulnerability-scanner/RegistriesConfiguration.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
class="btn role-secondary"
1515
aria-label="Refresh data"
1616
type="button"
17-
:disabled="disabled"
1817
@click="refresh()">
1918
<em class="icon-refresh-ss"></em>{{ t('imageScanner.registries.button.refresh') }}
2019
</button>
@@ -25,7 +24,6 @@
2524
class="btn role-primary"
2625
aria-label="Add new"
2726
type="button"
28-
:disabled="disabled"
2927
@click="openAddEditRegistry()">
3028
{{ t('imageScanner.registries.button.addNew') }}
3129
</button>

pkg/sbombastic-image-vulnerability-scanner/pages/c/_cluster/sbombastic-image-vulnerability-scanner/VexManagement.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
class="btn role-primary"
1414
:aria-label="t('imageScanner.vexManagement.button.create')"
1515
type="button"
16-
:disabled="disabled"
1716
@click="createVexHub()">
1817
{{ t('imageScanner.vexManagement.button.create') }}
1918
</button>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { shallowMount, flushPromises } from '@vue/test-utils';
2+
import registries from '../RegistriesConfiguration.vue';
3+
4+
describe('registries.vue', () => {
5+
let storeMock: any;
6+
let routerMock: any;
7+
8+
beforeEach(() => {
9+
// mock Vuex store
10+
storeMock = {
11+
dispatch: jest.fn(),
12+
getters: {
13+
'cluster/all': jest.fn().mockReturnValue([]),
14+
'cluster/canList': jest.fn().mockReturnValue(true),
15+
'cluster/schemaFor': jest.fn().mockReturnValue({}),
16+
'prefs/get': jest.fn().mockImplementation((key) => {
17+
if (key === 'DATE_FORMAT') return 'YYYY-MM-DD';
18+
if (key === 'TIME_FORMAT') return 'HH:mm:ss';
19+
}),
20+
},
21+
};
22+
23+
// mock Vue Router
24+
routerMock = {
25+
push: jest.fn(),
26+
};
27+
});
28+
29+
function factory() {
30+
return shallowMount(registries, {
31+
global: {
32+
mocks: {
33+
$store: storeMock,
34+
$router: routerMock,
35+
$route: { params: { cluster: 'c-abc' } },
36+
t: (msg: string) => msg, // mock i18n
37+
},
38+
stubs: {
39+
SortableTable: { template: '<div><slot/></div>' },
40+
RecentUpdatedRegistries: true,
41+
StatusDistribution: true,
42+
ScanButton: true,
43+
},
44+
},
45+
});
46+
}
47+
48+
it('renders the title correctly', () => {
49+
const wrapper = factory();
50+
expect(wrapper.find('.title').text()).toBe('imageScanner.registries.title');
51+
});
52+
53+
it('calls loadData when refresh is clicked', async () => {
54+
const wrapper = factory();
55+
const refreshSpy = jest.spyOn(wrapper.vm, 'loadData').mockResolvedValue();
56+
57+
await wrapper.find('button[aria-label="Refresh data"]').trigger('click');
58+
expect(refreshSpy).toHaveBeenCalledWith(true);
59+
});
60+
61+
it('navigates to create registry when Create is clicked', async () => {
62+
const wrapper = factory();
63+
await wrapper.find('button[aria-label="Add new"]').trigger('click');
64+
65+
expect(routerMock.push).toHaveBeenCalledWith({
66+
name: 'imageScanner-c-cluster-resource-create',
67+
params: {
68+
resource: 'sbombastic.rancher.io.registry',
69+
cluster: 'c-abc',
70+
product: 'imageScanner',
71+
},
72+
});
73+
});
74+
75+
it('updates rows after loadData', async () => {
76+
const wrapper = factory();
77+
78+
storeMock.getters['cluster/all']
79+
.mockReturnValueOnce([{ metadata: { name: 'reg1', namespace: 'ns1' }, spec: {} }]) // registries
80+
.mockReturnValueOnce([]); // scanJobs
81+
82+
await wrapper.vm.loadData(true);
83+
await flushPromises();
84+
85+
// Ensure rows is typed as any[] for test purposes
86+
const rows = wrapper.vm.rows as any[];
87+
expect(rows).toHaveLength(1);
88+
expect(rows[0].metadata.name).toBe('reg1');
89+
});
90+
91+
it('filterByStatus updates input value and triggers events', async () => {
92+
document.body.innerHTML = `
93+
<input type="search" placeholder="Filter" aria-label="Filter table results" />
94+
`;
95+
96+
const wrapper = factory();
97+
const el = document.querySelector('input[type="search"]');
98+
99+
const inputSpy = jest.fn();
100+
el?.addEventListener('input', inputSpy);
101+
102+
wrapper.vm.filterByStatus('active');
103+
104+
expect((el as HTMLInputElement)?.value).toBe('active');
105+
expect(inputSpy).toHaveBeenCalled();
106+
});
107+
});

pkg/sbombastic-image-vulnerability-scanner/pages/c/_cluster/sbombastic-image-vulnerability-scanner/__tests__/registries.spec.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)