|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import fetchMock from "fetch-mock"; |
| 2 | +import fetchMock, { type CallLog } from "fetch-mock"; |
3 | 3 | import { getUserAgent } from "universal-user-agent"; |
4 | 4 |
|
5 | 5 | import { graphql } from "../src"; |
@@ -323,4 +323,164 @@ describe("graphql()", () => { |
323 | 323 | ); |
324 | 324 | }); |
325 | 325 | }); |
| 326 | + |
| 327 | + describe("When using a query with multiple operations", () => { |
| 328 | + const mockData = { |
| 329 | + repository: { |
| 330 | + issues: { |
| 331 | + edges: [ |
| 332 | + { |
| 333 | + node: { |
| 334 | + title: "Foo", |
| 335 | + }, |
| 336 | + }, |
| 337 | + { |
| 338 | + node: { |
| 339 | + title: "Bar", |
| 340 | + }, |
| 341 | + }, |
| 342 | + { |
| 343 | + node: { |
| 344 | + title: "Baz", |
| 345 | + }, |
| 346 | + }, |
| 347 | + ], |
| 348 | + }, |
| 349 | + }, |
| 350 | + }; |
| 351 | + |
| 352 | + const mockErrors = { |
| 353 | + errors: [{ message: "An operation name is required" }], |
| 354 | + data: undefined, |
| 355 | + status: 400, |
| 356 | + }; |
| 357 | + |
| 358 | + const query = /* GraphQL */ ` |
| 359 | + query Blue($last: Int) { |
| 360 | + repository(owner: "blue", name: "graphql.js") { |
| 361 | + issues(last: $last) { |
| 362 | + edges { |
| 363 | + node { |
| 364 | + title |
| 365 | + } |
| 366 | + } |
| 367 | + } |
| 368 | + } |
| 369 | + } |
| 370 | +
|
| 371 | + query Green($last: Int) { |
| 372 | + repository(owner: "green", name: "graphql.js") { |
| 373 | + issues(last: $last) { |
| 374 | + edges { |
| 375 | + node { |
| 376 | + title |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | + } |
| 382 | + `.trim(); |
| 383 | + |
| 384 | + it("Sends both queries to the server (which will respond with bad request)", () => { |
| 385 | + const fetch = fetchMock.createInstance(); |
| 386 | + |
| 387 | + fetch.post("https://api.github.com/graphql", mockErrors, { |
| 388 | + method: "POST", |
| 389 | + headers: { |
| 390 | + accept: "application/vnd.github.v3+json", |
| 391 | + authorization: "token secret123", |
| 392 | + "user-agent": userAgent, |
| 393 | + }, |
| 394 | + matcherFunction: (callLog: CallLog) => { |
| 395 | + const expected = { |
| 396 | + query: query, |
| 397 | + variables: { last: 3 }, |
| 398 | + }; |
| 399 | + const result = callLog.options.body === JSON.stringify(expected); |
| 400 | + if (!result) { |
| 401 | + console.warn("Body did not match expected value", { |
| 402 | + expected, |
| 403 | + actual: JSON.parse(callLog.options.body as string), |
| 404 | + }); |
| 405 | + } |
| 406 | + return result; |
| 407 | + }, |
| 408 | + }); |
| 409 | + |
| 410 | + return new Promise<void>((res, rej) => |
| 411 | + graphql(query, { |
| 412 | + headers: { |
| 413 | + authorization: `token secret123`, |
| 414 | + }, |
| 415 | + request: { |
| 416 | + fetch: fetch.fetchHandler, |
| 417 | + }, |
| 418 | + last: 3, |
| 419 | + }) |
| 420 | + .then(() => { |
| 421 | + rej("Should have thrown an error"); |
| 422 | + }) |
| 423 | + .catch((result) => { |
| 424 | + expect(JSON.stringify(result.response)).toStrictEqual( |
| 425 | + JSON.stringify(mockErrors), |
| 426 | + ); |
| 427 | + res(); |
| 428 | + }), |
| 429 | + ); |
| 430 | + }); |
| 431 | + |
| 432 | + it('Allows the user to specify the operation name in the "operationName" option', () => { |
| 433 | + const fetch = fetchMock.createInstance(); |
| 434 | + |
| 435 | + fetch.post( |
| 436 | + "https://api.github.com/graphql", |
| 437 | + { data: mockData }, |
| 438 | + { |
| 439 | + method: "POST", |
| 440 | + headers: { |
| 441 | + accept: "application/vnd.github.v3+json", |
| 442 | + authorization: "token secret123", |
| 443 | + "user-agent": userAgent, |
| 444 | + }, |
| 445 | + matcherFunction: (callLog: CallLog) => { |
| 446 | + const expected = { |
| 447 | + query: query, |
| 448 | + operationName: "Blue", |
| 449 | + variables: { last: 3 }, |
| 450 | + }; |
| 451 | + const result = callLog.options.body === JSON.stringify(expected); |
| 452 | + if (!result) { |
| 453 | + console.warn("Body did not match expected value", { |
| 454 | + expected, |
| 455 | + actual: JSON.parse(callLog.options.body as string), |
| 456 | + }); |
| 457 | + } |
| 458 | + return result; |
| 459 | + }, |
| 460 | + }, |
| 461 | + ); |
| 462 | + |
| 463 | + return new Promise<void>((res, rej) => |
| 464 | + graphql(query, { |
| 465 | + headers: { |
| 466 | + authorization: `token secret123`, |
| 467 | + }, |
| 468 | + request: { |
| 469 | + fetch: fetch.fetchHandler, |
| 470 | + }, |
| 471 | + operationName: "Blue", |
| 472 | + last: 3, |
| 473 | + }) |
| 474 | + .then((result) => { |
| 475 | + expect(JSON.stringify(result)).toStrictEqual( |
| 476 | + JSON.stringify(mockData), |
| 477 | + ); |
| 478 | + res(); |
| 479 | + }) |
| 480 | + .catch((error) => { |
| 481 | + rej(error); |
| 482 | + }), |
| 483 | + ); |
| 484 | + }); |
| 485 | + }); |
326 | 486 | }); |
0 commit comments