|
| 1 | +import 'package:ht_shared/ht_shared.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('SuggestedContentTemplate', () { |
| 6 | + SuggestedContentTemplate createSubject({ |
| 7 | + SuggestionTemplateType type = SuggestionTemplateType.categoriesToFollow, |
| 8 | + SuggestedContentDisplayType displayType = |
| 9 | + SuggestedContentDisplayType.horizontalCardList, |
| 10 | + ContentType suggestedContentType = ContentType.category, |
| 11 | + String? title, |
| 12 | + String? description, |
| 13 | + int? maxItemsToDisplay, |
| 14 | + String? fetchCriteria, |
| 15 | + }) { |
| 16 | + return SuggestedContentTemplate( |
| 17 | + type: type, |
| 18 | + displayType: displayType, |
| 19 | + suggestedContentType: suggestedContentType, |
| 20 | + title: title, |
| 21 | + description: description, |
| 22 | + maxItemsToDisplay: maxItemsToDisplay, |
| 23 | + fetchCriteria: fetchCriteria, |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + test('can be instantiated', () { |
| 28 | + final instance = createSubject(); |
| 29 | + expect(instance, isNotNull); |
| 30 | + expect(instance.type, SuggestionTemplateType.categoriesToFollow); |
| 31 | + expect( |
| 32 | + instance.displayType, SuggestedContentDisplayType.horizontalCardList); |
| 33 | + expect(instance.suggestedContentType, ContentType.category); |
| 34 | + }); |
| 35 | + |
| 36 | + test('supports value equality', () { |
| 37 | + final instanceA = createSubject( |
| 38 | + title: 'Title A', |
| 39 | + ); |
| 40 | + final instanceB = createSubject( |
| 41 | + title: 'Title A', |
| 42 | + ); |
| 43 | + final instanceC = createSubject( |
| 44 | + type: SuggestionTemplateType.sourcesToFollow, |
| 45 | + displayType: SuggestedContentDisplayType.verticalCardList, |
| 46 | + suggestedContentType: ContentType.source, |
| 47 | + title: 'Title C', |
| 48 | + ); |
| 49 | + |
| 50 | + expect(instanceA, equals(instanceB)); |
| 51 | + expect(instanceA, isNot(equals(instanceC))); |
| 52 | + }); |
| 53 | + |
| 54 | + test('props are correct', () { |
| 55 | + final instance = createSubject( |
| 56 | + type: SuggestionTemplateType.countriesToFollow, |
| 57 | + displayType: SuggestedContentDisplayType.grid, |
| 58 | + suggestedContentType: ContentType.country, |
| 59 | + title: 'Explore Countries', |
| 60 | + description: 'Find news from around the world', |
| 61 | + maxItemsToDisplay: 10, |
| 62 | + fetchCriteria: 'popular', |
| 63 | + ); |
| 64 | + |
| 65 | + expect( |
| 66 | + instance.props, |
| 67 | + [ |
| 68 | + SuggestionTemplateType.countriesToFollow, |
| 69 | + 'Explore Countries', |
| 70 | + 'Find news from around the world', |
| 71 | + SuggestedContentDisplayType.grid, |
| 72 | + ContentType.country, |
| 73 | + 10, |
| 74 | + 'popular', |
| 75 | + ], |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + group('fromJson', () { |
| 80 | + test('returns correct SuggestedContentTemplate object', () { |
| 81 | + final json = <String, dynamic>{ |
| 82 | + 'type': 'categories-to-follow', |
| 83 | + 'title': 'Suggested Categories', |
| 84 | + 'description': 'Discover new topics.', |
| 85 | + 'displayType': 'horizontal-card-list', |
| 86 | + 'suggestedContentType': 'category', |
| 87 | + 'maxItemsToDisplay': 5, |
| 88 | + 'fetchCriteria': 'trending', |
| 89 | + }; |
| 90 | + |
| 91 | + final instance = SuggestedContentTemplate.fromJson(json); |
| 92 | + |
| 93 | + expect(instance.type, SuggestionTemplateType.categoriesToFollow); |
| 94 | + expect(instance.title, 'Suggested Categories'); |
| 95 | + expect(instance.description, 'Discover new topics.'); |
| 96 | + expect(instance.displayType, |
| 97 | + SuggestedContentDisplayType.horizontalCardList); |
| 98 | + expect(instance.suggestedContentType, ContentType.category); |
| 99 | + expect(instance.maxItemsToDisplay, 5); |
| 100 | + expect(instance.fetchCriteria, 'trending'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('handles null optional fields', () { |
| 104 | + final json = <String, dynamic>{ |
| 105 | + 'type': 'sources-to-follow', |
| 106 | + 'displayType': 'vertical-card-list', |
| 107 | + 'suggestedContentType': 'source', |
| 108 | + }; |
| 109 | + |
| 110 | + final instance = SuggestedContentTemplate.fromJson(json); |
| 111 | + |
| 112 | + expect(instance.type, SuggestionTemplateType.sourcesToFollow); |
| 113 | + expect(instance.title, isNull); |
| 114 | + expect(instance.description, isNull); |
| 115 | + expect( |
| 116 | + instance.displayType, SuggestedContentDisplayType.verticalCardList); |
| 117 | + expect(instance.suggestedContentType, ContentType.source); |
| 118 | + expect(instance.maxItemsToDisplay, isNull); |
| 119 | + expect(instance.fetchCriteria, isNull); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + group('toJson', () { |
| 124 | + test('returns correct JSON map', () { |
| 125 | + final instance = createSubject( |
| 126 | + type: SuggestionTemplateType.countriesToFollow, |
| 127 | + displayType: SuggestedContentDisplayType.grid, |
| 128 | + suggestedContentType: ContentType.country, |
| 129 | + title: 'Explore Countries', |
| 130 | + description: 'Find news from around the world', |
| 131 | + maxItemsToDisplay: 10, |
| 132 | + fetchCriteria: 'popular', |
| 133 | + ); |
| 134 | + |
| 135 | + final json = instance.toJson(); |
| 136 | + |
| 137 | + expect(json, <String, dynamic>{ |
| 138 | + 'type': 'countries-to-follow', |
| 139 | + 'title': 'Explore Countries', |
| 140 | + 'description': 'Find news from around the world', |
| 141 | + 'displayType': 'grid', |
| 142 | + 'suggestedContentType': 'country', |
| 143 | + 'maxItemsToDisplay': 10, |
| 144 | + 'fetchCriteria': 'popular', |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + test('handles null optional fields', () { |
| 149 | + final instance = createSubject( |
| 150 | + type: SuggestionTemplateType.sourcesToFollow, |
| 151 | + displayType: SuggestedContentDisplayType.textList, |
| 152 | + suggestedContentType: ContentType.source, |
| 153 | + ); |
| 154 | + |
| 155 | + final json = instance.toJson(); |
| 156 | + |
| 157 | + expect(json, <String, dynamic>{ |
| 158 | + 'type': 'sources-to-follow', |
| 159 | + 'displayType': 'text-list', |
| 160 | + 'suggestedContentType': 'source', |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +} |
0 commit comments