|
| 1 | +/** |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it } from 'mocha'; |
| 11 | +import { expectPassesRule, expectFailsRule } from './harness'; |
| 12 | +import ProvidedNonNullArguments from '../rules/ProvidedNonNullArguments'; |
| 13 | +import { missingFieldArgMessage, missingDirectiveArgMessage } from '../errors'; |
| 14 | + |
| 15 | + |
| 16 | +function missingFieldArg(fieldName, argName, typeName, line, column) { |
| 17 | + return { |
| 18 | + message: missingFieldArgMessage(fieldName, argName, typeName), |
| 19 | + locations: [ { line: line, column: column } ], |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function missingDirectiveArg(directiveName, argName, typeName, line, column) { |
| 24 | + return { |
| 25 | + message: missingDirectiveArgMessage(directiveName, argName, typeName), |
| 26 | + locations: [ { line: line, column: column } ], |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe('Validate: Provided required arguments', () => { |
| 31 | + |
| 32 | + describe('Valid non-nullable value', () => { |
| 33 | + |
| 34 | + it('Arg on optional arg', () => { |
| 35 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 36 | + { |
| 37 | + dog { |
| 38 | + isHousetrained(atOtherHomes: true) |
| 39 | + } |
| 40 | + } |
| 41 | + `); |
| 42 | + }); |
| 43 | + |
| 44 | + it('No Arg on optional arg', () => { |
| 45 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 46 | + { |
| 47 | + dog { |
| 48 | + isHousetrained |
| 49 | + } |
| 50 | + } |
| 51 | + `); |
| 52 | + }); |
| 53 | + |
| 54 | + it('Multiple args', () => { |
| 55 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 56 | + { |
| 57 | + complicatedArgs { |
| 58 | + multipleReqs(req1: 1, req2: 2) |
| 59 | + } |
| 60 | + } |
| 61 | + `); |
| 62 | + }); |
| 63 | + |
| 64 | + it('Multiple args reverse order', () => { |
| 65 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 66 | + { |
| 67 | + complicatedArgs { |
| 68 | + multipleReqs(req2: 2, req1: 1) |
| 69 | + } |
| 70 | + } |
| 71 | + `); |
| 72 | + }); |
| 73 | + |
| 74 | + it('No args on multiple optional', () => { |
| 75 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 76 | + { |
| 77 | + complicatedArgs { |
| 78 | + multipleOpts |
| 79 | + } |
| 80 | + } |
| 81 | + `); |
| 82 | + }); |
| 83 | + |
| 84 | + it('One arg on multiple optional', () => { |
| 85 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 86 | + { |
| 87 | + complicatedArgs { |
| 88 | + multipleOpts(opt1: 1) |
| 89 | + } |
| 90 | + } |
| 91 | + `); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Second arg on multiple optional', () => { |
| 95 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 96 | + { |
| 97 | + complicatedArgs { |
| 98 | + multipleOpts(opt2: 1) |
| 99 | + } |
| 100 | + } |
| 101 | + `); |
| 102 | + }); |
| 103 | + |
| 104 | + it('Multiple reqs on mixedList', () => { |
| 105 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 106 | + { |
| 107 | + complicatedArgs { |
| 108 | + multipleOptAndReq(req1: 3, req2: 4) |
| 109 | + } |
| 110 | + } |
| 111 | + `); |
| 112 | + }); |
| 113 | + |
| 114 | + it('Multiple reqs and one opt on mixedList', () => { |
| 115 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 116 | + { |
| 117 | + complicatedArgs { |
| 118 | + multipleOptAndReq(req1: 3, req2: 4, opt1: 5) |
| 119 | + } |
| 120 | + } |
| 121 | + `); |
| 122 | + }); |
| 123 | + |
| 124 | + it('All reqs and opts on mixedList', () => { |
| 125 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 126 | + { |
| 127 | + complicatedArgs { |
| 128 | + multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) |
| 129 | + } |
| 130 | + } |
| 131 | + `); |
| 132 | + }); |
| 133 | + |
| 134 | + }); |
| 135 | + |
| 136 | + |
| 137 | + describe('Invalid non-nullable value', () => { |
| 138 | + |
| 139 | + it('Missing one non-nullable argument', () => { |
| 140 | + expectFailsRule(ProvidedNonNullArguments, ` |
| 141 | + { |
| 142 | + complicatedArgs { |
| 143 | + multipleReqs(req2: 2) |
| 144 | + } |
| 145 | + } |
| 146 | + `, [ |
| 147 | + missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13) |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('Missing multiple non-nullable arguments', () => { |
| 152 | + expectFailsRule(ProvidedNonNullArguments, ` |
| 153 | + { |
| 154 | + complicatedArgs { |
| 155 | + multipleReqs |
| 156 | + } |
| 157 | + } |
| 158 | + `, [ |
| 159 | + missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13), |
| 160 | + missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('Incorrect value and missing argument', () => { |
| 165 | + expectFailsRule(ProvidedNonNullArguments, ` |
| 166 | + { |
| 167 | + complicatedArgs { |
| 168 | + multipleReqs(req1: "one") |
| 169 | + } |
| 170 | + } |
| 171 | + `, [ |
| 172 | + missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), |
| 173 | + ]); |
| 174 | + }); |
| 175 | + |
| 176 | + }); |
| 177 | + |
| 178 | + describe('Directive arguments', () => { |
| 179 | + |
| 180 | + it('with directives of valid types', () => { |
| 181 | + expectPassesRule(ProvidedNonNullArguments, ` |
| 182 | + { |
| 183 | + dog @include(if: true) { |
| 184 | + name |
| 185 | + } |
| 186 | + human @skip(if: false) { |
| 187 | + name |
| 188 | + } |
| 189 | + } |
| 190 | + `); |
| 191 | + }); |
| 192 | + |
| 193 | + it('with directive with missing types', () => { |
| 194 | + expectFailsRule(ProvidedNonNullArguments, ` |
| 195 | + { |
| 196 | + dog @include { |
| 197 | + name @skip |
| 198 | + } |
| 199 | + } |
| 200 | + `, [ |
| 201 | + missingDirectiveArg('include', 'if', 'Boolean!', 3, 15), |
| 202 | + missingDirectiveArg('skip', 'if', 'Boolean!', 4, 18) |
| 203 | + ]); |
| 204 | + }); |
| 205 | + |
| 206 | + }); |
| 207 | + |
| 208 | +}); |
0 commit comments