Skip to content

Commit

Permalink
Bugfix where the survey doesn't end with the first occurence of a com…
Browse files Browse the repository at this point in the history
…pletion task (#153)

Co-authored-by: MagginOne <[email protected]>
  • Loading branch information
magginichi and maggin1 authored Nov 18, 2024
1 parent 93815b7 commit 86f840c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/src/navigator/task_navigator.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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];
Expand All @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions test/navigator/navigable_task_navigator_test.dart
Original file line number Diff line number Diff line change
@@ -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<QuestionStep>());

final completionStep0 = navigator.nextStep(step: step0!);
expect(completionStep0, isNotNull);
expect(completionStep0, isA<CompletionStep>());

final completionStep1 = navigator.nextStep(step: completionStep0!);
expect(completionStep1, isNull);
});
}
38 changes: 38 additions & 0 deletions test/navigator/ordered_task_navigator_test.dart
Original file line number Diff line number Diff line change
@@ -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<QuestionStep>());

final completionStep0 = navigator.nextStep(step: step0!);
expect(completionStep0, isNotNull);
expect(completionStep0, isA<CompletionStep>());

final completionStep1 = navigator.nextStep(step: completionStep0!);
expect(completionStep1, isNull);
});
}

0 comments on commit 86f840c

Please sign in to comment.