Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fakes for design time view model #460

Merged
merged 2 commits into from
Apr 21, 2022
Merged
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
8 changes: 1 addition & 7 deletions src/Elmish.WPF.Tests/ViewModelTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ module Extensions =
(?<-) this propName value


let private emptyLoggingArgs =
{ performanceLogThresholdMs = 1
nameChain = ""
log = NullLogger.Instance
logPerformance = NullLogger.Instance }

type internal TestVm<'model, 'msg>(model, bindings) as this =
inherit ViewModel<'model, 'msg>({ initialModel = model; dispatch = (fun x -> this.Dispatch x); loggingArgs = emptyLoggingArgs }, bindings)
inherit ViewModel<'model, 'msg>({ initialModel = model; dispatch = (fun x -> this.Dispatch x); loggingArgs = LoggingViewModelArgs.fake }, bindings)

let pcTriggers = ConcurrentDictionary<string, int>()
let ecTriggers = ConcurrentDictionary<string, int>()
Expand Down
12 changes: 12 additions & 0 deletions src/Elmish.WPF/ViewModelArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ module internal LoggingViewModelArgs =

let map nameChain v = { v with nameChain = nameChain }

let none =
{ performanceLogThresholdMs = 1
log = NullLogger.Instance
logPerformance = NullLogger.Instance
nameChain = "" }


type internal ViewModelArgs<'model, 'msg> =
{ initialModel: 'model
dispatch: 'msg -> unit
Expand All @@ -35,3 +42,8 @@ module internal ViewModelArgs =
{ initialModel = v.initialModel |> mapModel
dispatch = mapMsg >> v.dispatch
loggingArgs = v.loggingArgs }

let simple initialModel =
{ initialModel = initialModel
dispatch = ignore
loggingArgs = LoggingViewModelArgs.none }
11 changes: 1 addition & 10 deletions src/Elmish.WPF/ViewModelModule.fs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
module Elmish.WPF.ViewModel

open Microsoft.Extensions.Logging.Abstractions

/// Creates a design-time view model using the given model and bindings.
let designInstance (model: 'model) (bindings: Binding<'model, 'msg> list) =
let args =
{ initialModel = model
dispatch = ignore
loggingArgs =
{ performanceLogThresholdMs = 1
nameChain = "main"
log = NullLogger.Instance
logPerformance = NullLogger.Instance } }
let args = ViewModelArgs.simple model

ViewModel(args, bindings) |> box