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

fix: rootNode.getAllContext() returns empty object #2483

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading