Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 2.4 kB view raw
1import { formatDocument, gql } from '@urql/core'; 2import { describe, it, expect } from 'vitest'; 3 4import { getSelectionSet } from './node'; 5import { getMainOperation, shouldInclude } from './traversal'; 6 7describe('getMainOperation', () => { 8 it('retrieves the first operation', () => { 9 const doc = formatDocument(gql` 10 query Query { 11 field 12 } 13 `); 14 15 const operation = getMainOperation(doc); 16 expect(operation).toBe(doc.definitions[0]); 17 }); 18 19 it('throws when no operation is found', () => { 20 const doc = formatDocument(gql` 21 fragment _ on Query { 22 field 23 } 24 `); 25 26 expect(() => getMainOperation(doc)).toThrow(); 27 }); 28}); 29 30describe('shouldInclude', () => { 31 it('should include fields with truthy @include or falsy @skip directives', () => { 32 const doc = formatDocument(gql` 33 { 34 fieldA @include(if: true) 35 fieldB @skip(if: false) 36 } 37 `); 38 39 const fieldA = getSelectionSet(getMainOperation(doc))[0]; 40 const fieldB = getSelectionSet(getMainOperation(doc))[1]; 41 expect(shouldInclude(fieldA, {})).toBe(true); 42 expect(shouldInclude(fieldB, {})).toBe(true); 43 }); 44 45 it('should exclude fields with falsy @include or truthy @skip directives', () => { 46 const doc = formatDocument(gql` 47 { 48 fieldA @include(if: false) 49 fieldB @skip(if: true) 50 } 51 `); 52 53 const fieldA = getSelectionSet(getMainOperation(doc))[0]; 54 const fieldB = getSelectionSet(getMainOperation(doc))[1]; 55 expect(shouldInclude(fieldA, {})).toBe(false); 56 expect(shouldInclude(fieldB, {})).toBe(false); 57 }); 58 59 it('ignore other directives', () => { 60 const doc = formatDocument(gql` 61 { 62 field @test(if: false) 63 } 64 `); 65 66 const field = getSelectionSet(getMainOperation(doc))[0]; 67 expect(shouldInclude(field, {})).toBe(true); 68 }); 69 70 it('ignore unknown arguments on directives', () => { 71 const doc = formatDocument(gql` 72 { 73 field @skip(if: true, other: false) 74 } 75 `); 76 77 const field = getSelectionSet(getMainOperation(doc))[0]; 78 expect(shouldInclude(field, {})).toBe(false); 79 }); 80 81 it('ignore directives with invalid first arguments', () => { 82 const doc = formatDocument(gql` 83 { 84 field @skip(other: true) 85 } 86 `); 87 88 const field = getSelectionSet(getMainOperation(doc))[0]; 89 expect(shouldInclude(field, {})).toBe(true); 90 }); 91});