From 253fae13d8bfac0876fec2cb50d9e140ceb89bce Mon Sep 17 00:00:00 2001 From: Anna L Date: Tue, 7 May 2024 11:35:05 +0200 Subject: [PATCH] fix --- src/TreeOperations/tree.ts | 16 ++++++++------ src/__tests__/TreeOperations/mocks.ts | 22 ++++++++++++++++--- .../TreeOperations/tree.rename.spec.ts | 17 ++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/TreeOperations/tree.ts b/src/TreeOperations/tree.ts index 2eb3c8e..ca99819 100644 --- a/src/TreeOperations/tree.ts +++ b/src/TreeOperations/tree.ts @@ -33,12 +33,14 @@ import { } from '@/TreeOperations/shared'; export const mutate = (tree: ParserTree, allNodes: ParserField[]) => { - const mutateParentIfField = (node: ParserField) => { + const mutateParentIfField = (node: ParserField, parentNode?: string) => { if (node.data.type === TypeSystemDefinition.FieldDefinition) { - const parentNode = allNodes.find((an) => an.args.some((a) => a.id === node.id)); - if (!parentNode) throw new Error('Invalid field definition'); - const fieldIndex = parentNode.args.findIndex((a) => a.id == node.id); - updateFieldOnNode(parentNode, fieldIndex, node); + const findParentNode = parentNode + ? allNodes.find((n) => n.id === parentNode) + : allNodes.find((an) => an.args.some((a) => a.id === node.id)); + if (!findParentNode) throw new Error('Invalid field definition'); + const fieldIndex = findParentNode.args.findIndex((a) => a.id == node.id); + updateFieldOnNode(findParentNode, fieldIndex, node); return; } }; @@ -65,7 +67,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => { mutateParentIfField(n); }; - const updateFieldOnNode = (node: ParserField, i: number, updatedField: ParserField) => { + const updateFieldOnNode = (node: ParserField, i: number, updatedField: ParserField, parentNode?: string) => { regenerateId(updatedField); if (node.data.type === TypeSystemDefinition.DirectiveDefinition) { const oldField: ParserField = JSON.parse(JSON.stringify(node.args[i])); @@ -77,7 +79,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => { } node.args[i] = updatedField; regenerateId(node); - mutateParentIfField(node); + mutateParentIfField(node, parentNode); }; const addFieldToNode = (node: ParserField, f: ParserField) => { diff --git a/src/__tests__/TreeOperations/mocks.ts b/src/__tests__/TreeOperations/mocks.ts index a4567e6..d08a0b4 100644 --- a/src/__tests__/TreeOperations/mocks.ts +++ b/src/__tests__/TreeOperations/mocks.ts @@ -44,6 +44,16 @@ const mainMock: ParserTree = { name: 'friends', type: ScalarTypes.String, }), + createPlainField({ + name: 'testUpdate', + type: ScalarTypes.Boolean, + args: [ + createPlainInputValue({ + name: 'firstName', + type: ScalarTypes.String, + }), + ], + }), ], }), createRootField({ @@ -105,9 +115,15 @@ const mainMock: ParserTree = { name: 'AnInput', type: TypeDefinition.InputObjectTypeDefinition, args: [ - createPlainInputValue({ - name: 'firstName', - type: ScalarTypes.String, + createPlainField({ + name: 'testUpdate', + type: ScalarTypes.Boolean, + args: [ + createPlainInputValue({ + name: 'firstName', + type: ScalarTypes.String, + }), + ], }), ], }), diff --git a/src/__tests__/TreeOperations/tree.rename.spec.ts b/src/__tests__/TreeOperations/tree.rename.spec.ts index 242357a..291629f 100644 --- a/src/__tests__/TreeOperations/tree.rename.spec.ts +++ b/src/__tests__/TreeOperations/tree.rename.spec.ts @@ -55,6 +55,23 @@ describe('Tree Operations tests - rename operations', () => { fromInterface: [treeMock.nodes[0].name], }); }); + test('Update field on input definition inside node args', () => { + const treeMock = createMock(); + const oldField = treeMock.nodes[2].args[0].args[0]; + const updatedField = { + ...oldField, + name: 'secondName', + }; + + mutate(treeMock, treeMock.nodes).updateFieldOnNode( + treeMock.nodes[2].args[0], + 0, + updatedField, + treeMock.nodes[2].id, + ); + expect(treeMock.nodes[2].args[0].args).toContainEqual(updatedField); + expect(treeMock.nodes[0].args[3].args).not.toContainEqual(updatedField); + }); test('Update field name on directive definition', () => { const treeMock = createMock(); const updatedField = createParserField({