Skip to content

Commit

Permalink
visitor-test: cleanup test for nodes with unknown kinds (#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 11, 2021
1 parent 0460fe9 commit 264f758
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions src/language/__tests__/visitor-test.js
Expand Up @@ -418,6 +418,49 @@ describe('Visitor', () => {
]);
});

it('visit nodes with unknown kinds but does not traverse deeper', () => {
const customAST = parse('{ a }');
// $FlowExpectedError[prop-missing]
customAST.definitions[0].selectionSet.selections.push({
kind: 'CustomField',
name: { kind: 'Name', value: 'NamedNodeToBeSkipped' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'CustomField',
name: { kind: 'Name', value: 'NamedNodeToBeSkipped' },
},
],
},
});

const visited = [];
visit(customAST, {
enter(node) {
visited.push(['enter', node.kind, getValue(node)]);
},
leave(node) {
visited.push(['leave', node.kind, getValue(node)]);
},
});

expect(visited).to.deep.equal([
['enter', 'Document', undefined],
['enter', 'OperationDefinition', undefined],
['enter', 'SelectionSet', undefined],
['enter', 'Field', undefined],
['enter', 'Name', 'a'],
['leave', 'Name', 'a'],
['leave', 'Field', undefined],
['enter', 'CustomField', undefined],
['leave', 'CustomField', undefined],
['leave', 'SelectionSet', undefined],
['leave', 'OperationDefinition', undefined],
['leave', 'Document', undefined],
]);
});

it('Legacy: visits variables defined in fragments', () => {
const ast = parse('fragment a($v: Boolean = false) on t { f }', {
noLocation: true,
Expand Down Expand Up @@ -845,56 +888,6 @@ describe('Visitor', () => {
]);
});

describe('Support for custom AST nodes', () => {
const customAST = parse('{ a }');
(customAST: any).definitions[0].selectionSet.selections.push({
kind: 'CustomField',
name: {
kind: 'Name',
value: 'b',
},
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'CustomField',
name: {
kind: 'Name',
value: 'c',
},
},
],
},
});

it('does not traverse unknown node kinds', () => {
const visited = [];
visit(customAST, {
enter(node) {
visited.push(['enter', node.kind, getValue(node)]);
},
leave(node) {
visited.push(['leave', node.kind, getValue(node)]);
},
});

expect(visited).to.deep.equal([
['enter', 'Document', undefined],
['enter', 'OperationDefinition', undefined],
['enter', 'SelectionSet', undefined],
['enter', 'Field', undefined],
['enter', 'Name', 'a'],
['leave', 'Name', 'a'],
['leave', 'Field', undefined],
['enter', 'CustomField', undefined],
['leave', 'CustomField', undefined],
['leave', 'SelectionSet', undefined],
['leave', 'OperationDefinition', undefined],
['leave', 'Document', undefined],
]);
});
});

describe('visitInParallel', () => {
// Note: nearly identical to the above test of the same test but
// using visitInParallel.
Expand Down

0 comments on commit 264f758

Please sign in to comment.