aboutsummaryrefslogtreecommitdiff
path: root/tests/operations.test.ts
blob: 14fe768ed017b4e8bbdd9f0d04e2182a99b0e1da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*** IMPORT ------------------------------------------- ***/

import { expect, test } from "vitest";

/*** UTILITY ------------------------------------------ ***/

import { deriveTitle, parseOperations } from "../source/library/graphql/operations.ts";

/*** TESTS -------------------------------------------- ***/

test("parseOperations returns empty for blank query", () => {
  expect(parseOperations("")).toEqual([]);
  expect(parseOperations("   ")).toEqual([]);
});

test("parseOperations returns empty on syntax error", () => {
  expect(parseOperations("query { ...")).toEqual([]);
});

test("parseOperations captures a single named query", () => {
  const ops = parseOperations("query Foo { viewer { id } }");
  expect(ops).toEqual([{ name: "Foo", type: "query" }]);
});

test("parseOperations returns null name for anonymous ops", () => {
  const ops = parseOperations("{ viewer { id } }");
  expect(ops).toEqual([{ name: null, type: "query" }]);
});

test("parseOperations captures multiple operations", () => {
  const ops = parseOperations(`
    query Foo { a }
    mutation Bar { b }
    subscription Baz { c }
  `);
  expect(ops).toEqual([
    { name: "Foo", type: "query" },
    { name: "Bar", type: "mutation" },
    { name: "Baz", type: "subscription" }
  ]);
});

test("deriveTitle prefers the first operation name", () => {
  const ops = parseOperations("query Foo { a }");
  expect(deriveTitle("query Foo { a }", ops)).toEqual("Foo");
});

test("deriveTitle falls back to operation type", () => {
  const ops = parseOperations("mutation { a }");
  expect(deriveTitle("mutation { a }", ops)).toEqual("mutation");
});

test("deriveTitle falls back to the first 20 chars when unparsable", () => {
  const query = "this is not valid graphql at all";
  expect(deriveTitle(query, parseOperations(query))).toEqual("this is not valid gr");
});

test("deriveTitle returns 'untitled' for empty input", () => {
  expect(deriveTitle("", [])).toEqual("untitled");
  expect(deriveTitle("   ", [])).toEqual("untitled");
});