|
1 | 1 | import 'dart:convert'; |
2 | 2 |
|
3 | 3 | import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:nc_cookbook_api/nc_cookbook_api.dart'; |
4 | 5 | import 'package:nextcloud_cookbook_flutter/src/models/timer.dart'; |
5 | 6 |
|
| 7 | +import 'helpers/translation_helpers.dart'; |
| 8 | + |
6 | 9 | void main() { |
7 | | - const title = 'title'; |
8 | | - const body = 'body'; |
| 10 | + const recipeName = 'title'; |
9 | 11 | const duration = Duration(minutes: 5); |
10 | 12 | final done = DateTime.now().add(duration); |
11 | 13 | const recipeId = '12345678'; |
12 | | - const id = 0; |
| 14 | + const oldId = 0; |
| 15 | + |
| 16 | + const title = recipeName; |
| 17 | + const body = 'body'; |
13 | 18 |
|
14 | | - final timer = Timer.restoreOld(done, id, recipeId, title, duration, recipeId); |
| 19 | + final recipe = Recipe( |
| 20 | + (b) => b |
| 21 | + ..name = recipeName |
| 22 | + ..id = recipeId |
| 23 | + ..dateCreated = DateTime.now().toUtc() |
| 24 | + ..cookTime = duration, |
| 25 | + ); |
| 26 | + final oldTimer = |
| 27 | + Timer.restoreOld(done, oldId, title, body, duration, recipeId); |
| 28 | + final newTimer = Timer(recipe); |
15 | 29 |
|
16 | 30 | final json = |
17 | | - '{"title":"$title","body":"$body","duration":${duration.inMinutes},"done":${done.millisecondsSinceEpoch},"id":$id,"recipeId":"$recipeId"}'; |
| 31 | + '{"title":"$title","body":"$body","duration":${duration.inMinutes},"done":${done.microsecondsSinceEpoch},"id":$oldId,"recipeId":"$recipeId"}'; |
18 | 32 | final orderedJson = |
19 | | - '{"title":"$title","body":"$body","duration":${duration.inMinutes},"id":$id,"done":${done.millisecondsSinceEpoch},"recipeId":"$recipeId"}'; |
| 33 | + '{"title":"$title","body":"$body","duration":${duration.inMinutes},"id":$oldId,"done":${done.microsecondsSinceEpoch},"recipeId":"$recipeId"}'; |
20 | 34 | final oldJson = |
21 | | - '{"title":"$title","body":"$body","duration":${duration.inMinutes},"done":${done.millisecondsSinceEpoch},"id":$id,"recipeId":$recipeId}'; |
| 35 | + '{"title":"$title","body":"$body","duration":${duration.inMinutes},"done":${done.microsecondsSinceEpoch},"id":$oldId,"recipeId":$recipeId}'; |
| 36 | + final newJson = |
| 37 | + '{"recipe":${jsonEncode(recipeToJson(recipe))},"done":"${newTimer.done.toIso8601String()}","id":null}'; |
22 | 38 |
|
23 | | - final newJson = '{"recipe":null,"done":"${done.toIso8601String()}","id":$id}'; |
| 39 | + setUpAll(setupL10n); |
24 | 40 |
|
25 | 41 | group(Timer, () { |
26 | 42 | test('toJson', () { |
27 | | - expect(jsonEncode(timer.toJson()), equals(newJson)); |
| 43 | + expect(jsonEncode(newTimer.toJson()).trim(), equals(newJson)); |
28 | 44 | }); |
29 | 45 |
|
30 | 46 | test('fromJson', () { |
31 | 47 | expect( |
32 | 48 | Timer.fromJson(jsonDecode(json) as Map<String, dynamic>), |
33 | | - isA<Timer>(), |
| 49 | + equals(oldTimer), |
34 | 50 | ); |
35 | 51 | expect( |
36 | 52 | Timer.fromJson(jsonDecode(orderedJson) as Map<String, dynamic>), |
37 | | - isA<Timer>(), |
| 53 | + equals(oldTimer), |
38 | 54 | ); |
| 55 | + |
39 | 56 | expect( |
40 | 57 | Timer.fromJson(jsonDecode(oldJson) as Map<String, dynamic>), |
41 | | - isA<Timer>(), |
| 58 | + equals(oldTimer), |
| 59 | + ); |
| 60 | + |
| 61 | + expect( |
| 62 | + Timer.fromJson(jsonDecode(newJson) as Map<String, dynamic>), |
| 63 | + equals(newTimer), |
42 | 64 | ); |
43 | 65 | }); |
| 66 | + |
| 67 | + test('timer progress', () { |
| 68 | + const duration = Duration(minutes: 5); |
| 69 | + final now = DateTime.now(); |
| 70 | + final done = now.add(duration); |
| 71 | + |
| 72 | + var timer = Timer.restoreOld( |
| 73 | + now, |
| 74 | + oldId, |
| 75 | + title, |
| 76 | + body, |
| 77 | + Duration.zero, |
| 78 | + recipeId, |
| 79 | + ); |
| 80 | + |
| 81 | + expect(timer.remaining, Duration.zero); |
| 82 | + expect(timer.progress, 1.0); |
| 83 | + |
| 84 | + timer = Timer.restoreOld( |
| 85 | + done, |
| 86 | + oldId, |
| 87 | + title, |
| 88 | + body, |
| 89 | + duration, |
| 90 | + recipeId, |
| 91 | + ); |
| 92 | + |
| 93 | + expect(timer.progress, greaterThan(0)); |
| 94 | + expect(timer.progress, lessThanOrEqualTo(1.0)); |
| 95 | + }); |
44 | 96 | }); |
45 | 97 | } |
0 commit comments