2026-04-27 21:53:36 +12:00
|
|
|
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(() => ({
|
2026-04-29 01:21:16 +12:00
|
|
|
getStoredClientSession: vi.fn(),
|
2026-04-27 21:53:36 +12:00
|
|
|
hasStoredClientSession: vi.fn(),
|
2026-04-29 01:21:16 +12:00
|
|
|
hasStoredAdminSession: vi.fn(),
|
|
|
|
|
hasModuleAccess: vi.fn()
|
2026-04-27 21:53:36 +12:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
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);
|
2026-04-29 01:21:16 +12:00
|
|
|
sessionMocks.getStoredClientSession.mockReturnValue({ role: 'client', module_permissions: {} });
|
|
|
|
|
sessionMocks.hasModuleAccess.mockReturnValue(true);
|
2026-04-27 21:53:36 +12:00
|
|
|
|
|
|
|
|
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 }]);
|
2026-04-29 01:21:16 +12:00
|
|
|
apiMocks.clientAccessExport.mockResolvedValue({
|
|
|
|
|
generated_at: '',
|
|
|
|
|
client_rows: [],
|
|
|
|
|
user_rows: [],
|
|
|
|
|
feature_rows: [],
|
|
|
|
|
permission_rows: [],
|
|
|
|
|
audit_rows: [],
|
|
|
|
|
clients: []
|
|
|
|
|
});
|
2026-04-27 21:53:36 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|