Skip to content
This repository
tag: 4.3.2
Fetching contributors…

Cannot retrieve contributors at this time

file 94 lines (77 sloc) 2.581 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReactiveUI;
using ReactiveUI.Routing;
using Xunit;

namespace Foobar.ViewModels
{
    public interface IFooBarViewModel : IRoutableViewModel {}

    public interface IBazViewModel : IRoutableViewModel {}

    public class FooBarViewModel : ReactiveObject, IFooBarViewModel
    {
        public string UrlPathSegment { get { return "foo"; } }
        public IScreen HostScreen { get; private set; }

        public FooBarViewModel(IScreen hostScreen)
        {
            HostScreen = hostScreen;
        }
    }

    public class BazViewModel : ReactiveObject, IBazViewModel
    {
        public string UrlPathSegment { get { return "foo"; } }
        public IScreen HostScreen { get; private set; }

        public BazViewModel(IScreen hostScreen)
        {
            HostScreen = hostScreen;
        }
    }
}

namespace Foobar.Views
{
    using ViewModels;

    public class FooBarView : IViewFor<IFooBarViewModel>
    {
        object IViewFor.ViewModel { get { return ViewModel; } set { ViewModel = (IFooBarViewModel) value; } }
        public IFooBarViewModel ViewModel { get; set; }
    }

    public interface IBazView : IViewFor<IBazViewModel> {}

    public class BazView : IBazView
    {
        object IViewFor.ViewModel { get { return ViewModel; } set { ViewModel = (IBazViewModel)value; } }
        public IBazViewModel ViewModel { get; set; }
    }
}

namespace ReactiveUI.Routing.Tests
{
    using Foobar.Views;
    using Foobar.ViewModels;

    public class RxRoutingTests : IEnableLogger
    {
[Fact]
        public void ResolveExplicitViewType()
        {
            RxApp.ConfigureServiceLocator(
                (x, _) => (x.Name == "IBazView" ? new BazView() : null),
                (x, _) => null,
                (c, t, k) => { });

            var vm = new BazViewModel(null);

            var result = RxRouting.ResolveView(vm);
            this.Log().Info(result.GetType().FullName);
            Assert.True(result is BazView);
        }

[Fact]
        public void ResolvePureInterfaceType()
        {
            RxApp.ConfigureServiceLocator(
                (x, _) => (x == typeof(IViewFor<IFooBarViewModel>) ? new FooBarView() : null),
                (x, _) => null,
                (c, t, k) => { });

            var vm = new FooBarViewModel(null);

            var result = RxRouting.ResolveView(vm);
            this.Log().Info(result.GetType().FullName);
            Assert.True(result is FooBarView);
        }
    }
}
Something went wrong with that request. Please try again.