Skip to content

Commit

Permalink
test(spread): added fork and type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
earthspacon committed Jul 25, 2024
1 parent cf60b04 commit dc67148
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 53 deletions.
82 changes: 50 additions & 32 deletions src/spread/spread.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
const second = app.createEvent<number>();

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<string>();
const second = app.createEvent<number>();

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<string>();
const second = app.createEvent<number>();

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();
Expand All @@ -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');
});
Loading

0 comments on commit dc67148

Please sign in to comment.