Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Test Coverage: Additional useState tests (#42)
Browse files Browse the repository at this point in the history
* Additional useState tests

* Formatting
  • Loading branch information
bryphe authored Jan 5, 2019
1 parent 1f4801a commit 7cd94e2
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions test/HooksUseStateTest.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ let cComponent = (~children, ()) => primitiveComponent(C, ~children);
module ComponentWithState = (
val createComponent((render, ~children, ()) =>
render(
() => useStateExperimental(2, ((s, _setS)) => <aComponent testVal=s />),
() =>
useStateExperimental(2, ((s, _setS)) =>
<aComponent testVal=s />
),
~children,
)
)
Expand Down Expand Up @@ -124,6 +127,82 @@ test("useState", () => {
validateStructure(rootNode, expectedStructure);
});

test("nested useState set state persists across renders", () => {
let rootNode = createRootNode();

let container = createContainer(rootNode);

let event: Event.t(int) = Event.create();

updateContainer(
container,
<bComponent> <ComponentThatUpdatesState event /> </bComponent>,
);

Event.dispatch(event, 5);

let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(5))])]);
validateStructure(rootNode, expectedStructure);

updateContainer(
container,
<bComponent> <ComponentThatUpdatesState event /> </bComponent>,
);

let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(5))])]);
validateStructure(rootNode, expectedStructure);
});

test(
"nested useState setState for multiple components persists across renders",
() => {
let rootNode = createRootNode();

let container = createContainer(rootNode);

let event1: Event.t(int) = Event.create();
let event2: Event.t(int) = Event.create();

updateContainer(
container,
<bComponent>
<ComponentThatUpdatesState event=event1 />
<ComponentThatUpdatesState event=event2 />
</bComponent>,
);

Event.dispatch(event1, 5);
Event.dispatch(event2, 6);

let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(5)), TreeLeaf(A(6))])]);
validateStructure(rootNode, expectedStructure);

updateContainer(
container,
<bComponent>
<ComponentThatUpdatesState event=event1 />
<ComponentThatUpdatesState event=event2 />
</bComponent>,
);

let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(5)), TreeLeaf(A(6))])]);
validateStructure(rootNode, expectedStructure);

Event.dispatch(event1, 3);
let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(3)), TreeLeaf(A(6))])]);
validateStructure(rootNode, expectedStructure);

Event.dispatch(event2, 4);
let expectedStructure: tree(primitives) =
TreeNode(Root, [TreeNode(B, [TreeLeaf(A(3)), TreeLeaf(A(4))])]);
validateStructure(rootNode, expectedStructure);
});

test("useState can update multiple times", () => {
let rootNode = createRootNode();
let container = createContainer(rootNode);
Expand Down Expand Up @@ -254,4 +333,4 @@ test("useState", () => {
TreeNode(Root, [TreeLeaf(A(5))]);
validateStructure(rootNode, expectedStructure);
});
});
});

0 comments on commit 7cd94e2

Please sign in to comment.