|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @emails oncall+relay |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const RelayFindGraphQLTags = require('RelayFindGraphQLTags'); |
| 14 | +const FindGraphQLTags = require('FindGraphQLTags'); |
| 15 | + |
| 16 | +import type {GraphQLTagFinderOptions} from 'RelayFindGraphQLTags'; |
| 17 | + |
| 18 | +describe('RelayFindGraphQLTags', () => { |
| 19 | + function find(text, options: GraphQLTagFinderOptions, absPath: string = '/path/to/FindGraphQLTags.js') { |
| 20 | + return RelayFindGraphQLTags.find( |
| 21 | + FindGraphQLTags.find, |
| 22 | + text, |
| 23 | + absPath, |
| 24 | + options, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + describe('query validation', () => { |
| 29 | + it('prints correct file numbers in errors', () => { |
| 30 | + expect(() => { |
| 31 | + find( |
| 32 | + 'const foo = 1;\n' + |
| 33 | + 'foo(graphql`\n' + |
| 34 | + ' fragment FindGraphQLTags on User {\n' + |
| 35 | + ' ?\n' + |
| 36 | + ' id\n' + |
| 37 | + ' }\n' + |
| 38 | + '`);\n', |
| 39 | + {validateNames: true} |
| 40 | + ); |
| 41 | + }).toThrow('Syntax Error: Cannot parse the unexpected character "?".'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('query name validation', () => { |
| 46 | + it('throws for invalid query names', () => { |
| 47 | + expect(() => |
| 48 | + find( |
| 49 | + 'graphql`query NotModuleName { me { id } }`;', |
| 50 | + {validateNames: true}, |
| 51 | + ), |
| 52 | + ).toThrow( |
| 53 | + 'FindGraphQLTags: Operation names in graphql tags must be prefixed with ' + |
| 54 | + 'the module name and end in "Mutation", "Query", or "Subscription". ' + |
| 55 | + 'Got `NotModuleName` in module `FindGraphQLTags`.', |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not validate names when options is not set', () => { |
| 60 | + find( |
| 61 | + 'graphql`query NotModuleName { me { id } }`;', |
| 62 | + {validateNames: false}, |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('parses queries with valid names', () => { |
| 67 | + expect( |
| 68 | + find( |
| 69 | + 'graphql`query FindGraphQLTagsQuery { me { id } }`;', |
| 70 | + {validateNames: true}, |
| 71 | + ), |
| 72 | + ).toEqual(['query FindGraphQLTagsQuery { me { id } }']); |
| 73 | + }); |
| 74 | + |
| 75 | + it('parses queries with valid names from filepath', () => { |
| 76 | + expect( |
| 77 | + find( |
| 78 | + 'graphql`query TestComponentQuery { me { id } }`;', |
| 79 | + {validateNames: true}, |
| 80 | + './PathTo/SuperDuper/TestComponent.js', |
| 81 | + ), |
| 82 | + ).toEqual(['query TestComponentQuery { me { id } }']); |
| 83 | + expect( |
| 84 | + find( |
| 85 | + 'graphql`query TestComponentQuery { me { id } }`;', |
| 86 | + {validateNames: true}, |
| 87 | + './PathTo/SuperDuper/TestComponent.react.js', |
| 88 | + ), |
| 89 | + ).toEqual(['query TestComponentQuery { me { id } }']); |
| 90 | + expect( |
| 91 | + find( |
| 92 | + 'graphql`query TestComponentQuery { me { id } }`;', |
| 93 | + {validateNames: true}, |
| 94 | + './PathTo/SuperDuper/TestComponent.react.jsx', |
| 95 | + ), |
| 96 | + ).toEqual(['query TestComponentQuery { me { id } }']); |
| 97 | + expect( |
| 98 | + find( |
| 99 | + 'graphql`query TestComponentQuery { me { id } }`;', |
| 100 | + {validateNames: true}, |
| 101 | + './PathTo/SuperDuper/TestComponent/index.js', |
| 102 | + ), |
| 103 | + ).toEqual(['query TestComponentQuery { me { id } }']); |
| 104 | + }); |
| 105 | + |
| 106 | + it('throws for invalid top-level fragment names', () => { |
| 107 | + expect(() => |
| 108 | + find( |
| 109 | + 'graphql`fragment NotModuleName on User { name }`;', |
| 110 | + {validateNames: true}, |
| 111 | + ), |
| 112 | + ).toThrow( |
| 113 | + 'FindGraphQLTags: Fragment names in graphql tags ' + |
| 114 | + 'must be prefixed with the module name. Got ' + |
| 115 | + '`NotModuleName` in module `FindGraphQLTags`.', |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('parses top-level fragments with valid names', () => { |
| 120 | + expect( |
| 121 | + find( |
| 122 | + 'graphql`fragment FindGraphQLTags on User { name }`;', |
| 123 | + {validateNames: true}, |
| 124 | + ), |
| 125 | + ).toEqual(['fragment FindGraphQLTags on User { name }']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('throws for invalid container fragment names', () => { |
| 129 | + expect(() => |
| 130 | + find(` |
| 131 | + createFragmentContainer(Foo, { |
| 132 | + foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`, |
| 133 | + }); |
| 134 | + `, {validateNames: true}), |
| 135 | + ).toThrow( |
| 136 | + 'FindGraphQLTags: Container fragment names must be ' + |
| 137 | + '`<ModuleName>_<propName>`. Got `FindGraphQLTags_notFoo`, expected ' + |
| 138 | + '`FindGraphQLTags_foo`.', |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('parses container fragments with valid names', () => { |
| 143 | + expect( |
| 144 | + find(` |
| 145 | + createFragmentContainer(Foo, { |
| 146 | + foo: graphql\`fragment FindGraphQLTags_foo on User { name }\`, |
| 147 | + }); |
| 148 | + `, {validateNames: true}), |
| 149 | + ).toEqual(['fragment FindGraphQLTags_foo on User { name }']); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments