diff --git a/apps/admin/README.md b/apps/admin/README.md new file mode 100644 index 0000000..95de983 --- /dev/null +++ b/apps/admin/README.md @@ -0,0 +1,7 @@ +# Admin App Test Data + +This directory contains resources for development of the admin UI. +It includes mock data used by `installTestApi` in `libs/ui` to +patch the custom `HttpResourceApi` so calls to `/users` return +predefined data. Import and invoke `installTestApi()` during +testing or local development before any HTTP requests are made. diff --git a/libs/ui/test-api.interceptor.ts b/libs/ui/test-api.interceptor.ts new file mode 100644 index 0000000..c175584 --- /dev/null +++ b/libs/ui/test-api.interceptor.ts @@ -0,0 +1,39 @@ +import { AxiosResponse } from 'axios'; +import { HttpResourceApi } from '../src/app/Core/HttpResourceApi'; +import { MOCK_USERS } from './test-data/users'; + +/** + * Patches the custom `HttpResourceApi` so mock data can be returned + * at the last possible moment before an HTTP request is executed. + */ +let installed = false; + +export function installTestApi(): void { + if (installed) { + return; + } + installed = true; + + const axios = (HttpResourceApi as any)._axios; + if (!axios) { + console.warn('Test API could not access HttpResourceApi axios instance'); + return; + } + + axios.interceptors.request.use((config) => { + const url = config.url?.replace('//', '/'); + if (url && url.endsWith('/users')) { + config.adapter = async () => { + const response: AxiosResponse = { + data: MOCK_USERS, + status: 200, + statusText: 'OK', + headers: {}, + config, + }; + return Promise.resolve(response); + }; + } + return config; + }); +} diff --git a/libs/ui/test-data/users.ts b/libs/ui/test-data/users.ts new file mode 100644 index 0000000..1174da8 --- /dev/null +++ b/libs/ui/test-data/users.ts @@ -0,0 +1,10 @@ +export interface User { + id: number; + name: string; +} + +export const MOCK_USERS: User[] = [ + { id: 1, name: 'Alice' }, + { id: 2, name: 'Bob' }, + { id: 3, name: 'Charlie' } +];