|
1 | 1 | import { describe, it } from 'mocha';
|
2 | 2 |
|
| 3 | +import { expectJSON } from '../../__testUtils__/expectJSON'; |
| 4 | + |
| 5 | +import type { DocumentNode } from '../../language/ast'; |
| 6 | +import { OperationTypeNode } from '../../language/ast'; |
| 7 | +import { Kind } from '../../language/kinds'; |
| 8 | + |
3 | 9 | import { ScalarLeafsRule } from '../rules/ScalarLeafsRule';
|
| 10 | +import { validate } from '../validate'; |
4 | 11 |
|
5 |
| -import { expectValidationErrors } from './harness'; |
| 12 | +import { expectValidationErrors, testSchema } from './harness'; |
6 | 13 |
|
7 | 14 | function expectErrors(queryStr: string) {
|
8 | 15 | return expectValidationErrors(ScalarLeafsRule, queryStr);
|
@@ -126,4 +133,37 @@ describe('Validate: Scalar leafs', () => {
|
126 | 133 | },
|
127 | 134 | ]);
|
128 | 135 | });
|
| 136 | + |
| 137 | + it('object type having only one selection', () => { |
| 138 | + const doc: DocumentNode = { |
| 139 | + kind: Kind.DOCUMENT, |
| 140 | + definitions: [ |
| 141 | + { |
| 142 | + kind: Kind.OPERATION_DEFINITION, |
| 143 | + operation: OperationTypeNode.QUERY, |
| 144 | + selectionSet: { |
| 145 | + kind: Kind.SELECTION_SET, |
| 146 | + selections: [ |
| 147 | + { |
| 148 | + kind: Kind.FIELD, |
| 149 | + name: { kind: Kind.NAME, value: 'human' }, |
| 150 | + selectionSet: { kind: Kind.SELECTION_SET, selections: [] }, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }, |
| 154 | + }, |
| 155 | + ], |
| 156 | + }; |
| 157 | + |
| 158 | + // We can't leverage expectErrors since it doesn't support passing in the |
| 159 | + // documentNode directly. We have to do this because this is technically |
| 160 | + // an invalid document. |
| 161 | + const errors = validate(testSchema, doc, [ScalarLeafsRule]); |
| 162 | + expectJSON(errors).toDeepEqual([ |
| 163 | + { |
| 164 | + message: |
| 165 | + 'Field "human" of type "Human" must have at least one field selected.', |
| 166 | + }, |
| 167 | + ]); |
| 168 | + }); |
129 | 169 | });
|
0 commit comments