0
0
Fork 0

test: refs #6919 fix typos in useArrayData tests and add new test cases for single record handling

This commit is contained in:
Jorge Penadés 2025-01-30 17:51:18 +01:00
parent 78da5f5393
commit 169389b5b8
1 changed files with 25 additions and 4 deletions

View File

@ -16,7 +16,7 @@ describe('useArrayData', () => {
vi.clearAllMocks();
});
it('should fetch and repalce url with new params', async () => {
it('should fetch and replace url with new params', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
@ -33,11 +33,11 @@ describe('useArrayData', () => {
});
expect(routerReplace.path).toEqual('mockSection/list');
expect(JSON.parse(routerReplace.query.params)).toEqual(
expect.objectContaining(params)
expect.objectContaining(params),
);
});
it('Should get data and send new URL without keeping parameters, if there is only one record', async () => {
it('should get data and send new URL without keeping parameters, if there is only one record', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
@ -56,7 +56,7 @@ describe('useArrayData', () => {
expect(routerPush.query).toBeUndefined();
});
it('Should get data and send new URL keeping parameters, if you have more than one record', async () => {
it('should get data and send new URL keeping parameters, if you have more than one record', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] });
vi.spyOn(vueRouter, 'useRoute').mockReturnValue({
@ -95,4 +95,25 @@ describe('useArrayData', () => {
expect(routerPush.path).toEqual('mockName/');
expect(routerPush.query.params).toBeDefined();
});
it('should return one record', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({
data: [
{ id: 1, name: 'Entity 1' },
{ id: 2, name: 'Entity 2' },
],
});
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true });
await arrayData.fetch({});
expect(arrayData.store.data).toEqual({ id: 1, name: 'Entity 1' });
});
it('should handle empty data gracefully if has to return one record', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true });
await arrayData.fetch({});
expect(arrayData.store.data).toBeUndefined();
});
});