From 1eb4dca6faa215adfb20b06dda65ebba75003d46 Mon Sep 17 00:00:00 2001 From: Nicholas Vinson Date: Thu, 10 Oct 2024 13:09:40 -0400 Subject: [PATCH] fix: rootNode.getAllContext() returns empty object (#2483) Correct getAllContext() to return the root node's context in addition to specified defaults. Fixes: #2239 --- src/construct.ts | 10 ++-------- test/construct.test.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/construct.ts b/src/construct.ts index 1f946bfe..ba2988b8 100644 --- a/src/construct.ts +++ b/src/construct.ts @@ -245,14 +245,8 @@ export class Node { * @returns The context object or an empty object if there is discovered context */ public getAllContext(defaults?: object): any { - if (typeof defaults === 'undefined') { - defaults = {}; - } - - if (this.scope === undefined) { return defaults; } - - const value = { ...this._context, ...defaults }; - return this.scope && this.scope.node.getAllContext(value); + return this.scopes.reverse() + .reduce((a, s) => ({ ...(s.node._context), ...a }), { ...defaults }); } /** diff --git a/test/construct.test.ts b/test/construct.test.ts index ebc92941..5da39baa 100644 --- a/test/construct.test.ts +++ b/test/construct.test.ts @@ -150,6 +150,23 @@ test('construct.getContext(key) throws if context is not defined', () => { }).toThrowError(`No context value present for ${key} key`); }); +test('construct.getAllContext can be used to read the full context of a root node', () => { + // GIVEN + const context = { + ctx1: 12, + ctx2: 'hello', + }; + + // WHEN + const t = new Root(); + for (const [k, v] of Object.entries(context)) { + t.node.setContext(k, v); + } + + // THEN + expect(t.node.getAllContext()).toStrictEqual(context); +}); + test('construct.getAllContext can be used to read the full context of a node', () => { // GIVEN const context = {