From a9e63d4dedaad11532eed12332142baf2d1baf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9=20Larivi=C3=A8re?= Date: Thu, 20 Oct 2022 12:08:58 +0200 Subject: [PATCH] Better unit tests --- .../APISketchTests/APISketchTests.fs | 42 ++++++++++++++++++- .../APISketchTests/TestUI.Attributes.fs | 27 ++++++++++++ .../APISketchTests/TestUI.Platform.fs | 15 +++++++ .../APISketchTests/TestUI.Widgets.fs | 11 ++++- 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/Fabulous.Tests/APISketchTests/APISketchTests.fs b/src/Fabulous.Tests/APISketchTests/APISketchTests.fs index 3568d8e0f..9190ba3f8 100644 --- a/src/Fabulous.Tests/APISketchTests/APISketchTests.fs +++ b/src/Fabulous.Tests/APISketchTests/APISketchTests.fs @@ -954,7 +954,7 @@ module ViewHelpers = childButton.Tap() [] - let ``Cascading View.map produces the right result`` () = + let ``Several layers of View.map produce the right result`` () = let mutable expectedMsg = Unchecked.defaultof let childView = @@ -976,7 +976,7 @@ module ViewHelpers = (View.map ParentMessage parentView) .automationId("parentStack") - .tap2(GrandParentTap) + .tapContainer(GrandParentTap) } @@ -1014,3 +1014,41 @@ module ViewHelpers = expectedMsg <- GrandParentTap parentStack.Tap() + + [] + let ``Cascading View.map produce the right result`` () = + let mutable expectedMsg = Unchecked.defaultof + + let childView = + Button("Child button", ChildClick) + .automationId("childButton") + + let parentView = + (View.map ChildMessage childView).tap(ParentTap) + + let grandParentView model = + (View.map ParentMessage parentView) + .tap2(GrandParentTap) + + let init () = true + + let update msg model = + Assert.AreEqual(expectedMsg, msg) + not model + + let program = + StatefulWidget.mkSimpleView init update grandParentView + + let instance = Run.Instance program + let tree = instance.Start() + + let childButton = find tree "childButton" + + expectedMsg <- ParentMessage(ChildMessage ChildClick) + childButton.Press() + + expectedMsg <- ParentMessage ParentTap + childButton.Tap() + + expectedMsg <- GrandParentTap + childButton.Tap2() diff --git a/src/Fabulous.Tests/APISketchTests/TestUI.Attributes.fs b/src/Fabulous.Tests/APISketchTests/TestUI.Attributes.fs index a1e5c2bee..f72e0005b 100644 --- a/src/Fabulous.Tests/APISketchTests/TestUI.Attributes.fs +++ b/src/Fabulous.Tests/APISketchTests/TestUI.Attributes.fs @@ -61,6 +61,32 @@ module TestUI_Attributes = { Key = key; Name = name } + let defineTappable2 name : ScalarAttributeDefinition = + let key = + ScalarAttributeDefinition.CreateAttributeData( + (fun x -> x), + ScalarAttributeComparers.noCompare, + (fun _ newValueOpt node -> + + let btn = node.Target :?> IButton + + match node.TryGetHandler(name) with + | ValueNone -> () + | ValueSome handlerId -> btn.RemoveTap2Listener handlerId + + match newValueOpt with + | ValueNone -> node.SetHandler(name, ValueNone) + + | ValueSome msg -> + let handler () = Dispatcher.dispatch node msg + + let handlerId = btn.AddTap2Listener handler + node.SetHandler(name, ValueSome handlerId)) + ) + |> AttributeDefinitionStore.registerScalar + + { Key = key; Name = name } + let defineContainerTappable name : ScalarAttributeDefinition = let key = ScalarAttributeDefinition.CreateAttributeData( @@ -116,6 +142,7 @@ module TestUI_Attributes = module Button = let Pressed = definePressable "Button_Pressed" let Tap = defineTappable "Button_Tap" + let Tap2 = defineTappable2 "Button_Tap2" module Automation = diff --git a/src/Fabulous.Tests/APISketchTests/TestUI.Platform.fs b/src/Fabulous.Tests/APISketchTests/TestUI.Platform.fs index bac11867a..6e23caff1 100644 --- a/src/Fabulous.Tests/APISketchTests/TestUI.Platform.fs +++ b/src/Fabulous.Tests/APISketchTests/TestUI.Platform.fs @@ -27,6 +27,8 @@ module Platform = abstract RemovePressListener: int -> unit abstract AddTapListener: ButtonHandler -> int abstract RemoveTapListener: int -> unit + abstract AddTap2Listener: ButtonHandler -> int + abstract RemoveTap2Listener: int -> unit type LabelChangeList = | TextSet of string @@ -82,8 +84,10 @@ module Platform = inherit TestViewElement() let mutable pressCounter: int = 1 let mutable tapCounter: int = 1 + let mutable tap2Counter: int = 1 let pressHandlers = Dictionary() let tapHandlers = Dictionary() + let tap2Handlers = Dictionary() member _.Press() = for handler in Array.ofSeq(pressHandlers.Values) do @@ -93,6 +97,10 @@ module Platform = for handler in Array.ofSeq(tapHandlers.Values) do handler() + member _.Tap2() = + for handler in Array.ofSeq(tap2Handlers.Values) do + handler() + interface IText with member val Text = "" with get, set member val TextColor = "" with get, set @@ -112,6 +120,13 @@ module Platform = member this.RemoveTapListener(id) = tapHandlers.Remove(id) |> ignore + member this.AddTap2Listener(handler) = + tap2Handlers.Add(tap2Counter, handler) + tap2Counter <- tap2Counter + 1 + tap2Counter - 1 + + member this.RemoveTap2Listener(id) = tap2Handlers.Remove(id) |> ignore + type TestNumericBag() = inherit TestViewElement() diff --git a/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs b/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs index ee29ebeb1..eaa460a40 100644 --- a/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs +++ b/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs @@ -118,7 +118,16 @@ module TestUI_Widgets = [] - static member inline tap2<'msg, 'marker when 'marker :> TestStackMarker> + static member inline tap2<'msg, 'marker when 'marker :> TestButtonMarker> + ( + this: WidgetBuilder<'msg, 'marker>, + value: 'msg + ) = + this.AddScalar(Attributes.Button.Tap2.WithValue(value)) + + + [] + static member inline tapContainer<'msg, 'marker when 'marker :> TestStackMarker> ( this: WidgetBuilder<'msg, 'marker>, value: 'msg