public void WhenSettingTheResponseTextPropertyOnTheModel_ThenAChangeOnAvailableLengthIsNotifiedOnTheViewModel()
        {
            var question = new OpenQuestion { QuestionText = "Question", MaxLength = 25 };
            var viewModel = new OpenQuestionViewModel(question);

            var changeTracker = new PropertyChangeTracker(viewModel);
            question.Response = "1234567890";

            Assert.IsTrue(changeTracker.ChangedProperties.Contains("AvailableLength"));
        }
        public void WhenQuestionnaireIsCreatedWithService_ThenRetrievesQuestionnaireFromService()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);
            var changeTracker = new PropertyChangeTracker(viewModel);

            Assert.AreEqual("Loading", viewModel.CurrentState);
            Assert.AreEqual(0, changeTracker.ChangedProperties.Count());

            callback(CreateQuestionnaireResult());

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("Questionnaire"));
            Assert.AreEqual("Normal", viewModel.CurrentState);
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("CurrentState"));
            CollectionAssert.AreEqual(
                new[] { "Enter your name", "Enter your address" },
                viewModel.Questions.Cast<OpenQuestionViewModel>().Select(q => q.Question.QuestionText).ToArray());
        }
        public void AfterSubmittingQuestionnaire_ThenChangeDataRelatedNotificationsAreFired()
        {
            Action<IOperationResult<Questionnaire>> callback = null;
            Action<IOperationResult> submitCallback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);
            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Callback<Questionnaire, Action<IOperationResult>>(
                (q, sc) => submitCallback = sc);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var tracker = new PropertyChangeTracker(viewModel);
            viewModel.Questionnaire.Name = "TestName";
            viewModel.Questionnaire.Age = 25;
            AnswerAllQuestions(viewModel);

            viewModel.Submit();
            submitCallback(new Mock<IOperationResult>().Object);
            callback(CreateQuestionnaireResult());

            Assert.IsTrue(tracker.ChangedProperties.Contains("Questionnaire"));
        }
        public void AfterResetting_ThenANewQuestionnaireIsCreated()
        {
            QuestionViewModel[] originalQuestions = null;
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);
            callback(CreateQuestionnaireResult());

            originalQuestions = viewModel.Questions.ToArray();

            viewModel.Questionnaire.Name = "TestName";
            viewModel.Questionnaire.Age = 14;
            AnswerAllQuestions(viewModel);

            viewModel.Reset();

            var tracker = new PropertyChangeTracker(viewModel);
            callback(CreateQuestionnaireResult());

            CollectionAssert.AreNotEquivalent(originalQuestions, viewModel.Questions.ToArray());
            CollectionAssert.Contains(tracker.ChangedProperties, "CanSubmit");
        }
        public void WhenServiceIsDoneSubmitting_ThenStateIsSetToNormal()
        {
            Action<IOperationResult<Questionnaire>> callback = null;
            Action<IOperationResult> submitCallback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);
            repository.Setup(r => r.SubmitQuestionnaireAsync(It.IsAny<Questionnaire>(), It.IsAny<Action<IOperationResult>>()))
                .Callback<Questionnaire, Action<IOperationResult>>(
                (q, sc) => submitCallback = sc);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            viewModel.Submit();
            Assert.AreEqual("Submitting", viewModel.CurrentState);

            var tracker = new PropertyChangeTracker(viewModel);

            // Responds as the service would.
            submitCallback(new Mock<IOperationResult>().Object);
            callback(CreateQuestionnaireResult());

            Assert.AreEqual("Normal", viewModel.CurrentState);
            CollectionAssert.Contains(tracker.ChangedProperties, "CanSubmit");
        }
        public void WhenCurrentStateIsModified_ThenViewModelNotifiesUpdate()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var changeTracker = new PropertyChangeTracker(viewModel);

            viewModel.CurrentState = "newState";

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("CurrentState"));
        }
        public void WhenQuestionIsAnswered_ThenQuestionnaireModelNotifiesUpdate()
        {
            Action<IOperationResult<Questionnaire>> callback = null;

            var repository = new Mock<IQuestionnaireRepository>();
            repository.Setup(r => r.GetQuestionnaireAsync(It.IsAny<Action<IOperationResult<Questionnaire>>>()))
                .Callback<Action<IOperationResult<Questionnaire>>>(c => callback = c);

            var viewModel = new QuestionnaireViewModel(repository.Object);

            callback(CreateQuestionnaireResult());

            var question = viewModel.Questions.First() as OpenQuestionViewModel;
            var changeTracker = new PropertyChangeTracker(viewModel);
            question.Question.Response = "some text";

            // Assertions
            Assert.IsTrue(changeTracker.ChangedProperties.Contains("UnansweredQuestions"));
        }