Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

visitor-test: cleanup test for nodes with unknown kinds #3034

Merged
merged 1 commit into from Apr 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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