|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import '../fakes/test_actions.dart'; |
| 6 | +import '../fakes/test_model.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('payload action', () { |
| 10 | + test('should be json serializable', () { |
| 11 | + final action = TestPayloadAction( |
| 12 | + payload: 'Serializable payload', |
| 13 | + meta: 'Serializable meta', |
| 14 | + error: 'Serializable error'); |
| 15 | + final actual = jsonDecode(jsonEncode(action)); |
| 16 | + final expected = { |
| 17 | + 'payload': 'Serializable payload', |
| 18 | + 'meta': 'Serializable meta', |
| 19 | + 'error': 'Serializable error', |
| 20 | + }; |
| 21 | + expect(actual, equals(expected)); |
| 22 | + }); |
| 23 | + |
| 24 | + test('should serialize nested objects within payload', () { |
| 25 | + final action = TestPayloadAction( |
| 26 | + payload: TestModel( |
| 27 | + id: '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 28 | + title: 'Lorem ipsum dolor sit amet', |
| 29 | + completed: false), |
| 30 | + meta: 'Serializable meta', |
| 31 | + error: 'Serializable error'); |
| 32 | + final actual = jsonDecode(jsonEncode(action)); |
| 33 | + final expected = { |
| 34 | + 'payload': { |
| 35 | + 'id': '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 36 | + 'title': 'Lorem ipsum dolor sit amet', |
| 37 | + 'completed': false |
| 38 | + }, |
| 39 | + 'meta': 'Serializable meta', |
| 40 | + 'error': 'Serializable error', |
| 41 | + }; |
| 42 | + expect(actual, equals(expected)); |
| 43 | + }); |
| 44 | + |
| 45 | + test('should serialize nested objects within meta', () { |
| 46 | + final action = TestPayloadAction( |
| 47 | + payload: 'Serializable payload', |
| 48 | + meta: TestModel( |
| 49 | + id: '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 50 | + title: 'Lorem ipsum dolor sit amet', |
| 51 | + completed: false), |
| 52 | + error: 'Serializable error'); |
| 53 | + final actual = jsonDecode(jsonEncode(action)); |
| 54 | + final expected = { |
| 55 | + 'payload': 'Serializable payload', |
| 56 | + 'meta': { |
| 57 | + 'id': '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 58 | + 'title': 'Lorem ipsum dolor sit amet', |
| 59 | + 'completed': false |
| 60 | + }, |
| 61 | + 'error': 'Serializable error', |
| 62 | + }; |
| 63 | + expect(actual, equals(expected)); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should serialize nested objects within errors', () { |
| 67 | + final action = TestPayloadAction( |
| 68 | + payload: 'Serializable payload', |
| 69 | + meta: 'Serializable meta', |
| 70 | + error: TestModel( |
| 71 | + id: '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 72 | + title: 'Lorem ipsum dolor sit amet', |
| 73 | + completed: false)); |
| 74 | + final actual = jsonDecode(jsonEncode(action)); |
| 75 | + final expected = { |
| 76 | + 'payload': 'Serializable payload', |
| 77 | + 'meta': 'Serializable meta', |
| 78 | + 'error': { |
| 79 | + 'id': '9f4772ab-8756-47e1-89a0-69d3e7a056d5', |
| 80 | + 'title': 'Lorem ipsum dolor sit amet', |
| 81 | + 'completed': false |
| 82 | + }, |
| 83 | + }; |
| 84 | + expect(actual, equals(expected)); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
0 commit comments