Skip to content

Commit

Permalink
Better unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLariviere committed Oct 20, 2022
1 parent fcba7b8 commit a9e63d4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
42 changes: 40 additions & 2 deletions src/Fabulous.Tests/APISketchTests/APISketchTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ module ViewHelpers =
childButton.Tap()

[<Test>]
let ``Cascading View.map produces the right result`` () =
let ``Several layers of View.map produce the right result`` () =
let mutable expectedMsg = Unchecked.defaultof<GrandParentMsg>

let childView =
Expand All @@ -976,7 +976,7 @@ module ViewHelpers =

(View.map ParentMessage parentView)
.automationId("parentStack")
.tap2(GrandParentTap)
.tapContainer(GrandParentTap)
}


Expand Down Expand Up @@ -1014,3 +1014,41 @@ module ViewHelpers =

expectedMsg <- GrandParentTap
parentStack.Tap()

[<Test>]
let ``Cascading View.map produce the right result`` () =
let mutable expectedMsg = Unchecked.defaultof<GrandParentMsg>

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<TestButton> tree "childButton"

expectedMsg <- ParentMessage(ChildMessage ChildClick)
childButton.Press()

expectedMsg <- ParentMessage ParentTap
childButton.Tap()

expectedMsg <- GrandParentTap
childButton.Tap2()
27 changes: 27 additions & 0 deletions src/Fabulous.Tests/APISketchTests/TestUI.Attributes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ module TestUI_Attributes =

{ Key = key; Name = name }

let defineTappable2 name : ScalarAttributeDefinition<obj, obj> =
let key =
ScalarAttributeDefinition.CreateAttributeData<obj, obj>(
(fun x -> x),
ScalarAttributeComparers.noCompare,
(fun _ newValueOpt node ->

let btn = node.Target :?> IButton

match node.TryGetHandler<int>(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<int>(name, ValueSome handlerId))
)
|> AttributeDefinitionStore.registerScalar

{ Key = key; Name = name }

let defineContainerTappable name : ScalarAttributeDefinition<obj, obj> =
let key =
ScalarAttributeDefinition.CreateAttributeData<obj, obj>(
Expand Down Expand Up @@ -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 =
Expand Down
15 changes: 15 additions & 0 deletions src/Fabulous.Tests/APISketchTests/TestUI.Platform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int, ButtonHandler>()
let tapHandlers = Dictionary<int, ButtonHandler>()
let tap2Handlers = Dictionary<int, ButtonHandler>()

member _.Press() =
for handler in Array.ofSeq(pressHandlers.Values) do
Expand All @@ -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
Expand All @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ module TestUI_Widgets =


[<Extension>]
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))


[<Extension>]
static member inline tapContainer<'msg, 'marker when 'marker :> TestStackMarker>
(
this: WidgetBuilder<'msg, 'marker>,
value: 'msg
Expand Down

0 comments on commit a9e63d4

Please sign in to comment.