|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:gql/language.dart'; |
| 3 | + |
| 4 | +import 'package:normalize/normalize.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('Union Type Inline Fragments With Possible Types', () { |
| 8 | + final inlineFragmentQuery = parseString(''' |
| 9 | + query TestQuery { |
| 10 | + booksAndAuthors { |
| 11 | + id |
| 12 | + __typename |
| 13 | + ... on Book { |
| 14 | + title |
| 15 | + } |
| 16 | + ... on Author { |
| 17 | + name |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + '''); |
| 22 | + final namedFragmentQuery = parseString(''' |
| 23 | + query TestQuery { |
| 24 | + booksAndAuthors { |
| 25 | + id |
| 26 | + __typename |
| 27 | + ...BookFragment |
| 28 | + ...AuthorFragment |
| 29 | + } |
| 30 | + } |
| 31 | + fragment BookFragment on Book { |
| 32 | + title |
| 33 | + } |
| 34 | + fragment AuthorFragment on Author { |
| 35 | + name |
| 36 | + } |
| 37 | + '''); |
| 38 | + |
| 39 | + final data = { |
| 40 | + 'booksAndAuthors': [ |
| 41 | + {'id': '123', '__typename': 'Book', 'title': 'My awesome blog post'}, |
| 42 | + {'id': '324', '__typename': 'Author', 'name': 'Nicole'} |
| 43 | + ] |
| 44 | + }; |
| 45 | + final dataDeserializedWithoutPossibleTypes = { |
| 46 | + 'booksAndAuthors': [ |
| 47 | + { |
| 48 | + 'id': '123', |
| 49 | + '__typename': 'Book', |
| 50 | + 'title': 'My awesome blog post', |
| 51 | + 'name': null |
| 52 | + }, |
| 53 | + {'id': '324', '__typename': 'Author', 'name': 'Nicole', 'title': null} |
| 54 | + ] |
| 55 | + }; |
| 56 | + |
| 57 | + final normalizedMapWithPossibleTypes = { |
| 58 | + 'Query': { |
| 59 | + 'booksAndAuthors': [ |
| 60 | + {'\$ref': 'Book:123'}, |
| 61 | + {'\$ref': 'Author:324'} |
| 62 | + ] |
| 63 | + }, |
| 64 | + 'Book:123': { |
| 65 | + 'id': '123', |
| 66 | + '__typename': 'Book', |
| 67 | + 'title': 'My awesome blog post' |
| 68 | + }, |
| 69 | + 'Author:324': {'id': '324', '__typename': 'Author', 'name': 'Nicole'} |
| 70 | + }; |
| 71 | + final normalizedMapWithoutPossibleTypes = { |
| 72 | + 'Query': { |
| 73 | + 'booksAndAuthors': [ |
| 74 | + {'\$ref': 'Book:123'}, |
| 75 | + {'\$ref': 'Author:324'} |
| 76 | + ] |
| 77 | + }, |
| 78 | + 'Book:123': { |
| 79 | + 'id': '123', |
| 80 | + '__typename': 'Book', |
| 81 | + 'title': 'My awesome blog post', |
| 82 | + 'name': null |
| 83 | + }, |
| 84 | + 'Author:324': { |
| 85 | + 'id': '324', |
| 86 | + '__typename': 'Author', |
| 87 | + 'name': 'Nicole', |
| 88 | + 'title': null |
| 89 | + } |
| 90 | + }; |
| 91 | + final possibleTypes = { |
| 92 | + 'BookAndAuthor': {'Book', 'Author'} |
| 93 | + }; |
| 94 | + test('Produces same normalized object with possible types', () { |
| 95 | + final inlineNormalizedResult = {}; |
| 96 | + normalizeOperation( |
| 97 | + read: (dataId) => inlineNormalizedResult[dataId], |
| 98 | + write: (dataId, value) => inlineNormalizedResult[dataId] = value, |
| 99 | + document: inlineFragmentQuery, |
| 100 | + data: data, |
| 101 | + possibleTypes: possibleTypes, |
| 102 | + ); |
| 103 | + final namedFragmentNormalizedResult = {}; |
| 104 | + normalizeOperation( |
| 105 | + read: (dataId) => namedFragmentNormalizedResult[dataId], |
| 106 | + write: (dataId, value) => namedFragmentNormalizedResult[dataId] = value, |
| 107 | + document: inlineFragmentQuery, |
| 108 | + data: data, |
| 109 | + possibleTypes: possibleTypes, |
| 110 | + ); |
| 111 | + |
| 112 | + expect( |
| 113 | + inlineNormalizedResult, |
| 114 | + equals(namedFragmentNormalizedResult), |
| 115 | + ); |
| 116 | + expect( |
| 117 | + inlineNormalizedResult, |
| 118 | + equals(normalizedMapWithPossibleTypes), |
| 119 | + ); |
| 120 | + }); |
| 121 | + test('Produces same normalized object without possible types', () { |
| 122 | + final inlineNormalizedResult = {}; |
| 123 | + normalizeOperation( |
| 124 | + read: (dataId) => inlineNormalizedResult[dataId], |
| 125 | + write: (dataId, value) => inlineNormalizedResult[dataId] = value, |
| 126 | + document: inlineFragmentQuery, |
| 127 | + data: data, |
| 128 | + ); |
| 129 | + final namedFragmentNormalizedResult = {}; |
| 130 | + normalizeOperation( |
| 131 | + read: (dataId) => namedFragmentNormalizedResult[dataId], |
| 132 | + write: (dataId, value) => namedFragmentNormalizedResult[dataId] = value, |
| 133 | + document: inlineFragmentQuery, |
| 134 | + data: data, |
| 135 | + ); |
| 136 | + |
| 137 | + expect( |
| 138 | + inlineNormalizedResult, |
| 139 | + equals(namedFragmentNormalizedResult), |
| 140 | + ); |
| 141 | + expect( |
| 142 | + inlineNormalizedResult, |
| 143 | + equals(normalizedMapWithoutPossibleTypes), |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + test('Produces correct nested data object with possible types', () { |
| 148 | + expect( |
| 149 | + denormalizeOperation( |
| 150 | + document: inlineFragmentQuery, |
| 151 | + read: (dataId) => normalizedMapWithPossibleTypes[dataId], |
| 152 | + possibleTypes: possibleTypes, |
| 153 | + ), |
| 154 | + equals( |
| 155 | + denormalizeOperation( |
| 156 | + document: namedFragmentQuery, |
| 157 | + read: (dataId) => normalizedMapWithPossibleTypes[dataId], |
| 158 | + possibleTypes: possibleTypes, |
| 159 | + ), |
| 160 | + ), |
| 161 | + ); |
| 162 | + expect( |
| 163 | + denormalizeOperation( |
| 164 | + document: inlineFragmentQuery, |
| 165 | + read: (dataId) => normalizedMapWithPossibleTypes[dataId], |
| 166 | + possibleTypes: possibleTypes, |
| 167 | + ), |
| 168 | + equals(data), |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + test('Produces correct nested data object without possible types', () { |
| 173 | + expect( |
| 174 | + denormalizeOperation( |
| 175 | + document: inlineFragmentQuery, |
| 176 | + read: (dataId) => normalizedMapWithoutPossibleTypes[dataId], |
| 177 | + ), |
| 178 | + equals( |
| 179 | + denormalizeOperation( |
| 180 | + document: namedFragmentQuery, |
| 181 | + read: (dataId) => normalizedMapWithoutPossibleTypes[dataId], |
| 182 | + ), |
| 183 | + ), |
| 184 | + ); |
| 185 | + expect( |
| 186 | + denormalizeOperation( |
| 187 | + document: inlineFragmentQuery, |
| 188 | + read: (dataId) => normalizedMapWithoutPossibleTypes[dataId], |
| 189 | + ), |
| 190 | + equals(dataDeserializedWithoutPossibleTypes), |
| 191 | + ); |
| 192 | + }); |
| 193 | + }); |
| 194 | +} |
0 commit comments