public ProductPreviewMessage(Product product)
        {
            if (product == null)
                throw new ArgumentNullException("product");

            Product = product;
        }
        public ProductPreviewViewModel(IMessenger messenger, IBasketModel basketModel)
        {
            // Design setup
            if (this.IsInDesignMode)
            {
                Visible = true;

                Product = new Product()
                {
                    ProductId=12345,
                    Name = "Product Name",
                    Description = "Product Description",
                    UpdatedDate = DateTime.Today,
                };
            }

            // Unpack
            MessengerInstance = messenger;
            _basketModel = basketModel;

            // Register Message Handlers
            MessengerInstance.Register<ProductPreviewMessage>(this, HandleProductPreview);

            // Commands
            CloseCommand = new RelayCommand(CloseExecute);
            AddProductCommand = new RelayCommand(AddProductExecute);
        }
        public void ShouldAddProductWhenRecievingAddProductEvent()
        {
            var product = new Product();
            _model.AddItem(product);

            Assert.AreEqual(1, _model.Items.Count());
            Assert.AreEqual(product, _model.Items.First().Product);
        }
        public void ShouldBecomeVisibleAndContainProductWhenAProductPreviewMessageIsRecieved()
        {
            var product = new Product();
            Assert.IsFalse(_viewModel.Visible);
            Assert.IsNull(_viewModel.Product);

            _previewHandle(new ProductPreviewMessage(product));

            Assert.IsTrue(_viewModel.Visible);
            Assert.AreEqual(product, _viewModel.Product);
        }
        public void ShouldRaiseProductPreviewMessageWhenViewCommandIsExecuted()
        {
            var product = new Product();
            var message = (ProductPreviewMessage)null;

            _messenger.Register<ProductPreviewMessage>(this, m => message = m);

            _viewModel.ViewProductCommand.Execute(product);

            Assert.IsNotNull(message);
            Assert.AreEqual(product, message.Product);
        }
        public void ShouldUpdateTotalToReflectPriceOfItems()
        {
            var product1 = new Product() { ProductId = 1, Price = 1 };
            var product2 = new Product() { ProductId = 1, Price = 1 };
            var product3 = new Product() { ProductId = 2, Price = 2 };

            _model.AddItem(product1);
            _model.AddItem(product2);
            _model.AddItem(product3);

            Assert.AreEqual(4, _model.Total);
        }
        public void ShouldAddProductToBasketWhenAddProductCommandIsExecuted()
        {
            var product = new Product();

            _viewModel.Product = product;

            _basketModelMock
                .Setup(m => m.AddItem(product, 1))
                .Verifiable();

            _viewModel.AddProductCommand.Execute(null);

            _basketModelMock
                .Verify();
        }
        public void ShouldIncrementProductCountWhenRecievingAddProductEventForExistingProduct()
        {
            var product1 = new Product() { ProductId = 1 };
            var product2 = new Product() { ProductId = 1 };

            _model.AddItem(product1);
            _model.AddItem(product2);

            Assert.AreEqual(1, _model.Items.Count());

            var productViewModel = _model.Items.First();

            Assert.AreEqual(product1, productViewModel.Product);
            Assert.AreEqual(2, productViewModel.Count);
        }
        public void ShouldLoadProductsWhenSearchEventRecieved()
        {
            var searchTerm = "searchTerm";
            var message = new SearchMessage(searchTerm);
            var products = new List<Product>();
            var product = new Product();

            _productsServiceMock
                .Setup(p => p.GetProducts(searchTerm))
                .Returns(products)
                .Verifiable();

            _messenger.Send(message);
            Utilities.WaitFor(() => _viewModel.Products != null, 5000);

            _productsServiceMock.Verify();
            Assert.AreEqual(products, _viewModel.Products);
        }
        public ProductListViewModel(IMessenger messenger, IProductsService productService)
        {
            // Design Setup
            if (this.IsInDesignMode)
            {
                var product = new Product();
                product.ProductId = 1;
                product.Name = "Product";
                product.Price = 100;

                Products = new Product[] { product, product, product, product, product, product };
            }

            // Unpack
            _productService = productService;
            MessengerInstance = messenger;

            // Events
            MessengerInstance.Register<SearchMessage>(this, HandleSearch);

            // Commands
            ViewProductCommand = new RelayCommand<Product>(ViewProductExecute);
        }
        private void ViewProductExecute(Product product)
        {
            if (product == null)
                throw new ArgumentNullException();

            MessengerInstance.Send(new ProductPreviewMessage(product));
        }
Example #12
0
        public void AddItem(Product product, int count = 1)
        {
            var existingProduct = _products.SingleOrDefault(p => p.Product.Equals(product));
            if (existingProduct != null)
            {
                existingProduct.Count += count;
            }
            else
            {
                _products.Add(new BasketItemModel(product));
            }

            Total = _products.Sum(p => p.Count * p.Product.Price);
        }