Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 5.1 kB view raw
1import { expect, it, describe } from 'vitest'; 2 3import { parse, print } from '@0no-co/graphql.web'; 4import { gql } from '../gql'; 5import { createRequest, stringifyDocument } from './request'; 6import { formatDocument } from './formatDocument'; 7 8describe('createRequest', () => { 9 it('should hash identical queries identically', () => { 10 const reqA = createRequest('{ test }', undefined); 11 const reqB = createRequest('{ test }', undefined); 12 expect(reqA.key).toBe(reqB.key); 13 }); 14 15 it('should hash identical queries identically', () => { 16 const reqA = createRequest('{ test }', undefined); 17 const reqB = createRequest('{ test }', undefined); 18 expect(reqA.key).toBe(reqB.key); 19 }); 20 21 it('should hash identical DocumentNodes identically', () => { 22 const reqA = createRequest(parse('{ testB }'), undefined); 23 const reqB = createRequest(parse('{ testB }'), undefined); 24 expect(reqA.key).toBe(reqB.key); 25 expect(reqA.query).toBe(reqB.query); 26 }); 27 28 it('should use the hash from a key if available', () => { 29 const doc = parse('{ testC }'); 30 (doc as any).__key = 1234; 31 const req = createRequest(doc, undefined); 32 expect(req.key).toBe(1234); 33 }); 34 35 it('should hash DocumentNodes and strings identically', () => { 36 const docA = parse('{ field }'); 37 const docB = print(docA); 38 const reqA = createRequest(docA, undefined); 39 const reqB = createRequest(docB, undefined); 40 expect(reqA.key).toBe(reqB.key); 41 expect(reqA.query).toBe(reqB.query); 42 }); 43 44 it('should hash graphql-tag documents correctly', () => { 45 const doc = gql` 46 { 47 testD 48 } 49 `; 50 createRequest(doc, undefined); 51 expect((doc as any).__key).not.toBe(undefined); 52 }); 53 54 it('should return a valid query object', () => { 55 const doc = gql` 56 { 57 testE 58 } 59 `; 60 const val = createRequest(doc, undefined); 61 62 expect(val).toMatchObject({ 63 key: expect.any(Number), 64 query: expect.any(Object), 65 variables: {}, 66 }); 67 }); 68 69 it('should return a valid query object with variables', () => { 70 const doc = print(gql` 71 { 72 testF 73 } 74 `); 75 const val = createRequest(doc, { test: 5 }); 76 77 expect(print(val.query)).toBe(doc); 78 expect(val).toMatchObject({ 79 key: expect.any(Number), 80 query: expect.any(Object), 81 variables: { test: 5 }, 82 }); 83 }); 84 85 it('should hash persisted documents consistently', () => { 86 const doc = parse('{ testG }'); 87 const docPersisted = parse('{ testG }'); 88 (docPersisted as any).documentId = 'testG'; 89 90 const req = createRequest(doc, undefined); 91 const reqPersisted = createRequest(docPersisted, undefined); 92 expect(req.key).not.toBe(reqPersisted.key); 93 }); 94}); 95 96describe('stringifyDocument ', () => { 97 it('should reprint formatted documents', () => { 98 const doc = parse('{ test { field } }'); 99 const formatted = formatDocument(doc); 100 expect(stringifyDocument(formatted)).toBe(print(formatted)); 101 }); 102 103 it('should reprint request documents', () => { 104 const request = createRequest(`query { test { field } }`, {}); 105 const formatted = formatDocument(request.query); 106 expect(print(formatted)).toMatchInlineSnapshot(` 107 "{ 108 test { 109 field 110 __typename 111 } 112 }" 113 `); 114 expect(stringifyDocument(formatted)).toBe(print(formatted)); 115 }); 116 117 it('should reprint gql documents', () => { 118 const request = createRequest( 119 gql` 120 query { 121 test { 122 field 123 } 124 } 125 `, 126 {} 127 ); 128 const formatted = formatDocument(request.query); 129 expect(print(formatted)).toMatchInlineSnapshot(` 130 "{ 131 test { 132 field 133 __typename 134 } 135 }" 136 `); 137 expect(stringifyDocument(formatted)).toBe(print(formatted)); 138 }); 139 140 it('should remove comments', () => { 141 const doc = ` 142 { #query 143 # broken 144 test 145 } 146 `; 147 expect(stringifyDocument(createRequest(doc, undefined).query)) 148 .toMatchInlineSnapshot(` 149 "{ 150 test 151 }" 152 `); 153 }); 154 155 it('should remove duplicate spaces', () => { 156 const doc = ` 157 { 158 abc ,, test 159 } 160 `; 161 expect(stringifyDocument(createRequest(doc, undefined).query)) 162 .toMatchInlineSnapshot(` 163 "{ 164 abc 165 test 166 }" 167 `); 168 }); 169 170 it('should not sanitize within strings', () => { 171 const doc = ` 172 { 173 field(arg: "test #1") 174 } 175 `; 176 expect(stringifyDocument(createRequest(doc, undefined).query)) 177 .toMatchInlineSnapshot(` 178 "{ 179 field(arg: 180 "test #1") 181 }" 182 `); 183 }); 184 185 it('should not sanitize within block strings', () => { 186 const doc = ` 187 { 188 field( 189 arg: """ 190 hello 191 #hello 192 """ 193 ) 194 } 195 `; 196 expect(stringifyDocument(createRequest(doc, undefined).query)) 197 .toMatchInlineSnapshot(` 198 "{ 199 field(arg: 200 """ 201 hello 202 #hello 203 """) 204 }" 205 `); 206 }); 207});