|
| 1 | +import { |
| 2 | + actionTypes, |
| 3 | + Controller, |
| 4 | + DataClientDispatch, |
| 5 | + GenericDispatch, |
| 6 | +} from '../index.js'; |
| 7 | + |
| 8 | +import { collapseFixture } from './collapseFixture.js'; |
| 9 | +import { createFixtureMap } from './createFixtureMap.js'; |
| 10 | +import type { Fixture, Interceptor } from './fixtureTypes.js'; |
| 11 | +import { MockProps } from './mockTypes.js'; |
| 12 | + |
| 13 | +export function MockController<TBase extends typeof Controller, T>( |
| 14 | + Base: TBase, |
| 15 | + { |
| 16 | + fixtures = [], |
| 17 | + getInitialInterceptorData = () => ({}) as any, |
| 18 | + }: MockProps<T>, |
| 19 | +): TBase { |
| 20 | + const [fixtureMap, interceptors] = createFixtureMap(fixtures); |
| 21 | + |
| 22 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 23 | + // @ts-ignore |
| 24 | + return class MockedController< |
| 25 | + D extends GenericDispatch = DataClientDispatch, |
| 26 | + > extends Base<D> { |
| 27 | + // legacy compatibility (re-declaration) |
| 28 | + // TODO: drop when drop support for destructuring (0.14 and below) |
| 29 | + declare protected _dispatch: D; |
| 30 | + |
| 31 | + fixtureMap: Map<string, Fixture> = fixtureMap; |
| 32 | + interceptors: Interceptor<any>[] = interceptors; |
| 33 | + interceptorData: T = getInitialInterceptorData(); |
| 34 | + |
| 35 | + constructor(...args: any[]) { |
| 36 | + super(...args); |
| 37 | + |
| 38 | + // legacy compatibility |
| 39 | + // TODO: drop when drop support for destructuring (0.14 and below) |
| 40 | + if (!this._dispatch) { |
| 41 | + this._dispatch = (args[0] as any).dispatch; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // legacy compatibility - we need this to work with 0.14 and below as they do not have this setter |
| 46 | + // TODO: drop when drop support for destructuring (0.14 and below) |
| 47 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 48 | + // @ts-ignore |
| 49 | + set dispatch(dispatch: D) { |
| 50 | + this._dispatch = dispatch; |
| 51 | + } |
| 52 | + |
| 53 | + get dispatch(): D { |
| 54 | + return ((action: Parameters<D>[0]): Promise<void> => { |
| 55 | + // support legacy that has _TYPE suffix |
| 56 | + if (action.type === (actionTypes.FETCH ?? actionTypes.FETCH_TYPE)) { |
| 57 | + // eslint-disable-next-line prefer-const |
| 58 | + let { key, args } = action; |
| 59 | + let fixture: Fixture | Interceptor | undefined; |
| 60 | + if (this.fixtureMap.has(key)) { |
| 61 | + fixture = this.fixtureMap.get(key) as Fixture; |
| 62 | + if (!args) args = fixture.args; |
| 63 | + // exact matches take priority; now test ComputedFixture |
| 64 | + } else { |
| 65 | + for (const cfix of this.interceptors) { |
| 66 | + if (cfix.endpoint.testKey(key)) { |
| 67 | + fixture = cfix; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + // we have a match |
| 73 | + if (fixture !== undefined) { |
| 74 | + const replacedAction: typeof action = { |
| 75 | + ...action, |
| 76 | + }; |
| 77 | + const delayMs = |
| 78 | + typeof fixture.delay === 'function' ? |
| 79 | + fixture.delay(...(args as any)) |
| 80 | + : (fixture.delay ?? 0); |
| 81 | + |
| 82 | + if ('fetchResponse' in fixture) { |
| 83 | + const { fetchResponse } = fixture; |
| 84 | + fixture = { |
| 85 | + endpoint: fixture.endpoint, |
| 86 | + response(...args) { |
| 87 | + const endpoint = (action.endpoint as any).extend({ |
| 88 | + fetchResponse: (input: RequestInfo, init: RequestInit) => { |
| 89 | + const ret = fetchResponse.call(this, input, init); |
| 90 | + return Promise.resolve( |
| 91 | + new Response(JSON.stringify(ret), { |
| 92 | + status: 200, |
| 93 | + headers: new Headers({ |
| 94 | + 'Content-Type': 'application/json', |
| 95 | + }), |
| 96 | + }), |
| 97 | + ); |
| 98 | + }, |
| 99 | + }); |
| 100 | + return (endpoint as any)(...args); |
| 101 | + }, |
| 102 | + }; |
| 103 | + } |
| 104 | + const fetch = async () => { |
| 105 | + if (!fixture) { |
| 106 | + throw new Error('No fixture found'); |
| 107 | + } |
| 108 | + // delayCollapse determines when the fixture function is 'collapsed' (aka 'run') |
| 109 | + // collapsed: https://en.wikipedia.org/wiki/Copenhagen_interpretation |
| 110 | + if (fixture.delayCollapse) { |
| 111 | + await new Promise(resolve => setTimeout(resolve, delayMs)); |
| 112 | + } |
| 113 | + const result = await collapseFixture( |
| 114 | + fixture as any, |
| 115 | + args as any, |
| 116 | + this.interceptorData, |
| 117 | + ); |
| 118 | + if (!fixture.delayCollapse && delayMs) { |
| 119 | + await new Promise(resolve => setTimeout(resolve, delayMs)); |
| 120 | + } |
| 121 | + if (result.error) { |
| 122 | + throw result.response; |
| 123 | + } |
| 124 | + return result.response; |
| 125 | + }; |
| 126 | + if (typeof (replacedAction.endpoint as any).extend === 'function') { |
| 127 | + replacedAction.endpoint = (replacedAction.endpoint as any).extend( |
| 128 | + { |
| 129 | + fetch, |
| 130 | + }, |
| 131 | + ); |
| 132 | + } else { |
| 133 | + // TODO: full testing of this |
| 134 | + replacedAction.endpoint = fetch as any; |
| 135 | + (replacedAction.endpoint as any).__proto__ = action.endpoint; |
| 136 | + } |
| 137 | + |
| 138 | + // TODO: make super.dispatch (once we drop support for destructuring) |
| 139 | + return this._dispatch(replacedAction); |
| 140 | + } |
| 141 | + } |
| 142 | + // TODO: make super.dispatch (once we drop support for destructuring) |
| 143 | + return this._dispatch(action); |
| 144 | + }) as any; |
| 145 | + } |
| 146 | + }; |
| 147 | +} |
| 148 | + |
0 commit comments