mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { mapListItems, removeListItems, updateListItems, upsertListItem } from '@/api/queryData';
|
|
|
|
type Item = { id: number; name: string };
|
|
type Response = {
|
|
object: 'list';
|
|
data: Item[];
|
|
meta: { page: number };
|
|
};
|
|
|
|
const response = (data: Item[]): Response => ({ object: 'list', data, meta: { page: 2 } });
|
|
|
|
describe('query data list transforms', () => {
|
|
it('leaves missing cache data untouched unless an upsert creator is provided', () => {
|
|
expect(removeListItems<Response>(undefined, () => true)).toBeUndefined();
|
|
expect(
|
|
updateListItems<Response>(
|
|
undefined,
|
|
() => true,
|
|
(item) => item
|
|
)
|
|
).toBeUndefined();
|
|
expect(mapListItems<Response>(undefined, (item) => item)).toBeUndefined();
|
|
expect(upsertListItem<Response>(undefined, { id: 1, name: 'one' }, (item) => item.id === 1)).toBeUndefined();
|
|
expect(
|
|
upsertListItem<Response>(
|
|
undefined,
|
|
{ id: 1, name: 'one' },
|
|
(item) => item.id === 1,
|
|
(item) => response([item])
|
|
)
|
|
).toEqual(response([{ id: 1, name: 'one' }]));
|
|
});
|
|
|
|
it('inserts and replaces items without dropping response metadata', () => {
|
|
const initial = response([{ id: 1, name: 'one' }]);
|
|
const inserted = upsertListItem(initial, { id: 2, name: 'two' }, (item, next) => item.id === next.id);
|
|
const replaced = upsertListItem(inserted, { id: 1, name: 'updated' }, (item, next) => item.id === next.id);
|
|
|
|
expect(replaced).toEqual(
|
|
response([
|
|
{ id: 1, name: 'updated' },
|
|
{ id: 2, name: 'two' },
|
|
])
|
|
);
|
|
expect(replaced?.meta).toEqual(initial.meta);
|
|
});
|
|
|
|
it('removes, updates, and maps matching items immutably', () => {
|
|
const initial = response([
|
|
{ id: 1, name: 'one' },
|
|
{ id: 2, name: 'two' },
|
|
]);
|
|
|
|
const removed = removeListItems(initial, (item) => item.id === 1);
|
|
const updated = updateListItems(
|
|
initial,
|
|
(item) => item.id === 2,
|
|
(item) => ({ ...item, name: 'updated' })
|
|
);
|
|
const mapped = mapListItems(initial, (item) => ({ ...item, name: item.name.toUpperCase() }));
|
|
|
|
expect(removed).toEqual(response([{ id: 2, name: 'two' }]));
|
|
expect(updated).toEqual(
|
|
response([
|
|
{ id: 1, name: 'one' },
|
|
{ id: 2, name: 'updated' },
|
|
])
|
|
);
|
|
expect(mapped).toEqual(
|
|
response([
|
|
{ id: 1, name: 'ONE' },
|
|
{ id: 2, name: 'TWO' },
|
|
])
|
|
);
|
|
expect(initial).toEqual(
|
|
response([
|
|
{ id: 1, name: 'one' },
|
|
{ id: 2, name: 'two' },
|
|
])
|
|
);
|
|
});
|
|
});
|