Skip to content

Commit 1eb4dca

Browse files
authoredOct 10, 2024
fix: rootNode.getAllContext() returns empty object (#2483)
Correct getAllContext() to return the root node's context in addition to specified defaults. Fixes: #2239
1 parent 01cf3f8 commit 1eb4dca

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed
 

‎src/construct.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,8 @@ export class Node {
245245
* @returns The context object or an empty object if there is discovered context
246246
*/
247247
public getAllContext(defaults?: object): any {
248-
if (typeof defaults === 'undefined') {
249-
defaults = {};
250-
}
251-
252-
if (this.scope === undefined) { return defaults; }
253-
254-
const value = { ...this._context, ...defaults };
255-
return this.scope && this.scope.node.getAllContext(value);
248+
return this.scopes.reverse()
249+
.reduce((a, s) => ({ ...(s.node._context), ...a }), { ...defaults });
256250
}
257251

258252
/**

‎test/construct.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ test('construct.getContext(key) throws if context is not defined', () => {
150150
}).toThrowError(`No context value present for ${key} key`);
151151
});
152152

153+
test('construct.getAllContext can be used to read the full context of a root node', () => {
154+
// GIVEN
155+
const context = {
156+
ctx1: 12,
157+
ctx2: 'hello',
158+
};
159+
160+
// WHEN
161+
const t = new Root();
162+
for (const [k, v] of Object.entries(context)) {
163+
t.node.setContext(k, v);
164+
}
165+
166+
// THEN
167+
expect(t.node.getAllContext()).toStrictEqual(context);
168+
});
169+
153170
test('construct.getAllContext can be used to read the full context of a node', () => {
154171
// GIVEN
155172
const context = {

0 commit comments

Comments
 (0)
Failed to load comments.