diff --git a/lib/src/navigator/task_navigator.dart b/lib/src/navigator/task_navigator.dart index 5f0706bf..693f1fcb 100644 --- a/lib/src/navigator/task_navigator.dart +++ b/lib/src/navigator/task_navigator.dart @@ -1,6 +1,7 @@ import 'dart:collection'; import 'package:survey_kit/src/result/question_result.dart'; +import 'package:survey_kit/src/steps/predefined_steps/completion_step.dart'; import 'package:survey_kit/src/steps/step.dart'; import 'package:survey_kit/src/task/task.dart'; @@ -17,6 +18,10 @@ abstract class TaskNavigator { Step? nextInList(Step? step) { final currentIndex = task.steps.indexWhere( (element) => element.stepIdentifier == step?.stepIdentifier); + // end survey if already completed via CompletionStep + if (step is CompletionStep) { + return null; + } return (currentIndex + 1 > task.steps.length - 1) ? null : task.steps[currentIndex + 1]; @@ -38,7 +43,11 @@ abstract class TaskNavigator { history.add(step); } - int get countSteps => task.steps.length; + // Discard count of multiple completion steps and count only one if present + int get countSteps => + task.steps.where((step) => !(step is CompletionStep)).length + + (task.steps.where((step) => step is CompletionStep).isEmpty ? 0 : 1); + int currentStepIndex(Step step) { return task.steps.indexOf(step); } diff --git a/test/navigator/navigable_task_navigator_test.dart b/test/navigator/navigable_task_navigator_test.dart new file mode 100644 index 00000000..82f30038 --- /dev/null +++ b/test/navigator/navigable_task_navigator_test.dart @@ -0,0 +1,36 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:survey_kit/src/answer_format/boolean_answer_format.dart'; +import 'package:survey_kit/src/navigator/navigable_task_navigator.dart'; +import 'package:survey_kit/src/steps/identifier/step_identifier.dart'; +import 'package:survey_kit/src/steps/predefined_steps/completion_step.dart'; +import 'package:survey_kit/src/steps/predefined_steps/question_step.dart'; +import 'package:survey_kit/src/task/navigable_task.dart'; +import 'package:survey_kit/src/task/task.dart'; + +void main() { + test('''Navigable task navigator completes + after first occurence of completion step''', () { + final Task task = NavigableTask( + steps: [ + QuestionStep( + answerFormat: BooleanAnswerFormat( + positiveAnswer: 'Yes', negativeAnswer: 'No')), + CompletionStep( + stepIdentifier: StepIdentifier(id: '111'), title: '', text: ''), + CompletionStep( + stepIdentifier: StepIdentifier(id: '222'), title: '', text: ''), + ], + ); + final NavigableTaskNavigator navigator = NavigableTaskNavigator(task); + final step0 = navigator.firstStep(); + expect(step0, isNotNull); + expect(step0, isA()); + + final completionStep0 = navigator.nextStep(step: step0!); + expect(completionStep0, isNotNull); + expect(completionStep0, isA()); + + final completionStep1 = navigator.nextStep(step: completionStep0!); + expect(completionStep1, isNull); + }); +} diff --git a/test/navigator/ordered_task_navigator_test.dart b/test/navigator/ordered_task_navigator_test.dart new file mode 100644 index 00000000..31461426 --- /dev/null +++ b/test/navigator/ordered_task_navigator_test.dart @@ -0,0 +1,38 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:survey_kit/src/answer_format/boolean_answer_format.dart'; +import 'package:survey_kit/src/navigator/ordered_task_navigator.dart'; +import 'package:survey_kit/src/steps/identifier/step_identifier.dart'; +import 'package:survey_kit/src/steps/predefined_steps/completion_step.dart'; +import 'package:survey_kit/src/steps/predefined_steps/question_step.dart'; +import 'package:survey_kit/src/task/identifier/task_identifier.dart'; +import 'package:survey_kit/src/task/ordered_task.dart'; +import 'package:survey_kit/src/task/task.dart'; + +void main() { + test('''Ordered task navigator completes + after first occurence of completion step''', () { + final Task task = OrderedTask( + id: TaskIdentifier(id: '100'), + steps: [ + QuestionStep( + answerFormat: BooleanAnswerFormat( + positiveAnswer: 'Yes', negativeAnswer: 'No')), + CompletionStep( + stepIdentifier: StepIdentifier(id: '111'), title: '', text: ''), + CompletionStep( + stepIdentifier: StepIdentifier(id: '222'), title: '', text: ''), + ], + ); + final OrderedTaskNavigator navigator = OrderedTaskNavigator(task); + final step0 = navigator.firstStep(); + expect(step0, isNotNull); + expect(step0, isA()); + + final completionStep0 = navigator.nextStep(step: step0!); + expect(completionStep0, isNotNull); + expect(completionStep0, isA()); + + final completionStep1 = navigator.nextStep(step: completionStep0!); + expect(completionStep1, isNull); + }); +}