Esempio n. 1
0
        public async Task Test_Refresh_Clears_And_Loads_Questions()
        {
            Site = new SiteViewModel(new Site());
            Subject.Questions.Add(new QuestionItemViewModel());
            NetworkApi.Questions(
                (site, order, sort, page, pagesize, filter) => Task.FromResult(new Response <Question>()
            {
                Items = new[]
                {
                    new Question()
                    {
                        Title = "Test",
                        Owner = new ShallowUser()
                        {
                            ProfileImage = "https://example.com"
                        }
                    },
                }
            }));

            await Subject.Refresh.Execute();

            Assert.Collection(Subject.Questions,
                              q => Assert.Equal("Test", q.Title));
        }
Esempio n. 2
0
        public async Task Test_LoadQuestions_Does_Reload_If_The_Site_Is_Different_Than_The_Current_Questions()
        {
            Site = new SiteViewModel(new Site()
            {
                ApiSiteParameter = "stackoverflow"
            });

            NetworkApi.Questions((site, order, sort, page, pagesize, filter) =>
            {
                if (site == "stackoverflow")
                {
                    return(Task.FromResult(new Response <Question>()
                    {
                        Items = new[]
                        {
                            new Question()
                            {
                                Title = "First"
                            },
                        }
                    }));
                }
                else
                {
                    return(Task.FromResult(new Response <Question>()
                    {
                        Items = new[]
                        {
                            new Question()
                            {
                                Title = "Second"
                            },
                        }
                    }));
                }
            });

            await Subject.LoadQuestions.Execute();

            Assert.Collection(Subject.Questions,
                              q => Assert.Equal("First", q.Title));

            Site = new SiteViewModel(new Site()
            {
                ApiSiteParameter = "other"
            });

            await Subject.LoadQuestions.Execute();

            Assert.Collection(Subject.Questions,
                              q => Assert.Equal("Second", q.Title));
        }
Esempio n. 3
0
        public Task Test_LoadQuestions_Waits_For_LoadSites_To_Finish_Executing_If_No_Site_Is_Selected_But_Is_Already_Executing()
        {
            // TODO: Clean up test implementation?
            var loadSitesTask = new TaskCompletionSource <Unit>();
            var testTask      = new TaskCompletionSource <int>();

            Site      = null;
            LoadSites = ReactiveCommand.CreateFromTask(() => loadSitesTask.Task);
            NetworkApi.Questions(
                (site, order, sort, page, pagesize, filter) => Task.FromResult(new Response <Question>()
            {
                Items = new[]
                {
                    new Question()
                    {
                        Owner = new ShallowUser()
                        {
                            DisplayName  = "Test",
                            ProfileImage = "http://example.com"
                        }
                    },
                }
            }));

            // Act
            LoadSites.Execute().Subscribe();

            var loadingObservable = Subject.LoadQuestions.Execute();

            loadingObservable.Subscribe(u =>
            {
                try
                {
                    // Assert
                    Assert.NotEmpty(Subject.Questions);
                    testTask.SetResult(0);
                }
                catch (Exception ex)
                {
                    testTask.SetException(ex);
                }
            }, ex => testTask.SetException(ex));

            // Precondition
            Assert.Empty(Subject.Questions);

            Site = new SiteViewModel();
            loadSitesTask.SetResult(Unit.Default);

            return(testTask.Task);
        }
Esempio n. 4
0
        public async Task Test_LoadQuestions_Executes_LoadSites_If_There_Is_No_Currently_Selected_Site()
        {
            Site = null;
            NetworkApi.Questions((site, order, sort, page, pagesize, filter) => Task.FromResult(new Response <Question>()));

            LoadSites = ReactiveCommand.Create(() =>
            {
                Site = new SiteViewModel();
            });

            await Subject.LoadQuestions.Execute();

            Assert.NotNull(Site);
        }
Esempio n. 5
0
        public async Task Test_LoadQuestions_Loads_From_Currently_Selected_Site()
        {
            var called = false;

            Site = new SiteViewModel(new Site()
            {
                ApiSiteParameter = "stackoverflow"
            });

            NetworkApi.Questions((site, order, sort, page, pagesize, filter) =>
            {
                Assert.Equal("stackoverflow", site);
                called = true;
                return(Task.FromResult(new Response <Question>()));
            });

            await Subject.LoadQuestions.Execute();

            Assert.True(called);
        }
Esempio n. 6
0
        private async Task LoadQuestionsImpl()
        {
            if (Search.SelectedSite == null)
            {
                if (!await Search.LoadSites.IsExecuting.FirstAsync())
                {
                    await Search.LoadSites.Execute();
                }
                else
                {
                    await Search.LoadSites.FirstAsync();
                }
            }
            if (!Questions.Any() || Search.SelectedSite.ApiSiteParameter != LoadedSite)
            {
                LoadedSite = Search.SelectedSite.ApiSiteParameter;
                var response = await NetworkApi.Questions(LoadedSite);

                var questions = response.Items.Select(q => new QuestionItemViewModel(q)).ToArray();
                Questions = new ReactiveList <QuestionItemViewModel>(questions);
            }
        }
Esempio n. 7
0
        public void Test_Refreshes_Questions_When_Site_Is_Changed_After_Activation()
        {
            NetworkApi.Questions((site, order, sort, page, pagesize, filter) =>
            {
                Assert.Equal("othersite", site);
                return(Task.FromResult(new Response <Question>()));
            });
            var api    = new StubINetworkApi();
            var search = new SearchViewModel(networkApi: api);

            Subject = new QuestionsViewModel(search: search, networkApi: NetworkApi);
            Subject.Questions.Add(new QuestionItemViewModel());

            using (Subject.Activator.Activate())
            {
                search.SelectedSite = new SiteViewModel(new Site()
                {
                    ApiSiteParameter = "othersite"
                });

                Assert.Empty(Subject.Questions);
            }
        }