cpd_2022_zi/test/unit_tests/input_provider_test.dart

103 lines
3.7 KiB
Dart
Raw Normal View History

2023-03-02 18:49:14 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:smoke_cess_app/providers/input_provider.dart';
2023-03-06 22:15:29 +01:00
import 'package:smoke_cess_app/globals.dart' as globals;
import '../mock/db_mock.dart';
2023-03-02 18:49:14 +01:00
void main() {
2023-03-06 22:15:29 +01:00
WidgetsFlutterBinding.ensureInitialized();
globals.databaseService = DatabaseMock();
2023-03-02 18:49:14 +01:00
group('InputProvider', () {
test('Initial values are correct', () {
late final inputProvider = InputProvider();
expect(inputProvider.sliderValue, equals(50));
expect(inputProvider.textController.text, equals(''));
expect(inputProvider.relapseCategory, equals(''));
2023-03-06 18:36:10 +01:00
expect(inputProvider.getTimeEntry(SleepTimes.wokeUpAt),
equals(const TimeOfDay(hour: 8, minute: 0)));
expect(inputProvider.getTimeEntry(SleepTimes.sleptAt),
equals(const TimeOfDay(hour: 22, minute: 0)));
2023-03-02 18:49:14 +01:00
});
test('Setters work right way', () {
2023-03-06 18:36:10 +01:00
late final inputProvider = InputProvider();
2023-03-02 18:49:14 +01:00
inputProvider.sliderValue = 75;
expect(inputProvider.sliderValue, equals(75));
inputProvider.textController.text = 'Test';
expect(inputProvider.textController.text, equals('Test'));
inputProvider.relapseCategory = 'Test';
expect(inputProvider.relapseCategory, equals('Test'));
2023-03-06 18:36:10 +01:00
inputProvider.setTime(
SleepTimes.wokeUpAt, const TimeOfDay(hour: 7, minute: 0));
expect(inputProvider.getTimeEntry(SleepTimes.wokeUpAt),
equals(const TimeOfDay(hour: 7, minute: 0)));
2023-03-02 18:49:14 +01:00
2023-03-06 18:36:10 +01:00
inputProvider.setTime(
SleepTimes.sleptAt, const TimeOfDay(hour: 23, minute: 0));
expect(inputProvider.getTimeEntry(SleepTimes.sleptAt),
equals(const TimeOfDay(hour: 23, minute: 0)));
2023-03-02 18:49:14 +01:00
});
2023-03-06 18:36:10 +01:00
test('Reset Fields should reset all fields correctly', () async {
2023-03-02 18:49:14 +01:00
final inputProvider = InputProvider();
2023-03-06 18:36:10 +01:00
inputProvider.sliderValue = 44;
inputProvider.textController.text = 'Test';
inputProvider.setTime(
SleepTimes.wokeUpAt, const TimeOfDay(hour: 7, minute: 0));
inputProvider.setTime(
SleepTimes.sleptAt, const TimeOfDay(hour: 23, minute: 0));
2023-03-02 18:49:14 +01:00
2023-03-06 18:36:10 +01:00
final result =
await inputProvider.saveMood(); // calls private function ResetFields
2023-03-02 18:49:14 +01:00
2023-03-06 18:36:10 +01:00
expect(result, equals(1));
expect(inputProvider.sliderValue, equals(50));
expect(inputProvider.textController.text, equals(''));
expect(inputProvider.getTimeEntry(SleepTimes.wokeUpAt),
equals(const TimeOfDay(hour: 8, minute: 0)));
expect(inputProvider.getTimeEntry(SleepTimes.sleptAt),
equals(const TimeOfDay(hour: 22, minute: 0)));
2023-03-02 18:49:14 +01:00
});
test('Save Mood ', () async {
2023-03-06 18:36:10 +01:00
late final inputProvider = InputProvider();
2023-03-02 18:49:14 +01:00
final result = await inputProvider.saveMood();
expect(result, equals(1));
expect(inputProvider.sliderValue, equals(50));
expect(inputProvider.textController.text, equals(''));
});
test('Save Relapse', () async {
2023-03-06 18:36:10 +01:00
late final inputProvider = InputProvider();
2023-03-02 18:49:14 +01:00
final result = await inputProvider.saveRelapse();
expect(result, equals(1));
expect(inputProvider.relapseCategory, equals(''));
expect(inputProvider.textController.text, equals(''));
});
test('Save Sleep', () async {
late final inputProvider = InputProvider();
2023-03-06 18:36:10 +01:00
final result = await inputProvider.saveSleep(
SleepTimes.wokeUpAt, SleepTimes.sleptAt);
2023-03-02 18:49:14 +01:00
expect(result, equals(1));
expect(inputProvider.sliderValue, equals(50));
expect(inputProvider.textController.text, equals(''));
2023-03-06 18:36:10 +01:00
expect(inputProvider.getTimeEntry(SleepTimes.wokeUpAt),
equals(const TimeOfDay(hour: 8, minute: 0)));
expect(inputProvider.getTimeEntry(SleepTimes.sleptAt),
equals(const TimeOfDay(hour: 22, minute: 0)));
2023-03-02 18:49:14 +01:00
});
});
}