forked from verdnatura/salix-front
feat: refs #7087 added more test cases
This commit is contained in:
parent
bb6fae5d08
commit
1295f394f3
|
@ -1,4 +1,4 @@
|
||||||
import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest';
|
import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
|
||||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||||
import CardSummary from 'src/components/ui/CardSummary.vue';
|
import CardSummary from 'src/components/ui/CardSummary.vue';
|
||||||
|
|
||||||
|
@ -7,42 +7,57 @@ describe('CardSummary', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
const mockApiResponse = {
|
vi.spyOn(axios, 'get').mockResolvedValue({ data: [] });
|
||||||
data: {
|
});
|
||||||
data: [],
|
|
||||||
total: 0,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.spyOn(axios, 'get').mockResolvedValue(mockApiResponse);
|
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
wrapper = createWrapper(CardSummary, {
|
wrapper = createWrapper(CardSummary, {
|
||||||
global: {
|
|
||||||
stubs: [
|
|
||||||
'arrayData',
|
|
||||||
'useArrayData',
|
|
||||||
],
|
|
||||||
mocks: {
|
|
||||||
validate: vi.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
propsData: {
|
propsData: {
|
||||||
dataKey: 'cardSummaryKey',
|
dataKey: 'cardSummaryKey',
|
||||||
url: 'cardSummaryUrl',
|
url: 'cardSummaryUrl',
|
||||||
|
filter: 'cardFilter',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
vm = wrapper.vm;
|
vm = wrapper.vm;
|
||||||
wrapper = wrapper.wrapper;
|
wrapper = wrapper.wrapper;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fetch data', async () => {
|
it('should fetch data correctly', async () => {
|
||||||
expect(wrapper.emitted('fetch'));
|
const fetchSpy = vi
|
||||||
|
.spyOn(vm.arrayData, 'fetch')
|
||||||
|
.mockResolvedValue({ data: [{ id: 1, name: 'Test Entity' }] });
|
||||||
|
await vm.fetch();
|
||||||
|
|
||||||
|
expect(fetchSpy).toHaveBeenCalledWith({ append: false, updateRouter: false });
|
||||||
|
expect(wrapper.emitted('onFetch')).toBeTruthy();
|
||||||
|
expect(vm.isLoading).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should set correct props to the store', () => {
|
||||||
|
expect(vm.store.url).toEqual('cardSummaryUrl');
|
||||||
|
expect(vm.store.filter).toEqual('cardFilter');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should compute entity correctly from store data', () => {
|
||||||
|
vm.store.data = [{ id: 1, name: 'Entity 1' }];
|
||||||
|
expect(vm.entity).toEqual({ id: 1, name: 'Entity 1' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty data gracefully', () => {
|
||||||
|
vm.store.data = [];
|
||||||
|
expect(vm.entity).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should respond to prop changes and refetch data', async () => {
|
||||||
|
const fetchSpy = vi.spyOn(vm.arrayData, 'fetch');
|
||||||
|
await wrapper.setProps({ url: 'newUrl', filter: { key: 'newValue' } });
|
||||||
|
|
||||||
|
expect(fetchSpy).toHaveBeenCalled();
|
||||||
|
expect(vm.store.url).toBe('newUrl');
|
||||||
|
expect(vm.store.filter).toEqual({ key: 'newValue' });
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue