Skip to content

Commit e7d6890

Browse files
committed
reduce diff
1 parent c573130 commit e7d6890

File tree

1 file changed

+43
-69
lines changed

1 file changed

+43
-69
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 43 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent';
@@ -11,6 +11,7 @@ import { Kind } from '../kinds';
1111
import { parse, parseConstValue, parseType, parseValue } from '../parser';
1212
import { Source } from '../source';
1313
import { TokenKind } from '../tokenKind';
14+
import { OperationDefinitionNode } from '../ast';
1415

1516
function expectSyntaxError(text: string) {
1617
return expectToThrowJSON(() => parse(text));
@@ -163,25 +164,11 @@ describe('Parser', () => {
163164
# This comment has a \u0A0A multi-byte character.
164165
{ field(arg: "Has a \u0A0A multi-byte character.") }
165166
`);
166-
const opDef = ast.definitions.find(
167-
(d) => d.kind === Kind.OPERATION_DEFINITION,
167+
168+
expect(ast).to.have.nested.property(
169+
'definitions[0].selectionSet.selections[0].arguments[0].value.value',
170+
'Has a \u0A0A multi-byte character.',
168171
);
169-
if (!opDef || opDef.kind !== Kind.OPERATION_DEFINITION) {
170-
throw new Error('No operation definition found');
171-
}
172-
const fieldSel = opDef.selectionSet.selections[0];
173-
if (fieldSel.kind !== Kind.FIELD) {
174-
throw new Error('Expected a field selection');
175-
}
176-
const args = fieldSel.arguments;
177-
if (!args || args.length === 0) {
178-
throw new Error('No arguments found');
179-
}
180-
const argValueNode = args[0].value;
181-
if (argValueNode.kind !== Kind.STRING) {
182-
throw new Error('Expected a string value');
183-
}
184-
expect(argValueNode.value).to.equal('Has a \u0A0A multi-byte character.');
185172
});
186173

187174
it('parses kitchen sink', () => {
@@ -350,7 +337,6 @@ describe('Parser', () => {
350337

351338
it('creates ast from nameless query without variables', () => {
352339
const result = parse(dedent`
353-
"Query description"
354340
query {
355341
node {
356342
id
@@ -360,47 +346,42 @@ describe('Parser', () => {
360346

361347
expectJSON(result).toDeepEqual({
362348
kind: Kind.DOCUMENT,
363-
loc: { start: 0, end: 49 },
349+
loc: { start: 0, end: 29 },
364350
definitions: [
365351
{
366352
kind: Kind.OPERATION_DEFINITION,
367-
loc: { start: 0, end: 49 },
368-
description: {
369-
kind: Kind.STRING,
370-
loc: { start: 0, end: 19 },
371-
block: false,
372-
value: 'Query description',
373-
},
353+
loc: { start: 0, end: 29 },
354+
description: undefined,
374355
operation: 'query',
375356
name: undefined,
376357
variableDefinitions: [],
377358
directives: [],
378359
selectionSet: {
379360
kind: Kind.SELECTION_SET,
380-
loc: { start: 26, end: 49 },
361+
loc: { start: 6, end: 29 },
381362
selections: [
382363
{
383364
kind: Kind.FIELD,
384-
loc: { start: 30, end: 47 },
365+
loc: { start: 10, end: 27 },
385366
alias: undefined,
386367
name: {
387368
kind: Kind.NAME,
388-
loc: { start: 30, end: 34 },
369+
loc: { start: 10, end: 14 },
389370
value: 'node',
390371
},
391372
arguments: [],
392373
directives: [],
393374
selectionSet: {
394375
kind: Kind.SELECTION_SET,
395-
loc: { start: 35, end: 47 },
376+
loc: { start: 15, end: 27 },
396377
selections: [
397378
{
398379
kind: Kind.FIELD,
399-
loc: { start: 41, end: 43 },
380+
loc: { start: 21, end: 23 },
400381
alias: undefined,
401382
name: {
402383
kind: Kind.NAME,
403-
loc: { start: 41, end: 43 },
384+
loc: { start: 21, end: 23 },
404385
value: 'id',
405386
},
406387
arguments: [],
@@ -693,13 +674,10 @@ describe('Parser', () => {
693674
field(a: $a, b: $b)
694675
}
695676
`);
696-
// Find the operation definition
697-
const opDef = result.definitions.find(
698-
(d) => d.kind === Kind.OPERATION_DEFINITION,
699-
);
700-
if (!opDef || opDef.kind !== Kind.OPERATION_DEFINITION) {
701-
throw new Error('No operation definition found');
702-
}
677+
678+
const opDef = result.definitions[0];
679+
assert(opDef.kind === Kind.OPERATION_DEFINITION);
680+
703681
expect(opDef.description?.value).to.equal('Operation description');
704682
expect(opDef.name?.value).to.equal('myQuery');
705683
expect(opDef.variableDefinitions?.[0].description?.value).to.equal(
@@ -712,15 +690,16 @@ describe('Parser', () => {
712690
expect(opDef.variableDefinitions?.[1].description?.block).to.equal(true);
713691
expect(opDef.variableDefinitions?.[0].variable.name.value).to.equal('a');
714692
expect(opDef.variableDefinitions?.[1].variable.name.value).to.equal('b');
715-
// Check type names safely
693+
716694
const typeA = opDef.variableDefinitions?.[0].type;
717-
if (typeA && typeA.kind === Kind.NAMED_TYPE) {
718-
expect(typeA.name.value).to.equal('Int');
719-
}
695+
assert(typeA?.kind === Kind.NAMED_TYPE);
696+
697+
expect(typeA.name.value).to.equal('Int');
698+
720699
const typeB = opDef.variableDefinitions?.[1].type;
721-
if (typeB && typeB.kind === Kind.NAMED_TYPE) {
722-
expect(typeB.name.value).to.equal('String');
723-
}
700+
assert(typeB?.kind === Kind.NAMED_TYPE);
701+
702+
expect(typeB.name.value).to.equal('String');
724703
});
725704

726705
it('parses variable definition with description, default value, and directives', () => {
@@ -732,40 +711,35 @@ describe('Parser', () => {
732711
field(foo: $foo)
733712
}
734713
`);
735-
const opDef = result.definitions.find(
736-
(d) => d.kind === Kind.OPERATION_DEFINITION,
737-
);
738-
if (!opDef || opDef.kind !== Kind.OPERATION_DEFINITION) {
739-
throw new Error('No operation definition found');
740-
}
714+
715+
const opDef = result.definitions[0];
716+
assert(opDef.kind === Kind.OPERATION_DEFINITION);
741717
const varDef = opDef.variableDefinitions?.[0];
742718
expect(varDef?.description?.value).to.equal('desc');
743719
expect(varDef?.variable.name.value).to.equal('foo');
744-
if (varDef?.type.kind === Kind.NAMED_TYPE) {
745-
expect(varDef.type.name.value).to.equal('Int');
746-
}
747-
if (varDef?.defaultValue && 'value' in varDef.defaultValue) {
748-
expect(varDef.defaultValue.value).to.equal('42');
749-
}
720+
721+
assert(varDef?.type.kind === Kind.NAMED_TYPE);
722+
expect(varDef.type.name.value).to.equal('Int');
723+
724+
assert(varDef?.defaultValue?.kind === Kind.INT);
725+
expect(varDef.defaultValue.value).to.equal('42');
726+
750727
expect(varDef?.directives?.[0].name.value).to.equal('dir');
751728
});
752729

753730
it('parses fragment with variable description (legacy)', () => {
754731
const result = parse('fragment Foo("desc" $foo: Int) on Bar { baz }', {
755732
allowLegacyFragmentVariables: true,
756733
});
757-
const fragDef = result.definitions.find(
758-
(d) => d.kind === Kind.FRAGMENT_DEFINITION,
759-
);
760-
if (!fragDef || fragDef.kind !== Kind.FRAGMENT_DEFINITION) {
761-
throw new Error('No fragment definition found');
762-
}
734+
const fragDef = result.definitions[0]
735+
assert(fragDef.kind === Kind.FRAGMENT_DEFINITION);
736+
763737
const varDef = fragDef.variableDefinitions?.[0];
764738
expect(varDef?.description?.value).to.equal('desc');
765739
expect(varDef?.variable.name.value).to.equal('foo');
766-
if (varDef?.type.kind === Kind.NAMED_TYPE) {
767-
expect(varDef.type.name.value).to.equal('Int');
768-
}
740+
741+
assert(varDef?.type.kind === Kind.NAMED_TYPE);
742+
expect(varDef.type.name.value).to.equal('Int');
769743
});
770744

771745
it('produces sensible error for description on shorthand query', () => {

0 commit comments

Comments
 (0)