Skip to content
Open
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
1 change: 0 additions & 1 deletion samples/src/Samples/SelectSample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export function view(dispatch: Dispatcher<Msg>, model: Model) {
<select
value={value}
onChange={(e) => {
console.log('FW onChange', e.target.value);
dispatch({ type: 'selected', value: e.target.value });
}}
>
Expand Down
18 changes: 15 additions & 3 deletions tea-cup/src/TeaCup/Program.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, test } from 'vitest';
import { Cmd, Dispatcher, Maybe, noCmd, nothing, Result, Sub, Task } from 'tea-cup-fp';
import { Program, updateUntilIdle } from 'react-tea-cup';
import { render } from '@testing-library/react';
import { Cmd, Dispatcher, just, Maybe, noCmd, nothing, Result, Sub, Task } from 'tea-cup-fp';
import { Program } from './Program';
import { updateUntilCondition, updateUntilIdle } from './Testing';
import { render, RenderResult } from '@testing-library/react';
import * as React from 'react';

interface Model {
Expand Down Expand Up @@ -110,4 +111,15 @@ describe('program test', () => {
done();
}, 3000);
}));

test('update until condition', () => {
return updateUntilCondition(
{ init: init, view: (_d, model) => view(model), update: update, subscriptions: subscriptions },
(node) => render(node),
(model) => model.other,
).then(([model, { container }]) => {
expect(model).toEqual({ id: just('myid'), other: true });
expect(container.querySelector('#myid')?.textContent).toEqual('myid,true');
});
});
});
44 changes: 42 additions & 2 deletions tea-cup/src/TeaCup/Testing.ts → tea-cup/src/TeaCup/Testing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*
*/

import { Dispatcher, Cmd } from 'tea-cup-fp';
import { Dispatcher, Cmd, Sub, Maybe, nothing, just } from 'tea-cup-fp';
import { ReactElement } from 'react';
import { ProgramProps, Program, DispatchBridge } from './Program';
import * as React from 'react';

export class Testing<M> {
private _dispatched: M | undefined;
Expand All @@ -44,11 +47,48 @@ export class Testing<M> {
}

public dispatchFrom(cmd: Cmd<M>): Promise<M> {
return new Promise<M>((resolve) => {
return new Promise<M>((resolve, reject) => {
const dispatchedMsg = (msg: M) => {
resolve(msg);
};
cmd.execute(dispatchedMsg);
});
}
}

type RenderFun<Model, Msg, T> = (node: ReactElement<ProgramProps<Model, Msg>>) => T;
type Condition<Model, T> = (m: Model, t: T) => boolean;

export function updateUntilCondition<Model, Msg, T>(
props: ProgramProps<Model, Msg>,
render: RenderFun<Model, Msg, T>,
condition: Condition<Model, T>,
): Promise<[Model, T]> {
return new Promise((resolve) => {
let wrapper: Maybe<T> = nothing;
wrapper = just(
render(
<Program
init={props.init}
view={(d, model) => {
const r = props.view(d, model);
switch (wrapper.type) {
case 'Just': {
if (condition(model, wrapper.value)) {
resolve([model, wrapper.value]);
}
break;
}
default: {
break;
}
}
return r;
}}
update={props.update}
subscriptions={props.subscriptions}
/>,
),
);
});
}