public void AddAdvertisement_WithValidAd_AddsCurrentNewspaperItemToNewspapersCollectionInTheAddedAd() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); var paperModel1 = new Newspaper() {Name = "New paper 1"}; var paperItemViewModel1 = new NewspaperItemViewModel(repository) {Model = paperModel1}; var adModel = new Advertisement() {Name = "Ad 1 Name", Text = "Ad 1 text."}; var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; collectionViewModel.Newspapers.Add(paperItemViewModel1); collectionViewModel.CurrentItem.Should().Be(paperItemViewModel1, "The current item is the item that was added."); detailViewModel.ItemViewModel.Should().Be(collectionViewModel.CurrentItem, "The detail view model references the collection's current item."); paperItemViewModel1.Advertisements.Count.Should().Be(0, "No newspapers are referenced in the advertisement."); adItemViewModel.Newspapers.Count.Should().Be(0, "There are no papers in this ad's collection."); // Act paperItemViewModel1.Advertisements.Add(adItemViewModel); // Assert paperItemViewModel1.Advertisements.Count.Should().Be(1, "One item was added"); adItemViewModel.Newspapers.Count.Should().Be(1, "One newspaper reference was added to this advertisement."); adItemViewModel.Newspapers.ToList().First().Should().Be(collectionViewModel.CurrentItem, "The paper added to the advertisment's newspaper collection was the Newspaper collection's CurrentItem"); }
public void AddItemCommand_WhenInvoked_SendsMessageOfTypeAddingItemMessage() { var msgSent = false; var repository = Substitute.For<INewspaperAdRepository>(); Messenger.Default.Register<AddingNewspaperItemMessage>(this, (message) => { msgSent = true; }); // Act var viewModel = new NewspaperDetailViewModel(repository); viewModel.AddItemCommand.Execute(null); // Assert msgSent.Should().Be(true, "The message was sent."); }
public void NewspaperDetailItem_OnCreate_SendsNewspaperDetailViewModelReadyMessage() { // Arrange var messageSent = false; var repository = Substitute.For<INewspaperAdRepository>(); Messenger.Default.Register<NewspaperDetailViewModelReady>(this, (newspaperReady) => { messageSent = true; }); // Act var detailViewModel = new NewspaperDetailViewModel(repository); // Assert messageSent.Should().Be(true, "The message was sent."); }
public void AddItemCommand_WhenInvokedAgainstEmptyNewspapersCollection_ReturnsItemViewModelAsCurrentItemInCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); // Act detailViewModel.AddItemCommand.Execute(null); // Assert detailViewModel.ItemViewModel.Should().Be(collectionViewModel.CurrentItem, "The detail view model should be the CurrentItem in the collection."); }
public void AddingItemMessage_WhenReceived_AddsNewNewspaperToCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); var countBeforeAdd = collectionViewModel.Newspapers.Count; countBeforeAdd.Should().Be(0, "The collection should be empty"); // Act detailViewModel.AddItemCommand.Execute(null); var countAfterAdd = collectionViewModel.Newspapers.Count; // Assert countAfterAdd.Should().Be(1, "One item should have been added."); }
public void AddAdvertisement_WithValidAdvertisement_AddsAdvertisementToAdvertisementsCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var paperModel = new Newspaper(); var adModel = new Advertisement(); var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; var adDetailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = adItemViewModel}; var detailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = new NewspaperItemViewModel(repository) {Model = paperModel}}; // Act detailViewModel.Advertisements.Add(adItemViewModel); // Assert detailViewModel.Advertisements.Count.Should().Be(1, "There should be one item in the collection"); }
public void AddAdvertisement_WithValidAdvertisement_AddsNewspaperToAdvertisementNewspaperssCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var paperModel = new Newspaper(); var adModel = new Advertisement(); var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; var adDetailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = adItemViewModel}; var detailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = new NewspaperItemViewModel(repository) {Model = paperModel}}; // Act detailViewModel.Advertisements.Add(adItemViewModel); // Assert detailViewModel.Advertisements.ToList().First().Newspapers.Contains(detailViewModel.ItemViewModel); }
public void NewspaperDetailItem_WhenNewspaperCurrentItemChangedMessageReceived_ResetsItemViewModelToValueSentInMessage() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); var paperModel1 = new Newspaper() {Name = "New paper 1"}; var paperModel2 = new Newspaper() {Name = "New paper 2"}; var paperItemViewModel1 = new NewspaperItemViewModel(repository) {Model = paperModel1}; var paperItemViewModel2 = new NewspaperItemViewModel(repository) {Model = paperModel2}; collectionViewModel.Newspapers.Add(paperItemViewModel1); collectionViewModel.Newspapers.Add(paperItemViewModel2); collectionViewModel.CurrentItem.Should().Be(paperItemViewModel2); // Act collectionViewModel.CurrentItem = paperItemViewModel1; // Assert detailViewModel.ItemViewModel.Should().Be(paperItemViewModel1); }
public void CancelCommand_WhenInvoked_ResetsNameToCollectionCurrentValue() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); detailViewModel.AddItemCommand.Execute(null); collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel, "The detail model points to the Collection CurrentItem"); var oldName = collectionViewModel.CurrentItem.Name; var newName = "Changed Newspaper Name"; detailViewModel.Name = newName; collectionViewModel.CurrentItem.Name.Should().Be(oldName, "Name in the collection should not change"); detailViewModel.Name.Should().Be(newName, "Alteration is reflected in the detail view model"); // Act detailViewModel.CancelItemCommand.Execute(null); // Assert detailViewModel.Name.Should().Be(oldName, "The Newspaper name should revert back"); collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel, "The entire instance should have reverted back to the original value"); }
public void RemoveNewspaper_WithExistingNewspaperInNewspapersCollection_RemovesNewspaperFromNewspapersCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var advertisementModel = new Advertisement(); var paperModel1 = new Newspaper(); var newspaperItemViewModel1 = new NewspaperItemViewModel(repository) {Model = paperModel1}; var paperModel2 = new Newspaper(); var newspaperItemViewModel2 = new NewspaperItemViewModel(repository) {Model = paperModel2}; var adDetailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = newspaperItemViewModel1}; var detailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = new AdvertisementItemViewModel(repository) {Model = advertisementModel}}; detailViewModel.Newspapers.Add(newspaperItemViewModel1); detailViewModel.Newspapers.Add(newspaperItemViewModel2); detailViewModel.Newspapers.Should().BeEquivalentTo(new List<NewspaperItemViewModel> {newspaperItemViewModel1, newspaperItemViewModel2}); // Act detailViewModel.Newspapers.Remove(newspaperItemViewModel1); // Assert detailViewModel.Newspapers.Should().BeEquivalentTo(new List<NewspaperItemViewModel> {newspaperItemViewModel2}); }
public void Name_WhenNullOrEmpty_SetsAllowSaveToFalse() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var model = new Newspaper(); var itemViewModel = new NewspaperItemViewModel(repository) {Model = model}; var viewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = itemViewModel}; viewModel.AllowSave.Should().Be(true, "We have a valid name to reference."); // Act viewModel.Name = ""; // Assert viewModel.AllowSave.Should().Be(false, "Cannot save an newspaper without a name."); }
public void NewspaperDetailViewModel_WhenCreated_SendsDetailViewModelReadyMessage() { // Arrange var msgSent = false; //NewspaperDetailViewModelReady msg = null; var repository = Substitute.For<INewspaperAdRepository>(); Messenger.Default.Register<NewspaperDetailViewModelReady>(this, (message) => { msgSent = true; }); // Act var viewModel = new NewspaperDetailViewModel(repository); // Assert msgSent.Should().Be(true, "The message was sent."); }
public void RemoveAdvertisement_WithExistingAdvertisementInAdvertisementsCollection_RemovesNewspaperFromAdvertisementRemoved() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var paperModel = new Newspaper(); var adModel1 = new Advertisement(); var adItemViewModel1 = new AdvertisementItemViewModel(repository) {Model = adModel1}; var adModel2 = new Advertisement(); var adItemViewModel2 = new AdvertisementItemViewModel(repository) {Model = adModel2}; var adDetailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = adItemViewModel1}; var detailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = new NewspaperItemViewModel(repository) {Model = paperModel}}; detailViewModel.Advertisements.Add(adItemViewModel1); detailViewModel.Advertisements.Add(adItemViewModel2); detailViewModel.Advertisements.Should().BeEquivalentTo(new List<AdvertisementItemViewModel> {adItemViewModel1, adItemViewModel2}); detailViewModel.Advertisements.ToList().All(a => a.Newspapers.Contains(detailViewModel.ItemViewModel)); // Act detailViewModel.Advertisements.Remove(adItemViewModel1); // Assert detailViewModel.Advertisements.Should().BeEquivalentTo(new List<AdvertisementItemViewModel> {adItemViewModel2}); }
public void SaveCommand_WhenInvoked_UpdatesNewspaperNameInCollectionCurrentItem() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); detailViewModel.AddItemCommand.Execute(null); var originalName = detailViewModel.Name; var newName = "Changed Newspaper Name"; detailViewModel.Name = newName; Newspaper model = null; collectionViewModel.CurrentItem.Name.Should().Be(originalName, "Name does not change until Save command."); detailViewModel.ItemViewModel.Name.Should().Be(originalName, "Name does not change until Save command."); repository.WhenForAnyArgs(r => r.Save(detailViewModel.ItemViewModel.Model)).Do( (newspaper) => { model = (Newspaper) newspaper.Args()[0]; }); // Act detailViewModel.SaveItemCommand.Execute(new SaveNewspaperItemMessage(detailViewModel.ItemViewModel)); // Assert detailViewModel.Name.Should().Be(detailViewModel.ItemViewModel.Name, "ItemViewModel should have been updated"); detailViewModel.ItemViewModel.Should().Be(collectionViewModel.CurrentItem, "CurrentItem should be the same as detail ItemViewModel"); repository.Received().Save(detailViewModel.ItemViewModel.Model); }
public void Name_WhenNotNullOrEmpty_SetsAllowAddToTrue() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var model = new Newspaper(); var itemViewModel = new NewspaperItemViewModel(repository) {Model = model}; // Act var viewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = itemViewModel}; // Assert viewModel.AllowSave.Should().Be(true, "We have a valid name to reference."); }
public void SaveCommand_WhenInvoked_UpdatesCollectionCurrentItemWithNewValue() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); detailViewModel.AddItemCommand.Execute(null); collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel, "The detail model points to the Collection CurrentItem"); var oldName = collectionViewModel.CurrentItem.Name; var newName = "Changed Newspaper Name"; detailViewModel.Name = newName; // Act detailViewModel.SaveItemCommand.Execute(null); // Assert collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel, "The current item in the collection matches the one in the detail view model."); repository.Received().Save(detailViewModel.ItemViewModel.Model); }
public void ViewModel_WithNullItemViewModel_SetsAllowSaveToFalse() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); // Act var viewModel = new NewspaperDetailViewModel(repository); // Assert viewModel.AllowSave.Should().Be(false, "If not item view model set, do not allow a Save to be called."); }