diff --git a/src/spread/spread.fork.test.ts b/src/spread/spread.fork.test.ts index 9af7a498..4afac567 100644 --- a/src/spread/spread.fork.test.ts +++ b/src/spread/spread.fork.test.ts @@ -4,64 +4,84 @@ import { spread } from './index'; test('works in forked scope', async () => { const app = createDomain(); - const source = app.createEvent<{ first: string; second: number }>(); + const source = app.createEvent<{ first: string; second: number; third: string }>(); const first = app.createEvent(); const second = app.createEvent(); - const _$first = app.createStore('').on(first, (_, p) => p); - const _$second = restore(second, 0); + const $thirdA = app.createStore(''); + const $thirdB = app.createStore(''); + + const $first = app.createStore('').on(first, (_, p) => p); + const $second = restore(second, 0); spread({ source, - targets: { first, second }, + targets: { first, second, third: [$thirdA, $thirdB] }, }); const scope = fork(); - await allSettled(source, { scope, params: { first: 'sergey', second: 26 } }); - expect(serialize(scope)).toMatchInlineSnapshot(` - { - "-xihhjw": 26, - "nln5hw": "sergey", - } - `); + await allSettled(source, { + scope, + params: { first: 'sergey', second: 26, third: '30' }, + }); + + expect(scope.getState($first)).toBe('sergey'); + expect(scope.getState($second)).toBe(26); + expect(scope.getState($thirdA)).toBe('30'); + expect(scope.getState($thirdB)).toBe('30'); }); test('do not affects original store state', async () => { const app = createDomain(); - const source = app.createEvent<{ first: string; second: number }>(); + const source = app.createEvent<{ first: string; second: number; third: string }>(); const first = app.createEvent(); const second = app.createEvent(); + const $thirdA = app.createStore(''); + const $thirdB = app.createStore(''); + const $first = app.createStore('').on(first, (_, p) => p); const $second = restore(second, 0); spread({ source, - targets: { first, second }, + targets: { first, second, third: [$thirdA, $thirdB] }, }); const scope = fork(); - await allSettled(source, { scope, params: { first: 'sergey', second: 26 } }); + await allSettled(source, { + scope, + params: { first: 'sergey', second: 26, third: '30' }, + }); + expect(scope.getState($first)).toBe('sergey'); expect(scope.getState($second)).toBe(26); + expect(scope.getState($thirdA)).toBe('30'); + expect(scope.getState($thirdB)).toBe('30'); + expect($first.getState()).toBe(''); expect($second.getState()).toBe(0); + expect($thirdA.getState()).toBe(''); + expect($thirdB.getState()).toBe(''); }); test('do not affects another scope', async () => { const app = createDomain(); - const source = app.createEvent<{ first: string; second: number }>(); + const source = app.createEvent<{ first: string; second: number; third: string }>(); const first = app.createEvent(); const second = app.createEvent(); - const _$first = app.createStore('').on(first, (_, p) => p); - const _$second = restore(second, 0); + const $thirdA = app.createStore(''); + const $thirdB = app.createStore(''); + + const $first = app.createStore('').on(first, (_, p) => p); + const $second = restore(second, 0); spread({ source, - targets: { first, second }, + targets: { first, second, third: [$thirdA, $thirdB] }, }); const scope1 = fork(); @@ -70,23 +90,21 @@ test('do not affects another scope', async () => { await Promise.all([ allSettled(source, { scope: scope1, - params: { first: 'sergey', second: 26 }, + params: { first: 'sergey', second: 26, third: '30' }, }), allSettled(source, { scope: scope2, - params: { first: 'Anon', second: 90 }, + params: { first: 'Anon', second: 90, third: '154' }, }), ]); - expect(serialize(scope1)).toMatchInlineSnapshot(` - { - "-w3pd79": 26, - "f2h7kg": "sergey", - } - `); - expect(serialize(scope2)).toMatchInlineSnapshot(` - { - "-w3pd79": 90, - "f2h7kg": "Anon", - } - `); + + expect(scope1.getState($first)).toBe('sergey'); + expect(scope1.getState($second)).toBe(26); + expect(scope1.getState($thirdA)).toBe('30'); + expect(scope1.getState($thirdB)).toBe('30'); + + expect(scope2.getState($first)).toBe('Anon'); + expect(scope2.getState($second)).toBe(90); + expect(scope2.getState($thirdA)).toBe('154'); + expect(scope2.getState($thirdB)).toBe('154'); }); diff --git a/test-typings/spread.ts b/test-typings/spread.ts index eafd304d..35827b72 100644 --- a/test-typings/spread.ts +++ b/test-typings/spread.ts @@ -11,16 +11,17 @@ import { import { spread } from '../dist/spread'; { - const $source = createStore({ first: '', last: '' }); + const $source = createStore({ first: '', last: '', foo: 1 }); const first = createEvent(); const last = createEvent(); - expectType>( + expectType>( spread({ source: $source, targets: { first, last, + foo: [createEvent(), createStore(1)], }, }), ); @@ -54,36 +55,77 @@ import { spread } from '../dist/spread'; last: createEvent(), }, }); + + // @ts-expect-error + spread({ + source: createEvent<{ first: string; last: number }>(), + targets: { + first: createEvent(), + last: [createEvent(), createEvent()], + }, + }); + + spread({ + source: createEvent<{ first: string; last: number }>(), + targets: { + // @ts-expect-error + last: [createEvent(), createEvent()], + first: createEvent(), + }, + }); + + sample({ + // @ts-expect-error + source: createEvent<{ first: string; last: number }>(), + target: spread({ + first: createEvent(), + last: [createEvent(), createEvent()], + }), + }); + + sample({ + // @ts-expect-error + source: createEvent<{ first: string; last: number }>(), + target: spread({ + targets: { + first: createEvent(), + last: [createEvent(), createEvent()], + }, + }), + }); } // Check input source type with output { - expectType>( + expectType>( spread({ - source: createEvent<{ foo: string; bar: number }>(), + source: createEvent<{ foo: string; bar: number; baz: boolean }>(), targets: { foo: createEvent(), bar: createEvent(), + baz: [createEvent(), createEvent()], }, }), ); - expectType>( + expectType>( spread({ - source: createStore({ random: '', bar: 5 }), + source: createStore({ random: '', bar: 5, baz: true }), targets: { random: createEvent(), bar: createEvent(), + baz: [createEvent(), createEvent()], }, }), ); - expectType>( + expectType>( spread({ - source: createEffect<{ foo: string; bar: number }, void>(), + source: createEffect<{ foo: string; bar: number; baz: boolean }, void>(), targets: { foo: createEvent(), bar: createEvent(), + baz: [createEvent(), createEvent()], }, }), ); @@ -91,32 +133,35 @@ import { spread } from '../dist/spread'; // Check target different units { - expectType>( + expectType>( spread({ - source: createEvent<{ foo: string; bar: number }>(), + source: createEvent<{ foo: string; bar: number; baz: boolean }>(), targets: { foo: createStore(''), bar: createEffect(), + baz: [createEvent(), createStore(true)], }, }), ); - expectType>( + expectType>( spread({ - source: createStore({ foo: '', bar: 5 }), + source: createStore({ foo: '', bar: 5, baz: true }), targets: { foo: createStore(''), bar: createEffect(), + baz: [createEvent(), createEffect()], }, }), ); - expectType>( + expectType>( spread({ - source: createEffect<{ foo: string; bar: number }, void>(), + source: createEffect<{ foo: string; bar: number; baz: boolean }, void>(), targets: { foo: createStore(''), bar: createEffect(), + baz: [createStore(true), createEffect()], }, }), ); @@ -126,12 +171,16 @@ import { spread } from '../dist/spread'; { const foo = createEvent(); - expectType>( + expectType>( spread({ - source: createEvent<{ foo: string; bar: number }>(), + source: createEvent<{ foo: string; bar: number; baz: boolean }>(), targets: { foo: foo.prepend((string) => string.length), bar: createEvent(), + baz: [ + createEvent().prepend((bool) => (bool ? 'true' : 'false')), + createEvent().prepend((bool) => (bool ? 1 : 0)), + ], }, }), ); @@ -144,19 +193,24 @@ import { spread } from '../dist/spread'; foo: createStore(''), bar: createEffect(), baz: createEvent(), + last: [createEvent(), createStore(null)], }, }); - expectType>(spreadToStores); + expectType>( + spreadToStores, + ); } { const spreadToStores = spread({ foo: createStore(''), - bar: createEffect(), baz: createEvent(), + last: [createEvent(), createStore(null)], }); - expectType>(spreadToStores); + expectType>( + spreadToStores, + ); } // Example from readme with nullability @@ -201,14 +255,21 @@ import { spread } from '../dist/spread'; first: createEvent(), }, }); + spread({ + source: createEffect<{ first: string; last: string }, void>(), + targets: { + first: [createEvent(), createStore('')], + }, + }); } // allows nested { - const $source = createStore({ first: '', last: { nested: '', other: '' } }); + const $source = createStore({ first: '', last: { nested: '', other: '', arr: 1 } }); const first = createEvent(); const nested = createEvent(); const other = createEvent(); + const arrayOfUnits = [createEvent(), createStore(1)]; // nested full match spread({ @@ -219,6 +280,7 @@ import { spread } from '../dist/spread'; targets: { nested, other, + arr: arrayOfUnits, }, }), }, @@ -252,6 +314,7 @@ import { spread } from '../dist/spread'; targets: { nested, other, + arr: arrayOfUnits, }, }); @@ -298,6 +361,7 @@ import { spread } from '../dist/spread'; targets: { nested, other, + arr: arrayOfUnits, }, }), }, @@ -317,10 +381,11 @@ import { spread } from '../dist/spread'; }); } { - const $source = createStore({ first: '', last: { nested: '', other: '' } }); + const $source = createStore({ first: '', last: { nested: '', other: '', arr: 1 } }); const first = createEvent(); const nested = createEvent(); const other = createEvent(); + const arrayOfUnits = [createEvent(), createStore(1)]; // nested full match spread({ @@ -330,6 +395,7 @@ import { spread } from '../dist/spread'; last: spread({ nested, other, + arr: arrayOfUnits, }), }, }); @@ -357,6 +423,7 @@ import { spread } from '../dist/spread'; const out = spread({ nested, other, + arr: arrayOfUnits, }); spread({ @@ -392,6 +459,7 @@ import { spread } from '../dist/spread'; last: spread({ nested, other, + arr: arrayOfUnits, }), }), });