112 lines
3.8 KiB
TypeScript
112 lines
3.8 KiB
TypeScript
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
|
|
||
|
|
const apiMocks = vi.hoisted(() => ({
|
||
|
|
rawMaterials: vi.fn(),
|
||
|
|
mixes: vi.fn(),
|
||
|
|
mix: vi.fn(),
|
||
|
|
products: vi.fn(),
|
||
|
|
productCosts: vi.fn(),
|
||
|
|
scenarios: vi.fn(),
|
||
|
|
dataQuality: vi.fn(),
|
||
|
|
clientAccess: vi.fn(),
|
||
|
|
clientAccessExport: vi.fn()
|
||
|
|
}));
|
||
|
|
|
||
|
|
const sessionMocks = vi.hoisted(() => ({
|
||
|
|
hasStoredClientSession: vi.fn(),
|
||
|
|
hasStoredAdminSession: vi.fn()
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('$lib/api', () => ({
|
||
|
|
api: apiMocks
|
||
|
|
}));
|
||
|
|
|
||
|
|
vi.mock('$lib/session', () => sessionMocks);
|
||
|
|
|
||
|
|
import { load as homeLoad } from './+page';
|
||
|
|
import { load as adminLoad } from './admin/+page';
|
||
|
|
import { load as mixesLoad } from './mixes/+page';
|
||
|
|
import { load as mixNewLoad } from './mixes/new/+page';
|
||
|
|
import { load as mixDetailLoad } from './mixes/[id]/+page';
|
||
|
|
import { load as productsLoad } from './products/+page';
|
||
|
|
import { load as rawMaterialsLoad } from './raw-materials/+page';
|
||
|
|
import { load as scenariosLoad } from './scenarios/+page';
|
||
|
|
|
||
|
|
describe('route loaders use the SvelteKit fetch argument', () => {
|
||
|
|
const fetcher = vi.fn() as typeof fetch;
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.clearAllMocks();
|
||
|
|
sessionMocks.hasStoredClientSession.mockReturnValue(true);
|
||
|
|
sessionMocks.hasStoredAdminSession.mockReturnValue(true);
|
||
|
|
|
||
|
|
apiMocks.rawMaterials.mockResolvedValue([{ id: 1 }]);
|
||
|
|
apiMocks.mixes.mockResolvedValue([{ id: 2 }]);
|
||
|
|
apiMocks.mix.mockResolvedValue({ id: 42 });
|
||
|
|
apiMocks.products.mockResolvedValue([{ id: 3 }]);
|
||
|
|
apiMocks.productCosts.mockResolvedValue([{ id: 4 }]);
|
||
|
|
apiMocks.scenarios.mockResolvedValue([{ id: 5 }]);
|
||
|
|
apiMocks.dataQuality.mockResolvedValue([{ id: 6 }]);
|
||
|
|
apiMocks.clientAccess.mockResolvedValue([{ id: 7 }]);
|
||
|
|
apiMocks.clientAccessExport.mockResolvedValue({ generated_at: '', clients: [] });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the home page loader', async () => {
|
||
|
|
await homeLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.rawMaterials).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.mixes).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.productCosts).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.scenarios).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.dataQuality).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the raw materials loader', async () => {
|
||
|
|
await rawMaterialsLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.rawMaterials).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.mixes).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.products).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.productCosts).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the mixes loader', async () => {
|
||
|
|
await mixesLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.mixes).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the new mix loader', async () => {
|
||
|
|
await mixNewLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.rawMaterials).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the mix detail loader', async () => {
|
||
|
|
await mixDetailLoad({ params: { id: '42' }, fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.mix).toHaveBeenCalledWith(42, fetcher);
|
||
|
|
expect(apiMocks.rawMaterials).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the products loader', async () => {
|
||
|
|
await productsLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.products).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.productCosts).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the scenarios loader', async () => {
|
||
|
|
await scenariosLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.scenarios).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes fetch through the admin loader', async () => {
|
||
|
|
await adminLoad({ fetch: fetcher } as never);
|
||
|
|
|
||
|
|
expect(apiMocks.clientAccess).toHaveBeenCalledWith(fetcher);
|
||
|
|
expect(apiMocks.clientAccessExport).toHaveBeenCalledWith(fetcher);
|
||
|
|
});
|
||
|
|
});
|