public void ExecutingShoppingCartCommand_NavigatesToShoppingCart() { var navigateCalled = false; var navigationService = new MockNavigationService(); navigationService.NavigateDelegate = (s, o) => { Assert.AreEqual("ShoppingCart", s); navigateCalled = true; return true; }; var eventAggregator = new MockEventAggregator(); eventAggregator.GetEventDelegate = type => { if (type == typeof(ShoppingCartUpdatedEvent)) return new ShoppingCartUpdatedEvent(); if (type == typeof(ShoppingCartItemUpdatedEvent)) return new ShoppingCartItemUpdatedEvent(); return null; }; var shoppingCartRepository = new MockShoppingCartRepository(); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(null)); var accountService = new MockAccountService(); accountService.GetSignedInUserAsyncDelegate = () => Task.FromResult((UserInfo)null); var target = new ShoppingCartTabUserControlViewModel(shoppingCartRepository, eventAggregator, navigationService, new AlertMessageService(), null, accountService); target.ShoppingCartTabCommand.Execute(); Assert.IsTrue(navigateCalled); }
public void OnNavigatingTo_Search_Results_Page_Without_Search_Term() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); repository.GetFilteredProductsAsyncDelegate = (queryString) => { ReadOnlyCollection<Category> categories; if (queryString == "bike") categories = new ReadOnlyCollection<Category>(new List<Category> { new Category() {Products = new List<Product>() {new Product(){Title = "Bike1", ProductNumber = "1", ImageUri = new Uri("http://image")}}}, new Category() {Products = new List<Product>() {new Product(){Title = "Bike2", ProductNumber = "2", ImageUri = new Uri("http://image")}}}, }); else { categories = new ReadOnlyCollection<Category>(new List<Category> { new Category() {Products = new List<Product>() {new Product(){Title = "Bike1", ProductNumber = "1", ImageUri = new Uri("http://image")}}}, new Category() {Products = new List<Product>() {new Product(){Title = "Bike2", ProductNumber = "2", ImageUri = new Uri("http://image")}}}, new Category() {Products = new List<Product>() {new Product(){Title = "Product3", ProductNumber = "3", ImageUri = new Uri("http://image")}}} }); } return Task.FromResult(categories); }; var target = new SearchResultsPageViewModel(repository, navigationService, new MockSearchPaneService()); var searchTerm = string.Empty; target.OnNavigatedTo(searchTerm, NavigationMode.New, null); Assert.AreEqual(string.Empty, target.SearchTerm); Assert.IsNotNull(target.Results); Assert.AreEqual(3, target.Results.Count); }
public void OnNavigatedTo_When_Service_Not_Available_Then_Pops_Alert() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); var alertService = new MockAlertMessageService(); var resourceLoader = new MockResourceLoader(); bool alertCalled = false; repository.GetProductAsyncDelegate = (productNumber) => { throw new HttpRequestException(); }; repository.GetProductsAsyncDelegate = (subCategoryId) => { throw new HttpRequestException(); }; alertService.ShowAsyncDelegate = (msg, title) => { alertCalled = true; return Task.FromResult(string.Empty); }; var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), alertService, resourceLoader, null, new MockSearchPaneService()); target.OnNavigatedTo("1", NavigationMode.New, null); Assert.IsTrue(alertCalled); }
public async Task SubmitValidOrder_CallsSuccessDialog() { bool successDialogCalled = false; bool errorDialogCalled = false; var navigationService = new MockNavigationService(); var accountService = new MockAccountService() { GetSignedInUserAsyncDelegate = () => Task.FromResult<UserInfo>(new UserInfo()) }; var orderService = new MockOrderService() { // the order is valid, it can be processed ProcessOrderAsyncDelegate = (a, b) => Task.FromResult(true) }; var resourcesService = new MockResourceLoader() { GetStringDelegate = (key) => key }; var alertService = new MockAlertMessageService() { ShowAsyncWithCommandsDelegate = (dialogTitle, dialogMessage, dialogCommands) => { successDialogCalled = dialogTitle.ToLower().Contains("purchased"); errorDialogCalled = !successDialogCalled; return Task.FromResult(successDialogCalled); } }; var target = new CheckoutSummaryPageViewModel(navigationService, orderService, null, null, null, null, accountService, null, resourcesService, alertService); await target.SubmitCommand.Execute(); Assert.IsTrue(successDialogCalled); Assert.IsFalse(errorDialogCalled); }
public async Task SignOut_CallsSignOutinAccountServiceAndRemovesSavedCredentials() { bool closeFlyoutCalled = false; bool accountServiceSignOutCalled = false; bool clearHistoryCalled = false; bool navigateCalled = false; var accountService = new MockAccountService { SignOutDelegate = () => accountServiceSignOutCalled = true, GetSignedInUserAsyncDelegate = () => Task.FromResult(new UserInfo()) }; var navigationService = new MockNavigationService { ClearHistoryDelegate = () => clearHistoryCalled = true, NavigateDelegate = (s, o) => { navigateCalled = true; Assert.AreEqual("Hub", s); return true; } }; var target = new SignOutFlyoutViewModel(accountService, navigationService) { CloseFlyout = () => closeFlyoutCalled = true }; target.Open(null, null); await target.SignOutCommand.Execute(); Assert.IsTrue(accountServiceSignOutCalled); Assert.IsTrue(closeFlyoutCalled); Assert.IsTrue(clearHistoryCalled); Assert.IsTrue(navigateCalled); }
public void ShoppingCartUpdated_WhenShoppingCartChanged() { var navigationService = new MockNavigationService(); var accountService = new MockAccountService(); var shoppingCartRepository = new MockShoppingCartRepository(); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => { ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>()) { Currency = "USD"}; return Task.FromResult(shoppingCart); }; var eventAggregator = new MockEventAggregator(); var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent(); eventAggregator.GetEventDelegate = type => shoppingCartUpdatedEvent; var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, accountService, null, null, null, null, null, eventAggregator); target.OnNavigatedTo(null, NavigationMode.New, null); Assert.AreEqual("$0.00", target.FullPrice); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => { var shoppingCartItems = new ObservableCollection<ShoppingCartItem> { new ShoppingCartItem() { Product = new Product { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 2}, }; ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>(shoppingCartItems)) { Currency = "USD" }; return Task.FromResult(shoppingCart); }; shoppingCartUpdatedEvent.Publish(null); Assert.AreEqual("$200.00", target.FullPrice); }
public void OnNavigatedTo_Fill_Properties_With_Shopping_Cart_Items() { var navigationService = new MockNavigationService(); var shoppingCartRepository = new MockShoppingCartRepository(); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => { ShoppingCart shoppingCart = null; var shoppingCartItems = new ObservableCollection<ShoppingCartItem> { new ShoppingCartItem() {Product = new Product { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1}, new ShoppingCartItem() {Product = new Product { ListPrice = 100, ProductNumber = "p2", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1} }; shoppingCart = new ShoppingCart(shoppingCartItems) { Currency = "USD"}; return Task.FromResult(shoppingCart); }; var eventAggregator = new MockEventAggregator(); eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent(); var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, new MockAccountService(), null, null, null, null, null, eventAggregator); target.OnNavigatedTo(null, NavigationMode.New, null); Assert.AreEqual("$200.00", target.FullPrice); Assert.AreEqual("$100.00", target.TotalDiscount); Assert.AreEqual(2, target.ShoppingCartItemViewModels.Count); }
public void ProductNav_With_Null_Parameter_Does_Not_Navigate() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); navigationService.NavigateDelegate = (pageName, categoryId) => { Assert.Fail(); return false; }; var viewModel = new HubPageViewModel(repository, navigationService, null, null, null); viewModel.ProductNavigationAction.Invoke(null); }
public void OnNavigatedTo_Fill_Items_And_Title() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); var searchPaneService = new MockSearchPaneService(); repository.GetCategoryAsyncDelegate = (categoryId) => { Category category = null; if (categoryId == 1) { category = new Category { Id = categoryId, Title = "CategoryTitle" }; } return Task.FromResult(category); }; repository.GetSubcategoriesAsyncDelegate = (categoryId) => { ReadOnlyCollection<Category> categories = null; if (categoryId == 1) { categories = new ReadOnlyCollection<Category>(new List<Category> { new Category(), new Category(), new Category() }); } return Task.FromResult(categories); }; var viewModel = new GroupDetailPageViewModel(repository, navigationService, null, null, searchPaneService); viewModel.OnNavigatedTo(1, NavigationMode.New, null); Assert.IsNotNull(viewModel.Items); Assert.AreEqual(3, ((ICollection<CategoryViewModel>)viewModel.Items).Count); Assert.AreEqual("CategoryTitle", viewModel.Title); }
public void OnNavigatedTo_Fill_Items_And_SelectedProduct() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); repository.GetProductAsyncDelegate = (productNumber) => { Product product = null; if (productNumber == "1") { product = new Product { ProductNumber = productNumber, SubcategoryId = 1 }; } return Task.FromResult(product); }; repository.GetProductsAsyncDelegate = (subCategoryId) => { ReadOnlyCollection<Product> products = null; if (subCategoryId == 1) { products = new ReadOnlyCollection<Product>(new List<Product> { new Product(){ ProductNumber = "1", ImageUri = new Uri("http://image") }, new Product(){ ProductNumber = "2", ImageUri = new Uri("http://image") }, new Product(){ ProductNumber = "3", ImageUri = new Uri("http://image") } }); } return Task.FromResult(products); }; var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), null, null, null, new MockSearchPaneService()); target.OnNavigatedTo("1", NavigationMode.New, null); Assert.IsNotNull(target.Items); Assert.AreEqual(3, ((IReadOnlyCollection<ProductViewModel>)target.Items).Count); Assert.AreEqual(target.Items.First(), target.SelectedProduct); }
public void OnNavigatedTo_Fill_Properties_No_Shopping_Cart_Items() { var navigationService = new MockNavigationService(); var shoppingCartRepository = new MockShoppingCartRepository(); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => { var shoppingCartItems = new List<ShoppingCartItem>(); shoppingCartItems.Add(new ShoppingCartItem { Quantity = 1, Product = new Product { ListPrice = 200}, Currency = "USD" }); ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>(shoppingCartItems)) { Currency = "USD" }; return Task.FromResult(shoppingCart); }; var eventAggregator = new MockEventAggregator(); eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent(); var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, new MockAccountService(), null, null, null, null, null, eventAggregator); target.OnNavigatedTo(null, NavigationMode.New, null); Assert.AreEqual("$200.00", target.FullPrice); Assert.AreEqual("$100.00", target.TotalDiscount); Assert.AreEqual(1, target.ShoppingCartItemViewModels.Count); }
public async Task SubmitInvalidOrder_CallsErrorDialog() { bool successDialogCalled = false; bool errorDialogCalled = false; var navigationService = new MockNavigationService(); var accountService = new MockAccountService() { GetSignedInUserAsyncDelegate = () => Task.FromResult<UserInfo>(new UserInfo()) }; var orderService = new MockOrderService() { // the order is invalid, it cannot be processed ProcessOrderAsyncDelegate = (a, b) => { var modelValidationResult = new ModelValidationResult(); modelValidationResult.ModelState.Add("someKey", new List<string>() { "the value of someKey is invalid" }); throw new ModelValidationException(modelValidationResult); } }; var resourcesService = new MockResourceLoader() { GetStringDelegate = (key) => key }; var alertService = new MockAlertMessageService() { ShowAsyncDelegate = (dialogTitle, dialogMessage) => { successDialogCalled = dialogTitle.ToLower().Contains("purchased"); errorDialogCalled = !successDialogCalled; return Task.FromResult(successDialogCalled); } }; var target = new CheckoutSummaryPageViewModel(navigationService, orderService, null, null, null, null, accountService, null, resourcesService, alertService); await target.SubmitCommand.Execute(); Assert.IsFalse(successDialogCalled); Assert.IsTrue(errorDialogCalled); }
public void OnNavigatedTo_Fill_RootCategories() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); var searchPaneService = new MockSearchPaneService(); repository.GetCategoriesAsyncDelegate = (maxAmmountOfProducts) => { var categories = new ReadOnlyCollection<Category>(new List<Category>{ new Category(), new Category(), new Category() }); return Task.FromResult(categories); }; var viewModel = new HubPageViewModel(repository, navigationService, null, null, searchPaneService); viewModel.OnNavigatedTo(null, NavigationMode.New, null); Assert.IsNotNull(viewModel.RootCategories); Assert.AreEqual(((ICollection<CategoryViewModel>)viewModel.RootCategories).Count, 3); }
public void ProductNav_With_Valid_Parameter() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); var productToNavigate = new ProductViewModel(new Product() { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image"), Currency = "USD", Title = "My Title", Description = "My Description", }); navigationService.NavigateDelegate = (pageName, categoryId) => { Assert.AreEqual("ItemDetail", pageName); Assert.AreEqual(productToNavigate.ProductNumber, categoryId); return true; }; var viewModel = new HubPageViewModel(repository, navigationService, null, null, null); viewModel.ProductNavigationAction.Invoke(productToNavigate); }
public void DecrementCountCommand_NotExecutable() { var navigationService = new MockNavigationService(); var shoppingCartRepository = new MockShoppingCartRepository(); var eventAggregator = new MockEventAggregator(); eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent(); var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, null, null, null, null, null, null, eventAggregator); target.SelectedItem = new ShoppingCartItemViewModel(new ShoppingCartItem(){ Quantity = 2, Currency = "USD", Product = new Product(), }); Assert.IsTrue(target.DecrementCountCommand.CanExecute()); target.SelectedItem = new ShoppingCartItemViewModel(new ShoppingCartItem() { Quantity = 1, Currency = "USD", Product = new Product() }); Assert.IsFalse(target.DecrementCountCommand.CanExecute()); }
public async Task SettingUseShippingAddressToTrue_CopiesValuesFromShippingAddressToBilling() { var mockAddress = new Address() { FirstName = "TestFirstName", MiddleInitial = "TestMiddleInitial", LastName = "TestLastName", StreetAddress = "TestStreetAddress", OptionalAddress = "TestOptionalAddress", City = "TestCity", State = "TestState", ZipCode = "123456", Phone = "123456" }; var compareAddressesFunc = new Func<Address, Address, bool>((Address a1, Address a2) => { return a1.FirstName == a2.FirstName && a1.MiddleInitial == a2.MiddleInitial && a1.LastName == a2.LastName && a1.StreetAddress == a2.StreetAddress && a1.OptionalAddress == a2.OptionalAddress && a1.City == a2.City && a1.State == a2.State && a1.ZipCode == a2.ZipCode && a1.Phone == a2.Phone; }); var shippingAddressPageViewModel = new MockShippingAddressPageViewModel() { ValidateFormDelegate = () => true, ProcessFormDelegate = () => Task.Delay(0), Address = mockAddress }; var billingAddressPageViewModel = new MockBillingAddressPageViewModel() { ValidateFormDelegate = () => true }; billingAddressPageViewModel.ProcessFormDelegate = () => { // The Address have to be updated before the form is processed Assert.IsTrue(compareAddressesFunc(shippingAddressPageViewModel.Address, billingAddressPageViewModel.Address)); }; var paymentMethodPageViewModel = new MockPaymentMethodPageViewModel() { ValidateFormDelegate = () => true, ProcessFormDelegate = () => Task.Delay(0), }; var accountService = new MockAccountService() { GetSignedInUserAsyncDelegate = () => Task.FromResult(new UserInfo()) }; var orderRepository = new MockOrderRepository() { CreateBasicOrderAsyncDelegate = (userId, shoppingCart, shippingAddress, billingAddress, paymentMethod) => { // The Address information stored in the order must be the same Assert.IsTrue(compareAddressesFunc(shippingAddress, billingAddress)); return Task.FromResult<Order>(new Order()); } }; var shippingMethodService = new MockShippingMethodService() { GetBasicShippingMethodAsyncDelegate = () => Task.FromResult(new ShippingMethod()) }; var shoppingCartRepository = new MockShoppingCartRepository() { GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(null)) }; var navigationService = new MockNavigationService() { NavigateDelegate = (a, b) => true }; var target = new CheckoutHubPageViewModel(navigationService, accountService, orderRepository, shoppingCartRepository, shippingAddressPageViewModel, billingAddressPageViewModel, paymentMethodPageViewModel, null, null, null); target.UseSameAddressAsShipping = true; await target.GoNextCommand.Execute(); }
public async Task ExecuteGoNextCommand_ProcessesFormsAndNavigates_IfViewModelsAreValid() { bool shippingInfoProcessed = false; bool billingInfoProcessed = false; bool paymentInfoProcessed = false; bool navigated = false; var shippingAddressPageViewModel = new MockShippingAddressPageViewModel() { ValidateFormDelegate = () => true, ProcessFormDelegate = () => shippingInfoProcessed = true }; var billingAddressPageViewModel = new MockBillingAddressPageViewModel() { ValidateFormDelegate = () => true, ProcessFormDelegate = () => billingInfoProcessed = true }; var paymentMethodPageViewModel = new MockPaymentMethodPageViewModel() { ValidateFormDelegate = () => true, ProcessFormDelegate = () => paymentInfoProcessed = true }; var accountService = new MockAccountService() { GetSignedInUserAsyncDelegate = () => Task.FromResult(new UserInfo()) }; var orderRepository = new MockOrderRepository() { CreateBasicOrderAsyncDelegate = (a, b, c, d, e) => Task.FromResult(new Order() { Id = 1 }) }; var shoppingCartRepository = new MockShoppingCartRepository() { GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(null)) }; var navigationService = new MockNavigationService() { NavigateDelegate = (a, b) => navigated = true }; var target = new CheckoutHubPageViewModel(navigationService, accountService, orderRepository, shoppingCartRepository, shippingAddressPageViewModel, billingAddressPageViewModel, paymentMethodPageViewModel, null, null, null); await target.GoNextCommand.Execute(); Assert.IsTrue(shippingInfoProcessed); Assert.IsTrue(billingInfoProcessed); Assert.IsTrue(paymentInfoProcessed); Assert.IsTrue(navigated); }
public void GoBack_When_CanGoBack_Is_True() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService(); navigationService.CanGoBackDelegate = () => true; navigationService.GoBackDelegate = () => Assert.IsTrue(true); var viewModel = new GroupDetailPageViewModel(repository, navigationService, null, null, null); bool canExecute = viewModel.GoBackCommand.CanExecute(); if (canExecute) viewModel.GoBackCommand.Execute(); }
public void CheckoutCommand_NotExecutable_IfNoItemsInCart() { var navigationService = new MockNavigationService(); var shoppingCartRepository = new MockShoppingCartRepository(); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult<ShoppingCart>(null); var eventAggregator = new MockEventAggregator(); eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent(); var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, null, null, null, null, null, null, eventAggregator); target.UpdateShoppingCartAsync(null); Assert.IsFalse(target.CheckoutCommand.CanExecute()); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(new Collection<ShoppingCartItem>()){Currency = "USD", FullPrice = 0, TaxRate = 0, TotalDiscount = 0, TotalPrice = 0}); target.UpdateShoppingCartAsync(null); Assert.IsFalse(target.CheckoutCommand.CanExecute()); shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(new ShoppingCart(new Collection<ShoppingCartItem> { new ShoppingCartItem{Product = new Product(), Currency = "USD", Quantity = 0} }) { Currency = "USD", FullPrice = 0, TaxRate = 0, TotalDiscount = 0, TotalPrice = 0 }); target.UpdateShoppingCartAsync(null); Assert.IsTrue(target.CheckoutCommand.CanExecute()); }
public async Task GoBack_When_CanGoBack_Is_True() { var repository = new MockProductCatalogRepository(); var navigationService = new MockNavigationService { CanGoBackDelegate = () => true, GoBackDelegate = () => Assert.IsTrue(true, "I can go back") }; var target = new ItemDetailPageViewModel(repository, navigationService, new MockShoppingCartRepository(), null, null, null, null); bool canExecute = target.GoBackCommand.CanExecute(); if (canExecute) { await target.GoBackCommand.Execute(); } else { Assert.Fail(); } }
public void FailedCallToProductCatalogRepository_ShowsAlert() { var alertCalled = false; var productCatalogRepository = new MockProductCatalogRepository(); var navService = new MockNavigationService(); var searchPaneService = new MockSearchPaneService(); productCatalogRepository.GetCategoriesAsyncDelegate = (maxAmmountOfProducts) => { throw new HttpRequestException(); }; var alertMessageService = new MockAlertMessageService(); alertMessageService.ShowAsyncDelegate = (s, s1) => { alertCalled = true; Assert.AreEqual("Error", s1); return Task.FromResult(string.Empty); }; var target = new HubPageViewModel(productCatalogRepository, navService, alertMessageService, new MockResourceLoader(), searchPaneService); target.OnNavigatedTo(null, NavigationMode.New, null); Assert.IsTrue(alertCalled); }