public void OnNavigatedTo_When_Service_Not_Available_Then_Pops_Alert()
        {
            var repository = new MockProductCatalogRepository();
            var alertService = new MockAlertMessageService();
            var resourceLoader = new MockResourceLoader();

            bool alertCalled = false;
            repository.GetProductAsyncDelegate = (productNumber) =>
                {
                    throw new Exception();
                };

            repository.GetProductsAsyncDelegate = (subCategoryId) =>
                {
                    throw new Exception();
                };

            alertService.ShowAsyncDelegate = (msg, title) =>
                {
                    alertCalled = true;
                    return Task.FromResult(string.Empty);
                };

            var target = new ItemDetailPageViewModel(repository, new MockShoppingCartRepository(), alertService, resourceLoader, null);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = "1", NavigationMode = NavigationMode.New }, null);

            Assert.IsTrue(alertCalled);
        }
        public void OnNavigatedTo_ClearsNavigationHistory()
        {
            bool clearHistoryCalled = false;
            var navigationService = new MockNavigationService();
            navigationService.ClearHistoryDelegate = () =>
            {
                clearHistoryCalled = true;
            };
            var resourcesService = new MockResourceLoader()
            {
                GetStringDelegate = (key) => key
            };
            var target = new OrderConfirmationPageViewModel(resourcesService, navigationService);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.Forward }, null);

            Assert.IsTrue(clearHistoryCalled);
        }
        public void OnNavigateTo_LoadsDefault_IfTryLoadDefaultTrue()
        {
            var defaultAddress = new Address
            {
                FirstName = "FirstName",
                State = "WA"
            };
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetDefaultBillingAddressAsyncDelegate = () => Task.FromResult(defaultAddress);
            var locationService = new MockLocationService();
            var resourceLoader = new MockResourceLoader();
            var target = new BillingAddressUserControlViewModel(checkoutDataRepository, locationService, resourceLoader, null);

            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.IsNull(target.Address.FirstName);

            target.SetLoadDefault(true);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.AreEqual("FirstName", target.Address.FirstName);
        }
        public async Task SubmitInvalidOrder_CallsErrorDialog()
        {
            bool successDialogCalled = false;
            bool errorDialogCalled = false;
            var navigationService = new MockNavigationService();
            var accountService = new MockAccountService()
                {
                    VerifySavedCredentialsAsyncDelegate = () => Task.FromResult<UserInfo>(new UserInfo())
                };
            var orderService = new MockOrderService()
                {
                    // the order is invalid, it cannot be processed
                    ProcessOrderAsyncDelegate = (o) =>
                        {
                            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, resourcesService, alertService, null);
            await target.SubmitCommand.Execute();

            Assert.IsFalse(successDialogCalled);
            Assert.IsTrue(errorDialogCalled);
        }
        public async Task SubmitValidOrder_NavigatesToOrderConfirmation()
        {
            bool navigateCalled = false;
            bool clearCartCalled = false;
            var navigationService = new MockNavigationService();
            navigationService.NavigateDelegate = (s, o) =>
                                                     {
                                                         Assert.AreEqual("OrderConfirmation", s);
                                                         navigateCalled = true;
                                                         return true;
                                                     };
            
            var accountService = new MockAccountService()
                {
                    VerifySavedCredentialsAsyncDelegate = () => Task.FromResult<UserInfo>(new UserInfo())
                };
            var orderService = new MockOrderService()
                {
                    // the order is valid, it can be processed
                    ProcessOrderAsyncDelegate = (o) => Task.FromResult(true)
                };
            var resourcesService = new MockResourceLoader()
                {
                    GetStringDelegate = (key) => key
                };
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.ClearCartAsyncDelegate = () =>
                                                                {
                                                                    clearCartCalled = true;
                                                                    return Task.Delay(0);
                                                                };
            var target = new CheckoutSummaryPageViewModel(navigationService, orderService, null, null, null, shoppingCartRepository, accountService, resourcesService, null, null);
            await target.SubmitCommand.Execute();

            Assert.IsTrue(navigateCalled);
            Assert.IsTrue(clearCartCalled);
        }