Skip to content

Commit

Permalink
Add and implement "when creating a "in 5 lessons" custom chip it will…
Browse files Browse the repository at this point in the history
… be selected and the correct date will be selected" test.
  • Loading branch information
Jonas-Sander committed Nov 26, 2023
1 parent 8a9cf50 commit 1056b25
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
57 changes: 41 additions & 16 deletions app/lib/homework/homework_dialog/homework_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class HwDialogKeys {
static const Key titleTextField = Key("title-field");
static const Key courseTile = Key("course-tile");
static const Key todoUntilTile = Key("todo-until-tile");
static const Key customLessonChipDialogTextField =
Key("custom-lesson-chip-dialog-text-field");
static const Key customLessonChipDialogOkButton =
Key("custom-lesson-chip-dialog-ok-button");
static const Key submissionTile = Key("submission-tile");
static const Key submissionTimeTile = Key("submission-time-tile");
static const Key descriptionField = Key("description-field");
Expand Down Expand Up @@ -332,37 +336,49 @@ class _SaveButton extends StatelessWidget {
}
}

class _TodoUntilPicker extends StatelessWidget {
class _TodoUntilPicker extends StatefulWidget {
final Ready state;
final bool showLessonChips;

const _TodoUntilPicker({required this.state, required this.showLessonChips});
const _TodoUntilPicker({
required this.state,
required this.showLessonChips,
});

@override
Widget build(BuildContext context) {
final bloc = bloc_lib.BlocProvider.of<HomeworkDialogBloc>(context);
State<_TodoUntilPicker> createState() => _TodoUntilPickerState();
}

var inXHoursController = _InXHoursController(
class _TodoUntilPickerState extends State<_TodoUntilPicker> {
late _InXHoursController inXHoursController;

@override
void initState() {
super.initState();
final bloc =
bloc_lib.BlocProvider.of<HomeworkDialogBloc>(context, listen: false);
inXHoursController = _InXHoursController(
onChanged: (selection) => bloc.add(DueDateChanged(selection)),
initialChips: const IListConst([
_ChipSpec(
dueDate: DueDateSelection.nextSchoolday,
),
_ChipSpec(
dueDate: DueDateSelection.inXLessons(1),
// isSelected: true,
),
_ChipSpec(
dueDate: DueDateSelection.inXLessons(2),
),
// _ChipSpec(
// dueDate: DueDateSelection.inXLessons(3),
// isDeletable: true,
// ),
]),
);
if (state.dueDate.selection != null) {
inXHoursController.selectChip(state.dueDate.selection!);
}

@override
Widget build(BuildContext context) {
final bloc = bloc_lib.BlocProvider.of<HomeworkDialogBloc>(context);

if (widget.state.dueDate.selection != null) {
inXHoursController.selectChip(widget.state.dueDate.selection!);
}
return MaxWidthConstraintBox(
child: SafeArea(
Expand All @@ -373,21 +389,21 @@ class _TodoUntilPicker extends StatelessWidget {
children: [
DefaultTextStyle.merge(
style: TextStyle(
color: state.dueDate.error != null ? Colors.red : null,
color: widget.state.dueDate.error != null ? Colors.red : null,
),
child: DatePicker(
key: HwDialogKeys.todoUntilTile,
padding: showLessonChips
padding: widget.showLessonChips
? const EdgeInsets.fromLTRB(12, 12, 12, 5)
: const EdgeInsets.all(12),
selectedDate: state.dueDate.$1?.toDateTime,
selectedDate: widget.state.dueDate.$1?.toDateTime,
selectDate: (newDate) {
bloc.add(DueDateChanged(
DueDateSelection.date(Date.fromDateTime(newDate))));
},
),
),
if (showLessonChips)
if (widget.showLessonChips)
Padding(
padding: const EdgeInsets.only(left: 3.0),
child: _InXHours(controller: inXHoursController),
Expand Down Expand Up @@ -437,6 +453,11 @@ class _LessonChip {
dueDate: dueDate ?? this.dueDate,
);
}

@override
String toString() {
return '_LessonChip(label: $label, isSelected: $isSelected, isDeletable: $isDeletable, dueDate: $dueDate)';
}
}

class _InXHoursController extends ChangeNotifier {
Expand All @@ -446,6 +467,7 @@ class _InXHoursController extends ChangeNotifier {
required IList<_ChipSpec> initialChips,
required this.onChanged,
}) {
print('constructor');
final converted = initialChips
.map((config) => _LessonChip(
label: getName(config.dueDate),
Expand Down Expand Up @@ -496,6 +518,7 @@ class _InXHoursController extends ChangeNotifier {
dueDate: inXLessons,
isDeletable: true,
));
selectChip(inXLessons);
notifyListeners();
}

Expand Down Expand Up @@ -566,6 +589,7 @@ class _InXHours extends StatelessWidget {
context: context,
title: 'Stundenzeit auswählen',
right: AdaptiveDialogAction(
key: HwDialogKeys.customLessonChipDialogOkButton,
title: 'OK',
onPressed: () {
Navigator.pop(context, inXHours);
Expand All @@ -578,6 +602,7 @@ class _InXHours extends StatelessWidget {
SizedBox(
width: 50,
child: TextField(
key: HwDialogKeys.customLessonChipDialogTextField,
maxLength: 2,
textAlign: TextAlign.end,
style: const TextStyle(
Expand Down
34 changes: 32 additions & 2 deletions app/test/homework/homework_dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,26 @@ void main() {
expect(controller.getSelectedLessonChips(), ['Übernächste Stunde']);
expect(controller.getSelectedDueDate(), Date('2023-11-08'));
});
testWidgets(
'when creating a "in 5 lessons" custom chip it will be selected and the correct date will be selected',
(tester) async {
final controller = createController(tester);
controller.addCourse(courseWith(id: 'foo_course'));
controller.addNextLessonDates('foo_course', [
Date('2023-11-06'),
Date('2023-11-08'),
Date('2023-11-10'),
Date('2023-11-13'),
Date('2023-11-15'),
]);
await pumpAndSettleHomeworkDialog(tester,
showDueDateSelectionChips: true);

await controller.createCustomChip(inXLessons: 5);

expect(controller.getSelectedLessonChips(), ['5.-nächste Stunde']);
expect(controller.getSelectedDueDate(), Date('2023-11-15'));
});

testWidgets(
'when pressing the "next schoolday" chip the next schoolday will be selected',
Expand Down Expand Up @@ -630,8 +650,9 @@ class _TestController {
setClockOverride(Clock.fixed(date.toDateTime));
}

Future<void> selectLessonChip(String s) async {
await tester.tap(find.text(s));
Future<void> selectLessonChip(String label) async {
await tester.ensureVisible(find.widgetWithText(InputChip, label));
await tester.tap(find.text(label));
await tester.pumpAndSettle();
}

Expand Down Expand Up @@ -667,6 +688,15 @@ class _TestController {
void addNextLessonDates(String courseId, List<Date> nextDates) {
nextLessonCalculator.datesToReturn = nextDates;
}

Future<void> createCustomChip({required int inXLessons}) async {
await selectLessonChip('Benutzerdefiniert');
await tester.enterText(
find.byKey(HwDialogKeys.customLessonChipDialogTextField),
'$inXLessons');
await tester.tap(find.byKey(HwDialogKeys.customLessonChipDialogOkButton));
await tester.pumpAndSettle();
}
}

// Used temporarily when testing so one can see what happens "on the screen" in
Expand Down

0 comments on commit 1056b25

Please sign in to comment.