Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/condition/condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,115 @@ test('does not trigger twice if branch change predicate state', () => {
expect(onThen).toHaveBeenCalledTimes(1);
expect(onElse).not.toHaveBeenCalled();
});

test('source: event, if: store, then: event, else: event[]', () => {
const source = createEvent<string>();
const $if = createStore(false);
const target = createEvent<string>();
const elseTarget = createEvent<string>();
const elseTargetSecond = createEvent<string>();

const thenFn = jest.fn();
const elseFn = jest.fn();
const elseFnSecond = jest.fn();

target.watch(thenFn);
elseTarget.watch(elseFn);
elseTargetSecond.watch(elseFnSecond);

condition({
source,
if: $if,
then: target,
else: [elseTarget, elseTargetSecond],
});

// @ts-expect-error setState is internal
$if.setState(true);
source('baz');
expect(argumentHistory(thenFn)).toMatchInlineSnapshot(`
[
"baz",
]
`);
expect(argumentHistory(elseFn)).toMatchInlineSnapshot(`[]`);
expect(argumentHistory(elseFnSecond)).toMatchInlineSnapshot(`[]`);
expect(thenFn).toHaveBeenCalledTimes(1);
expect(elseFn).not.toHaveBeenCalled();
expect(elseFnSecond).not.toHaveBeenCalled();

// @ts-expect-error setState is internal
$if.setState(false);
source('bar');
expect(argumentHistory(elseFn)).toMatchInlineSnapshot(`
[
"bar",
]
`);
expect(argumentHistory(elseFnSecond)).toMatchInlineSnapshot(`
[
"bar",
]
`);

source('foo');
expect(argumentHistory(elseFn)).toMatchInlineSnapshot(`
[
"bar",
"foo",
]
`);
expect(argumentHistory(elseFnSecond)).toMatchInlineSnapshot(`
[
"bar",
"foo",
]
`);

expect(thenFn).toHaveBeenCalledTimes(1);
expect(elseFn).toHaveBeenCalledTimes(2);
expect(elseFnSecond).toHaveBeenCalledTimes(2);
});

test('source: event, if: store, then: event[]', () => {
const source = createEvent<string>();
const $if = createStore(false);
const target = createEvent<string>();
const targetSecond = createEvent<string>();
const fn = jest.fn();
const fnSecond = jest.fn();
target.watch(fn);
targetSecond.watch(fnSecond);

condition({
source,
if: $if,
then: [target, targetSecond],
});

source('bar');
expect(argumentHistory(fn)).toMatchInlineSnapshot(`[]`);
expect(argumentHistory(fnSecond)).toMatchInlineSnapshot(`[]`);
expect(fn).not.toHaveBeenCalled();
expect(fnSecond).not.toHaveBeenCalled();

// @ts-expect-error setState is internal
$if.setState(true);
source('foo');
expect(argumentHistory(fn)).toMatchInlineSnapshot(`
[
"foo",
]
`);
expect(argumentHistory(fnSecond)).toMatchInlineSnapshot(`
[
"foo",
]
`);

expect(fn).toHaveBeenCalledTimes(1);
expect(fnSecond).toHaveBeenCalledTimes(1);

expect(fn).toHaveBeenCalledWith('foo');
expect(fnSecond).toHaveBeenCalledWith('foo');
});
30 changes: 16 additions & 14 deletions src/condition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type NonFalsy<T> = T extends null | undefined | false | 0 | 0n | '' ? never : T;

type SourceUnit<T> = Store<T> | Event<T> | Effect<T, any, any>;

type Targetable<T> = UnitTargetable<T> | ReadonlyArray<UnitTargetable<T>>;

// -- Without `source`, with type guard --
export function condition<Payload, Then extends Payload = Payload>(options: {
source?: undefined;
if: ((payload: Payload) => payload is Then) | Then;
then?: UnitTargetable<NoInfer<Then> | void>;
else?: UnitTargetable<Exclude<NoInfer<Payload>, Then> | void>;
then?: Targetable<NoInfer<Then> | void>;
else?: Targetable<Exclude<NoInfer<Payload>, Then> | void>;
}): EventCallableAsReturnType<Payload>;

// -- Without `source`, with BooleanConstructor --
Expand All @@ -32,16 +34,16 @@ export function condition<
>(options: {
source?: undefined;
if: BooleanConstructor;
then?: UnitTargetable<NoInfer<Then> | void>;
else?: UnitTargetable<Exclude<NoInfer<Payload>, Then> | void>;
then?: Targetable<NoInfer<Then> | void>;
else?: Targetable<Exclude<NoInfer<Payload>, Then> | void>;
}): EventCallableAsReturnType<Payload>;

// -- Without `source` --
export function condition<Payload>(options: {
source?: undefined;
if: ((payload: Payload) => boolean) | Store<boolean> | NoInfer<Payload>;
then?: UnitTargetable<NoInfer<Payload> | void>;
else?: UnitTargetable<NoInfer<Payload> | void>;
then?: Targetable<NoInfer<Payload> | void>;
else?: Targetable<NoInfer<Payload> | void>;
}): EventCallableAsReturnType<Payload>;

// -- With `source` and type guard --
Expand All @@ -52,8 +54,8 @@ export function condition<
>(options: {
source: Source;
if: ((payload: Payload) => payload is Then) | Then;
then?: UnitTargetable<NoInfer<Then>>;
else?: UnitTargetable<Exclude<NoInfer<Payload>, Then>>;
then?: Targetable<NoInfer<Then>>;
else?: Targetable<Exclude<NoInfer<Payload>, Then>>;
}): Source;

// -- With `source` and BooleanConstructor --
Expand All @@ -64,8 +66,8 @@ export function condition<
>(options: {
source: Source;
if: BooleanConstructor;
then?: UnitTargetable<NoInfer<Then> | void>;
else?: UnitTargetable<Exclude<NoInfer<Payload>, Then>>;
then?: Targetable<NoInfer<Then> | void>;
else?: Targetable<Exclude<NoInfer<Payload>, Then>>;
}): EventCallable<Payload>;

// -- With `source` --
Expand All @@ -75,8 +77,8 @@ export function condition<
>(options: {
source: SourceUnit<Payload>;
if: ((payload: Payload) => boolean) | Store<boolean> | NoInfer<Payload>;
then?: UnitTargetable<NoInfer<Payload> | void>;
else?: UnitTargetable<NoInfer<Payload> | void>;
then?: Targetable<NoInfer<Payload> | void>;
else?: Targetable<NoInfer<Payload> | void>;
}): Source;

export function condition<Payload>({
Expand All @@ -87,8 +89,8 @@ export function condition<Payload>({
}: {
source?: SourceUnit<Payload>;
if: ((payload: Payload) => boolean) | Store<boolean> | Payload;
then?: UnitTargetable<Payload | void>;
else?: UnitTargetable<Payload | void>;
then?: Targetable<Payload | void>;
else?: Targetable<Payload | void>;
}) {
const checker =
is.unit(test) || isFunction(test) ? test : (value: Payload) => value === test;
Expand Down
44 changes: 44 additions & 0 deletions test-typings/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,47 @@ import { condition } from '../dist/condition';
else: createEvent<number>(),
});
}

// Correct pass type from source to then/then[], else/else[], and if
{
const $source = createStore(0);

expectType<Store<number>>(
condition({
source: $source,
if: (a) => a === 1,
then: [createEvent<number>(), createEvent<number>()],
}),
);
expectType<Store<number>>(
condition({
source: $source,
if: (a) => a === 1,
else: [createEvent<number>(), createEvent<number>()],
}),
);
expectType<Store<number>>(
condition({
source: $source,
if: (a) => a === 1,
then: [createEvent<number>(), createEvent<number>()],
else: createEvent<number>(),
}),
);
expectType<Store<number>>(
condition({
source: $source,
if: (a) => a === 1,
then: createEvent<number>(),
else: [createEvent<number>(), createEvent<number>()],
}),
);
expectType<Store<number>>(
condition({
source: $source,
if: (a) => a === 1,
then: [createEvent<number>(), createEvent<number>()],
else: [createEvent<number>(), createEvent<number>()],
}),
);
}
Loading