Every machine spec redefined its own throwaway fixture helper (editing1/2/3,
editingWith), hardcoding fields like errors: {} that assert against shapes
the reducer may never actually produce. given(reduce, initial)(...msgs)
(libs/shared/src/testing/machine.ts) replaces them by replaying real Msgs
through the real reduce, so a fixture is provably reachable. Adds the same
idiom for value objects (unwrapOk) and RemoteData (loading/success/failure),
plus intake.acceptance.spec.ts as a worked full-journey example.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
24 lines
1.0 KiB
TypeScript
24 lines
1.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { RemoteData, map2, map } from './remote-data';
|
|
import { loading, failure, success } from '../testing/remote-data';
|
|
|
|
const loadingRd: RemoteData<string, number> = loading();
|
|
const failureRd: RemoteData<string, number> = failure('x');
|
|
const ok = (n: number): RemoteData<string, number> => success(n);
|
|
|
|
describe('RemoteData combinators', () => {
|
|
it('map only touches Success', () => {
|
|
const times10 = (n: number) => n * 10;
|
|
expect(map(ok(2), times10)).toEqual({ tag: 'Success', value: 20 });
|
|
expect(map(loadingRd, times10)).toEqual(loadingRd);
|
|
});
|
|
|
|
it('map2 precedence: Failure > Loading > Success', () => {
|
|
const add = (a: number, b: number) => a + b;
|
|
expect(map2(failureRd, ok(1), add)).toEqual(failureRd); // a failed
|
|
expect(map2(ok(1), failureRd, add)).toEqual(failureRd); // b failed
|
|
expect(map2(loadingRd, ok(1), add)).toEqual({ tag: 'Loading' });
|
|
expect(map2(ok(2), ok(3), add)).toEqual({ tag: 'Success', value: 5 });
|
|
});
|
|
});
|