Beispiel #1
0
        public void IndexSearchPagingTest(int resultCount)
        {
            var searchServiceFake     = A.Fake <ISearchQueryService <JobProfileIndex> >(ops => ops.Strict());
            var loggerFake            = A.Fake <IApplicationLogger>();
            var asyncHelper           = new AsyncHelper();
            var webAppContextFake     = A.Fake <IWebAppContext>(ops => ops.Strict());
            var defaultJobProfilePage = "/jobprofile-details/";
            var stateManagerFake      = A.Fake <IPreSearchFilterStateManager>(ops => ops.Strict());
            var mapperCfg             = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <JobProfilesAutoMapperProfile>();
            });
            var buildSearchFilterServiceFake = A.Fake <IBuildSearchFilterService>(ops => ops.Strict());

            // Set up calls
            var expectedSearchResultsViewModel = Enumerable.Empty <JobProfileSearchResultItemViewModel>();
            var dummySearchResult    = A.Dummy <SearchResult <JobProfileIndex> >();
            var expectedTotalMessage = resultCount == 1 ? "1 result found" : $"{resultCount} results found";

            var dummyIndex = new JobProfileIndex
            {
                Title                       = nameof(JobProfileIndex.Title),
                AlternativeTitle            = new[] { "alt" },
                SalaryStarter               = 10,
                SalaryExperienced           = 10,
                Overview                    = "overview",
                UrlName                     = "dummy-url",
                JobProfileCategoriesWithUrl = new[] { "CatOneURL|Cat One", "CatTwoURL|Cat Two" }
            };

            dummySearchResult.Count = resultCount;
            var endList = new List <SearchResultItem <JobProfileIndex> >(resultCount);

            for (var i = 0; i < resultCount; i++)
            {
                endList.Add(new SearchResultItem <JobProfileIndex>
                {
                    ResultItem = dummyIndex
                });
            }

            dummySearchResult.Results = endList;

            var expectedVmList = new List <JobProfileSearchResultItemViewModel>();

            foreach (var dummyResult in dummySearchResult.Results)
            {
                expectedVmList.Add(
                    new JobProfileSearchResultItemViewModel
                {
                    ResultItemTitle            = dummyResult.ResultItem.Title,
                    ResultItemAlternativeTitle = string.Join(", ", dummyResult.ResultItem.AlternativeTitle).Trim().TrimEnd(','),
                    ResultItemSalaryRange      = string.Format(new CultureInfo("en-GB", false), "{0:C0} to {1:C0}", dummyResult.ResultItem.SalaryStarter, dummyResult.ResultItem.SalaryExperienced),
                    ResultItemOverview         = dummyResult.ResultItem.Overview,
                    ResultItemUrlName          = $"{defaultJobProfilePage}{dummyResult.ResultItem.UrlName}",
                    Rank  = (int)dummyResult.Rank,
                    Score = dummyResult.Score,
                    JobProfileCategoriesWithUrl = dummyResult.ResultItem.JobProfileCategoriesWithUrl
                });
            }

            expectedSearchResultsViewModel = expectedVmList.AsEnumerable();

            A.CallTo(() => searchServiceFake.SearchAsync(A <string> ._, A <SearchProperties> ._)).Returns(dummySearchResult);
            A.CallTo(() => buildSearchFilterServiceFake.BuildPreSearchFilters(A <PreSearchFiltersResultsModel> ._, A <Dictionary <string, PreSearchFilterLogicalOperator> > ._))
            .Returns(nameof(SearchProperties.FilterBy));
            A.CallTo(() => stateManagerFake.GetPreSearchFilterState()).Returns(new PreSearchFilterState());
            A.CallTo(() => stateManagerFake.GetStateJson()).Returns(string.Empty);
            A.CallTo(() => stateManagerFake.RestoreState(A <string> ._)).DoesNothing();
            A.CallTo(() => stateManagerFake.UpdateSectionState(A <PreSearchFilterSection> ._)).DoesNothing();

            //Instantiate & Act
            var psfSearchController = new PsfSearchController(searchServiceFake, webAppContextFake, mapperCfg.CreateMapper(), asyncHelper, buildSearchFilterServiceFake, stateManagerFake, loggerFake)
            {
                JobProfileDetailsPage = defaultJobProfilePage
            };

            var searchMethodCall = psfSearchController.WithCallTo(c => c.Index(new PsfModel {
                Section = new PsfSection {
                    Options = new List <PsfOption>()
                }
            }, new PsfSearchResultsViewModel(), 1));

            //Assert
            searchMethodCall
            .ShouldRenderView("SearchResult")
            .WithModel <PsfSearchResultsViewModel>(vm =>
            {
                vm.MainPageTitle.Should().Be(psfSearchController.MainPageTitle);
                vm.SecondaryText.Should().BeEquivalentTo(psfSearchController.SecondaryText);
                vm.TotalResultsMessage.Should().Be(expectedTotalMessage);
                vm.SearchResults.Should().NotBeNull();
                vm.SearchResults.Should().BeEquivalentTo(expectedSearchResultsViewModel);
                vm.BackPageUrl.OriginalString.Should().BeEquivalentTo(psfSearchController.BackPageUrl);
                vm.BackPageUrlText.Should().BeEquivalentTo(psfSearchController.BackPageUrlText);
            })
            .AndNoModelErrors();

            A.CallTo(() => searchServiceFake.SearchAsync(A <string> ._, A <SearchProperties> ._)).MustHaveHappened();
            A.CallTo(() => buildSearchFilterServiceFake.BuildPreSearchFilters(A <PreSearchFiltersResultsModel> ._, A <Dictionary <string, PreSearchFilterLogicalOperator> > ._))
            .MustHaveHappened();
            A.CallTo(() => stateManagerFake.RestoreState(A <string> ._)).MustHaveHappened();
            A.CallTo(() => stateManagerFake.GetPreSearchFilterState()).MustHaveHappened();
            A.CallTo(() => stateManagerFake.GetStateJson()).MustHaveHappened();
        }
        public void MatchingSkillsTest(bool skillsAreSelected, int?expectedMatchingCount, bool shouldShowMatchingCount)
        {
            //Set up
            var searchServiceFake     = A.Fake <ISearchQueryService <JobProfileIndex> >(ops => ops.Strict());
            var loggerFake            = A.Fake <IApplicationLogger>();
            var asyncHelper           = new AsyncHelper();
            var webAppContextFake     = A.Fake <IWebAppContext>(ops => ops.Strict());
            var defaultJobProfilePage = "/jobprofile-details/";
            var stateManagerFake      = A.Fake <IPreSearchFilterStateManager>(ops => ops.Strict());
            var mapperCfg             = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <JobProfilesAutoMapperProfile>();
            });
            var buildSearchFilterServiceFake = A.Fake <IBuildSearchFilterService>(ops => ops.Strict());

            var expectedSearchResultsViewModel = Enumerable.Empty <JobProfileSearchResultItemViewModel>();

            var dummySearchResult = A.Dummy <SearchResult <JobProfileIndex> >();

            dummySearchResult.Count = 1;
            var dummyReturnedResults = new List <SearchResultItem <JobProfileIndex> >()
            {
                new SearchResultItem <JobProfileIndex>
                {
                    ResultItem = new JobProfileIndex()
                    {
                        Skills = new List <string>()
                        {
                            "Skill1", "Skill2", "Skill3"
                        },
                        UrlName = "dummy-url",
                    }
                }
            };

            dummySearchResult.Results = dummyReturnedResults;

            var testPreSearchFilterState = new PreSearchFilterState();

            if (skillsAreSelected)
            {
                testPreSearchFilterState.Sections = new List <PreSearchFilterSection>()
                {
                    new PreSearchFilterSection()
                    {
                        SectionDataType = PreSearchFilterType.Skill,
                        Options         = new List <PreSearchFilterOption>()
                        {
                            new PreSearchFilterOption()
                            {
                                OptionKey = "Skill2", IsSelected = true
                            }
                        }
                    }
                };
            }

            A.CallTo(() => searchServiceFake.SearchAsync(A <string> ._, A <SearchProperties> ._)).Returns(dummySearchResult);
            A.CallTo(() => buildSearchFilterServiceFake.GetSearchTerm(A <SearchProperties> ._, A <PreSearchFiltersResultsModel> ._, A <string[]> ._)).Returns("*");
            A.CallTo(() => buildSearchFilterServiceFake.BuildPreSearchFilters(A <PreSearchFiltersResultsModel> ._, A <Dictionary <string, PreSearchFilterLogicalOperator> > ._)).Returns(nameof(SearchProperties.FilterBy));
            A.CallTo(() => stateManagerFake.GetPreSearchFilterState()).Returns(testPreSearchFilterState);
            A.CallTo(() => stateManagerFake.GetStateJson()).Returns(string.Empty);
            A.CallTo(() => stateManagerFake.RestoreState(A <string> ._)).DoesNothing();
            A.CallTo(() => stateManagerFake.UpdateSectionState(A <PreSearchFilterSection> ._)).DoesNothing();

            //Instantiate & Act
            var psfSearchController = new PsfSearchController(searchServiceFake, webAppContextFake, mapperCfg.CreateMapper(), asyncHelper, buildSearchFilterServiceFake, stateManagerFake, loggerFake)
            {
                JobProfileDetailsPage  = defaultJobProfilePage,
                ShowMacthingSkillCount = shouldShowMatchingCount
            };

            var searchMethodCall = psfSearchController.WithCallTo(c => c.Index(new PsfModel {
                Section = new PsfSection {
                    Options = new List <PsfOption>()
                }
            }, new PsfSearchResultsViewModel(), 1));

            //Assert
            searchMethodCall
            .ShouldRenderView("SearchResult")
            .WithModel <PsfSearchResultsViewModel>(vm =>
            {
                vm.SearchResults.FirstOrDefault().MatchingSkillsCount.Should().Be(expectedMatchingCount);
            })
            .AndNoModelErrors();
        }