Skip to content

Commit

Permalink
Merge pull request #1695 from unoplatform/dev/dr/vmCommands
Browse files Browse the repository at this point in the history
test(mvux): Ensure that feed parameters shares the same context as the Model
  • Loading branch information
dr1rrb authored Aug 11, 2023
2 parents 08d3b2b + 9bed523 commit 37a29c3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions.Reactive.UI;
using Uno.UI.RuntimeTests;
using Button = Microsoft.UI.Xaml.Controls.Button;

namespace Uno.Extensions.Reactive.WinUI.Tests.Generator;

[TestClass]
[RunsOnUIThread]
public partial class Given_VMWithCommands
{
public partial class When_ParameterFeed_Then_SubscribedWithSameContext_ViewModel
{
public int FeedInvokeCount { get; private set; }

public int CommandLastParameter { get; private set; } = -1;

public IFeed<int> MyFeed => Feed.Async(async ct => ++FeedInvokeCount);

public void DoSomething(int myFeed, CancellationToken ct)
=> CommandLastParameter = myFeed;
}

[TestMethod]
public async Task When_ParameterFeed_Then_SubscribedWithSameContext()
{
var vm = new BindableWhen_ParameterFeed_Then_SubscribedWithSameContext_ViewModel();

await UIHelper.WaitFor(() => vm.MyFeed != 0, default);

var current = vm.MyFeed;
vm.DoSomething.Execute(null);

await UIHelper.WaitFor(() => vm.Model.CommandLastParameter != -1, default);

Assert.AreEqual(1, vm.FeedInvokeCount);
Assert.AreEqual(current, vm.Model.CommandLastParameter);
}

[TestMethod]
public async Task When_ParameterFeed_Then_SubscribedWithSameContext_UsingUI()
{
FeedView view;
Button doSomething;
var vm = new BindableWhen_ParameterFeed_Then_SubscribedWithSameContext_ViewModel();
var ui = new StackPanel
{
DataContext = vm,
Children =
{
(view = new FeedView()),
(doSomething = new Button())
}
};

view.SetBinding(FeedView.SourceProperty, new Binding { Path = new PropertyPath("MyFeed") });
doSomething.SetBinding(Button.CommandProperty, new Binding { Path = new PropertyPath("DoSomething") });

await UIHelper.Load(ui, default);

await UIHelper.WaitFor(() => vm.MyFeed is not 0, default);

var current = vm.MyFeed;
doSomething.Command.Execute(null);

await UIHelper.WaitFor(() => vm.Model.CommandLastParameter != -1, default);

Assert.AreEqual(1, vm.FeedInvokeCount);
Assert.AreEqual(current, vm.Model.CommandLastParameter);
}
}
12 changes: 11 additions & 1 deletion src/Uno.Extensions.Reactive/Utils/FeedUIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ public static async IAsyncEnumerable<Message<T>> GetSource<T>(IFeed<T> feed, Sou
}
}

private static async ValueTask<IAsyncEnumerator<T>> GetSourceEnumerator<T>(ISignal<T> signal, SourceContext context)
private static async ValueTask<IAsyncEnumerator<Message<T>>> GetSourceEnumerator<T>(IFeed<T> signal, SourceContext context)
{
var ct = context.Token;
var src = DispatcherHelper.HasThreadAccess
? await Task.Run(() => context.GetOrCreateSource(signal), ct).ConfigureAwait(false)
: context.GetOrCreateSource(signal);

return src.GetAsyncEnumerator(ct);
}

private static async ValueTask<IAsyncEnumerator<TMessage>> GetSourceEnumerator<TMessage>(ISignal<TMessage> signal, SourceContext context)
{
var ct = context.Token;
var src = DispatcherHelper.HasThreadAccess
Expand Down

0 comments on commit 37a29c3

Please sign in to comment.