|
| 1 | +import { gql } from "../../../core"; |
| 2 | +import { compareResultsUsingQuery } from "../compareResults"; |
| 3 | + |
| 4 | +describe("compareResultsUsingQuery", () => { |
| 5 | + it("is importable and a function", () => { |
| 6 | + expect(typeof compareResultsUsingQuery).toBe("function"); |
| 7 | + }); |
| 8 | + |
| 9 | + it("works with a basic single-field query", () => { |
| 10 | + const query = gql` |
| 11 | + query { |
| 12 | + hello |
| 13 | + } |
| 14 | + `; |
| 15 | + |
| 16 | + expect(compareResultsUsingQuery( |
| 17 | + query, |
| 18 | + { hello: "hi" }, |
| 19 | + { hello: "hi" }, |
| 20 | + )).toBe(true); |
| 21 | + |
| 22 | + expect(compareResultsUsingQuery( |
| 23 | + query, |
| 24 | + { hello: "hi", unrelated: 1 }, |
| 25 | + { hello: "hi", unrelated: 100 }, |
| 26 | + )).toBe(true); |
| 27 | + |
| 28 | + expect(compareResultsUsingQuery( |
| 29 | + query, |
| 30 | + { hello: "hi" }, |
| 31 | + { hello: "hey" }, |
| 32 | + )).toBe(false); |
| 33 | + |
| 34 | + expect(compareResultsUsingQuery( |
| 35 | + query, |
| 36 | + {}, |
| 37 | + { hello: "hi" }, |
| 38 | + )).toBe(false); |
| 39 | + |
| 40 | + expect(compareResultsUsingQuery( |
| 41 | + query, |
| 42 | + { hello: "hi" }, |
| 43 | + {}, |
| 44 | + )).toBe(false); |
| 45 | + |
| 46 | + expect(compareResultsUsingQuery( |
| 47 | + query, |
| 48 | + { hello: "hi" }, |
| 49 | + null, |
| 50 | + )).toBe(false); |
| 51 | + |
| 52 | + expect(compareResultsUsingQuery( |
| 53 | + query, |
| 54 | + null, |
| 55 | + { hello: "hi" }, |
| 56 | + )).toBe(false); |
| 57 | + |
| 58 | + expect(compareResultsUsingQuery( |
| 59 | + query, |
| 60 | + null, |
| 61 | + null, |
| 62 | + )).toBe(true); |
| 63 | + |
| 64 | + expect(compareResultsUsingQuery( |
| 65 | + query, |
| 66 | + {}, |
| 67 | + {}, |
| 68 | + )).toBe(true); |
| 69 | + |
| 70 | + expect(compareResultsUsingQuery( |
| 71 | + query, |
| 72 | + { unrelated: "whatever" }, |
| 73 | + { unrelated: "no matter" }, |
| 74 | + )).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("is not confused by properties in different orders", () => { |
| 78 | + const query = gql` |
| 79 | + query { |
| 80 | + a |
| 81 | + b |
| 82 | + c |
| 83 | + } |
| 84 | + `; |
| 85 | + |
| 86 | + expect(compareResultsUsingQuery( |
| 87 | + query, |
| 88 | + { a: 1, b: 2, c: 3 }, |
| 89 | + { b: 2, c: 3, a: 1 }, |
| 90 | + )).toBe(true); |
| 91 | + |
| 92 | + expect(compareResultsUsingQuery( |
| 93 | + query, |
| 94 | + { d: "bogus", a: 1, b: 2, c: 3 }, |
| 95 | + { b: 2, c: 3, a: 1, d: "also bogus" }, |
| 96 | + )).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it("respects the @nonreactive directive on fields", () => { |
| 100 | + const query = gql` |
| 101 | + query { |
| 102 | + a |
| 103 | + b |
| 104 | + c @nonreactive |
| 105 | + } |
| 106 | + `; |
| 107 | + |
| 108 | + expect(compareResultsUsingQuery( |
| 109 | + query, |
| 110 | + { a: 1, b: 2, c: 3 }, |
| 111 | + { a: 1, b: 2, c: "different" }, |
| 112 | + )).toBe(true); |
| 113 | + |
| 114 | + expect(compareResultsUsingQuery( |
| 115 | + query, |
| 116 | + { a: 1, b: 2, c: 3 }, |
| 117 | + { a: "different", b: 2, c: 4 }, |
| 118 | + )).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it("respects the @nonreactive directive on inline fragments", () => { |
| 122 | + const query = gql` |
| 123 | + query { |
| 124 | + a |
| 125 | + ... @nonreactive { |
| 126 | + b |
| 127 | + c |
| 128 | + } |
| 129 | + } |
| 130 | + `; |
| 131 | + |
| 132 | + expect(compareResultsUsingQuery( |
| 133 | + query, |
| 134 | + { a: 1, b: 2, c: 3 }, |
| 135 | + { a: 1, b: 20, c: 30 }, |
| 136 | + )).toBe(true); |
| 137 | + |
| 138 | + expect(compareResultsUsingQuery( |
| 139 | + query, |
| 140 | + { a: 1, b: 2, c: 3 }, |
| 141 | + { a: 10, b: 20, c: 30 }, |
| 142 | + )).toBe(false); |
| 143 | + }); |
| 144 | + |
| 145 | + it("respects the @nonreactive directive on named fragment ...spreads", () => { |
| 146 | + const query = gql` |
| 147 | + query { |
| 148 | + a |
| 149 | + ...BCFragment @nonreactive |
| 150 | + } |
| 151 | +
|
| 152 | + fragment BCFragment on Query { |
| 153 | + b |
| 154 | + c |
| 155 | + } |
| 156 | + `; |
| 157 | + |
| 158 | + expect(compareResultsUsingQuery( |
| 159 | + query, |
| 160 | + { a: 1, b: 2, c: 3 }, |
| 161 | + { a: 1, b: 2, c: 30 }, |
| 162 | + )).toBe(true); |
| 163 | + |
| 164 | + expect(compareResultsUsingQuery( |
| 165 | + query, |
| 166 | + { a: 1, b: 2, c: 3 }, |
| 167 | + { a: 1, b: 20, c: 3 }, |
| 168 | + )).toBe(true); |
| 169 | + |
| 170 | + expect(compareResultsUsingQuery( |
| 171 | + query, |
| 172 | + { a: 1, b: 2, c: 3 }, |
| 173 | + { a: 1, b: 20, c: 30 }, |
| 174 | + )).toBe(true); |
| 175 | + |
| 176 | + expect(compareResultsUsingQuery( |
| 177 | + query, |
| 178 | + { a: 1, b: 2, c: 3 }, |
| 179 | + { a: 10, b: 20, c: 30 }, |
| 180 | + )).toBe(false); |
| 181 | + }); |
| 182 | + |
| 183 | + it("respects the @nonreactive directive on named fragment definitions", () => { |
| 184 | + const query = gql` |
| 185 | + query { |
| 186 | + a |
| 187 | + ...BCFragment |
| 188 | + } |
| 189 | +
|
| 190 | + fragment BCFragment on Query @nonreactive { |
| 191 | + b |
| 192 | + c |
| 193 | + } |
| 194 | + `; |
| 195 | + |
| 196 | + expect(compareResultsUsingQuery( |
| 197 | + query, |
| 198 | + { a: 1, b: 2, c: 3 }, |
| 199 | + { a: 1, b: 2, c: 30 }, |
| 200 | + )).toBe(true); |
| 201 | + |
| 202 | + expect(compareResultsUsingQuery( |
| 203 | + query, |
| 204 | + { a: 1, b: 2, c: 3 }, |
| 205 | + { a: 1, b: 20, c: 3 }, |
| 206 | + )).toBe(true); |
| 207 | + |
| 208 | + expect(compareResultsUsingQuery( |
| 209 | + query, |
| 210 | + { a: 1, b: 2, c: 3 }, |
| 211 | + { a: 1, b: 20, c: 30 }, |
| 212 | + )).toBe(true); |
| 213 | + |
| 214 | + expect(compareResultsUsingQuery( |
| 215 | + query, |
| 216 | + { a: 1, b: 2, c: 3 }, |
| 217 | + { a: 10, b: 20, c: 30 }, |
| 218 | + )).toBe(false); |
| 219 | + }); |
| 220 | + |
| 221 | + it("traverses fragments without @nonreactive", () => { |
| 222 | + const query = gql` |
| 223 | + query { |
| 224 | + a |
| 225 | + ...BCFragment |
| 226 | + } |
| 227 | +
|
| 228 | + fragment BCFragment on Query { |
| 229 | + b |
| 230 | + c |
| 231 | + } |
| 232 | + `; |
| 233 | + |
| 234 | + expect(compareResultsUsingQuery( |
| 235 | + query, |
| 236 | + { a: 1, b: 2, c: 3 }, |
| 237 | + { a: 1, b: 2, c: 3 }, |
| 238 | + )).toBe(true); |
| 239 | + |
| 240 | + expect(compareResultsUsingQuery( |
| 241 | + query, |
| 242 | + { a: 1, b: 2, c: 3 }, |
| 243 | + { c: 3, a: 1, b: 2 }, |
| 244 | + )).toBe(true); |
| 245 | + |
| 246 | + expect(compareResultsUsingQuery( |
| 247 | + query, |
| 248 | + { a: 1, b: 2, c: 3 }, |
| 249 | + { a: 1, b: 2, c: 30 }, |
| 250 | + )).toBe(false); |
| 251 | + |
| 252 | + expect(compareResultsUsingQuery( |
| 253 | + query, |
| 254 | + { a: 1, b: 2, c: 3 }, |
| 255 | + { a: 1, b: 20, c: 3 }, |
| 256 | + )).toBe(false); |
| 257 | + |
| 258 | + expect(compareResultsUsingQuery( |
| 259 | + query, |
| 260 | + { a: 1, b: 2, c: 3 }, |
| 261 | + { a: 1, b: 20, c: 30 }, |
| 262 | + )).toBe(false); |
| 263 | + |
| 264 | + expect(compareResultsUsingQuery( |
| 265 | + query, |
| 266 | + { a: 1, b: 2, c: 3 }, |
| 267 | + { a: 10, b: 20, c: 30 }, |
| 268 | + )).toBe(false); |
| 269 | + }); |
| 270 | + |
| 271 | + it("iterates over array-valued result fields", () => { |
| 272 | + const query = gql` |
| 273 | + query { |
| 274 | + things { |
| 275 | + __typename |
| 276 | + id |
| 277 | + ...ThingDetails |
| 278 | + } |
| 279 | + } |
| 280 | +
|
| 281 | + fragment ThingDetails on Thing { |
| 282 | + stable |
| 283 | + volatile @nonreactive |
| 284 | + } |
| 285 | + `; |
| 286 | + |
| 287 | + const makeThing = (id: string, stable = 1234) => ({ |
| 288 | + __typename: "Thing", |
| 289 | + id, |
| 290 | + stable, |
| 291 | + volatile: Math.random(), |
| 292 | + }); |
| 293 | + |
| 294 | + expect(compareResultsUsingQuery( |
| 295 | + query, |
| 296 | + { things: "abc".split("").map(id => makeThing(id)) }, |
| 297 | + { things: [makeThing("a"), makeThing("b"), makeThing("c")] }, |
| 298 | + )).toBe(true); |
| 299 | + |
| 300 | + expect(compareResultsUsingQuery( |
| 301 | + query, |
| 302 | + { things: "abc".split("").map(id => makeThing(id)) }, |
| 303 | + { things: "not an array" }, |
| 304 | + )).toBe(false); |
| 305 | + |
| 306 | + expect(compareResultsUsingQuery( |
| 307 | + query, |
| 308 | + { things: {} }, |
| 309 | + { things: [] }, |
| 310 | + )).toBe(false); |
| 311 | + |
| 312 | + expect(compareResultsUsingQuery( |
| 313 | + query, |
| 314 | + { things: [] }, |
| 315 | + { things: {} }, |
| 316 | + )).toBe(false); |
| 317 | + |
| 318 | + expect(compareResultsUsingQuery( |
| 319 | + query, |
| 320 | + { things: [] }, |
| 321 | + { things: [] }, |
| 322 | + )).toBe(true); |
| 323 | + |
| 324 | + expect(compareResultsUsingQuery( |
| 325 | + query, |
| 326 | + { things: {} }, |
| 327 | + { things: {} }, |
| 328 | + )).toBe(true); |
| 329 | + |
| 330 | + expect(compareResultsUsingQuery( |
| 331 | + query, |
| 332 | + { things: "ab".split("").map(id => makeThing(id)) }, |
| 333 | + { things: [makeThing("a"), makeThing("b")] }, |
| 334 | + )).toBe(true); |
| 335 | + |
| 336 | + expect(compareResultsUsingQuery( |
| 337 | + query, |
| 338 | + { things: "ab".split("").map(id => makeThing(id)) }, |
| 339 | + { things: [makeThing("b"), makeThing("a")] }, |
| 340 | + )).toBe(false); |
| 341 | + |
| 342 | + expect(compareResultsUsingQuery( |
| 343 | + query, |
| 344 | + { things: "ab".split("").map(id => makeThing(id)) }, |
| 345 | + { things: [makeThing("a"), makeThing("b", 2345)] }, |
| 346 | + )).toBe(false); |
| 347 | + |
| 348 | + expect(compareResultsUsingQuery( |
| 349 | + query, |
| 350 | + { things: "ab".split("").map(id => makeThing(id)) }, |
| 351 | + { things: [makeThing("a", 3456), makeThing("b")] }, |
| 352 | + )).toBe(false); |
| 353 | + |
| 354 | + expect(compareResultsUsingQuery( |
| 355 | + query, |
| 356 | + { things: "ab".split("").map(id => makeThing(id)) }, |
| 357 | + { things: [makeThing("b"), makeThing("a")] }, |
| 358 | + )).toBe(false); |
| 359 | + }); |
| 360 | +}); |
0 commit comments