public void WhenNavigatedToWithAToQueryParameter_ThenInitializesToField()
        {
            var emailServiceMock = new Mock<IEmailService>();

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            var uriQuery = new UriQuery();
            uriQuery.Add("To", "*****@*****.**");
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("" + uriQuery.ToString(), UriKind.Relative)));

            Assert.AreEqual("*****@*****.**", viewModel.EmailDocument.To);
        }
        public void WhenSendMessageCommandIsExecuted_ThenSendsMessageThroughService()
        {
            var emailServiceMock = new Mock<IEmailService>();

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("", UriKind.Relative)));

            viewModel.SendEmailCommand.Execute(null);

            Assert.AreEqual("Sending", viewModel.SendState);

            emailServiceMock.Verify(svc => svc.BeginSendEmailDocument(viewModel.EmailDocument, It.IsAny<AsyncCallback>(), null));
        }
        public void WhenNavigatedToWithAReplyToQueryParameter_ThenRepliesToTheAppropriateMessage()
        {
            var replyToEmail = new EmailDocument { From = "*****@*****.**", To = "", Subject = "", Text = "" };

            var emailServiceMock = new Mock<IEmailService>();
            emailServiceMock
                .Setup(svc => svc.GetEmailDocument(replyToEmail.Id))
                .Returns(replyToEmail);

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            var uriQuery = new UriQuery();
            uriQuery.Add("ReplyTo", replyToEmail.Id.ToString("N"));
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("" + uriQuery.ToString(), UriKind.Relative)));

            Assert.AreEqual("*****@*****.**", viewModel.EmailDocument.To);
        }
        public void WhenFinishedSendingMessage_ThenNavigatesBack()
        {
            var sendEmailResultMock = new Mock<IAsyncResult>();

            var emailServiceMock = new Mock<IEmailService>();
            AsyncCallback callback = null;
            emailServiceMock
                .Setup(svc => svc.BeginSendEmailDocument(It.IsAny<EmailDocument>(), It.IsAny<AsyncCallback>(), null))
                .Callback<EmailDocument, AsyncCallback, object>((e, c, o) => { callback = c; })
                .Returns(sendEmailResultMock.Object);
            emailServiceMock
                .Setup(svc => svc.EndSendEmailDocument(sendEmailResultMock.Object))
                .Verifiable();

            var journalMock = new Mock<IRegionNavigationJournal>();
            journalMock.Setup(j => j.GoBack()).Verifiable();

            var navigationServiceMock = new Mock<IRegionNavigationService>();
            navigationServiceMock.SetupGet(svc => svc.Journal).Returns(journalMock.Object);

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(navigationServiceMock.Object, new Uri("", UriKind.Relative)));

            viewModel.SendEmailCommand.Execute(null);

            this.EnqueueConditional(() => callback != null);

            this.EnqueueCallback(
                () =>
                {
                    callback(sendEmailResultMock.Object);
                });

            this.EnqueueCallback(
                () =>
                {
                    Assert.AreEqual("Sent", viewModel.SendState);
                    journalMock.VerifyAll();
                });

            this.EnqueueTestComplete();
        }
        public void WhenCancelling_ThenNavigatesBack()
        {
            var emailServiceMock = new Mock<IEmailService>();

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);

            var journalMock = new Mock<IRegionNavigationJournal>();

            var navigationServiceMock = new Mock<IRegionNavigationService>();
            navigationServiceMock.SetupGet(svc => svc.Journal).Returns(journalMock.Object);

            ((INavigationAware)viewModel).OnNavigatedTo(
                new NavigationContext(
                    navigationServiceMock.Object,
                    new Uri("location", UriKind.Relative)));

            viewModel.CancelEmailCommand.Execute(null);

            journalMock.Verify(j => j.GoBack());
        }
        public void WhenAcceptingConfirmationToNavigateAway_ThenInvokesRequestCallbackWithTrue()
        {
            var emailServiceMock = new Mock<IEmailService>();

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            var uriQuery = new UriQuery();
            uriQuery.Add("To", "*****@*****.**");
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("" + uriQuery.ToString(), UriKind.Relative)));

            InteractionRequestedEventArgs args = null;
            viewModel.ConfirmExitInteractionRequest.Raised += (s, e) => args = e;

            bool? callbackResult = null;
            ((IConfirmNavigationRequest)viewModel).ConfirmNavigationRequest(
                new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("some uri", UriKind.Relative)),
                b => callbackResult = b);

            var confirmation = args.Context as Confirmation;

            confirmation.Confirmed = true;

            args.Callback();

            Assert.IsTrue(callbackResult.Value);
        }
        public void WhenRequestedForVetoOnNavigationAfterSubmitting_ThenDoesNotRaiseInteractionRequest()
        {
            var emailServiceMock = new Mock<IEmailService>();

            var viewModel = new ComposeEmailViewModel(emailServiceMock.Object);
            var uriQuery = new UriQuery();
            uriQuery.Add("To", "*****@*****.**");
            ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("" + uriQuery.ToString(), UriKind.Relative)));

            InteractionRequestedEventArgs args = null;
            viewModel.ConfirmExitInteractionRequest.Raised += (s, e) => args = e;

            viewModel.SendEmailCommand.Execute(null);

            bool? callbackResult = null;
            ((IConfirmNavigationRequest)viewModel).ConfirmNavigationRequest(
                new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("some uri", UriKind.Relative)),
                b => callbackResult = b);

            Assert.IsNull(args);
            Assert.IsTrue(callbackResult.Value);
        }