From 7cd94e22ee06eec04be05dd929879258aa21deec Mon Sep 17 00:00:00 2001 From: Bryan Phelps Date: Sat, 5 Jan 2019 14:23:24 -0800 Subject: [PATCH] Test Coverage: Additional useState tests (#42) * Additional useState tests * Formatting --- test/HooksUseStateTest.re | 83 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/test/HooksUseStateTest.re b/test/HooksUseStateTest.re index de8a71d..a53309f 100644 --- a/test/HooksUseStateTest.re +++ b/test/HooksUseStateTest.re @@ -20,7 +20,10 @@ let cComponent = (~children, ()) => primitiveComponent(C, ~children); module ComponentWithState = ( val createComponent((render, ~children, ()) => render( - () => useStateExperimental(2, ((s, _setS)) => ), + () => + useStateExperimental(2, ((s, _setS)) => + + ), ~children, ) ) @@ -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, + , + ); + + Event.dispatch(event, 5); + + let expectedStructure: tree(primitives) = + TreeNode(Root, [TreeNode(B, [TreeLeaf(A(5))])]); + validateStructure(rootNode, expectedStructure); + + updateContainer( + container, + , + ); + + 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, + + + + , + ); + + 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, + + + + , + ); + + 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); @@ -254,4 +333,4 @@ test("useState", () => { TreeNode(Root, [TreeLeaf(A(5))]); validateStructure(rootNode, expectedStructure); }); -}); \ No newline at end of file +});