Skip to content

Commit

Permalink
chore: improve .with error management (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimee authored Sep 20, 2024
1 parent 6ab9622 commit 84fa7f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ module.exports = class Context {
}

_checkKeyNotAvailable(name) {
if (!this._bag[name]) throw new Error(`Key does not exists: "${name}"`);
(Array.isArray(name) ? name : [name]).forEach((key) => {
if (!this._bag[key]) throw new Error(`Key does not exists: ${key}`);
});
}

_setNewValue(name, value, options = {}) {
Expand Down Expand Up @@ -282,6 +284,7 @@ module.exports = class Context {

with(name, work) {
try {
this._checkKeyNotAvailable(name);
work(this._lookup(name));
return this;
} catch (cause) {
Expand Down
36 changes: 27 additions & 9 deletions test/integration/plan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,30 @@ describe('Plan', () => {
key2: 'value2',
});
});

it('runs a callback "with" missing context elements', () => {
const callback = jest.fn();
const planWithCallback = (plan) => plan
.addValue('key', 'value')
.addValue('key2', 'value2')
.with('not-existing-key', callback);

expect(() => execute(planWithCallback)).toThrow('Using with on path "/key2": Key does not exists: not-existing-key');

expect(callback).not.toHaveBeenCalled();
});

it('runs a callback "with" missing context elements', () => {
const callback = jest.fn();
const planWithCallback = (plan) => plan
.addValue('key', 'value')
.addValue('key2', 'value2')
.with(['not-existing-key', 'key2'], callback);

expect(() => execute(planWithCallback)).toThrow('Using with on path "/key2": Key does not exists: not-existing-key');

expect(callback).not.toHaveBeenCalled();
});
});

describe('private scope', () => {
Expand Down Expand Up @@ -791,9 +815,8 @@ describe('Plan', () => {
});

it('private value is not exposed to other steps', () => {
expect.assertions(7);
expect.assertions(4);

const receiveUndefined = jest.fn();
const receiveTheValue = jest.fn();

const plan = newPlan()
Expand All @@ -803,24 +826,19 @@ describe('Plan', () => {
.addStep('step_0_1', (step1Plan) => step1Plan
.addValue('step01Private', 'visible only in step_0_1', { private: true })
.with('step01Private', receiveTheValue)
.with('rootPrivate', receiveTheValue))
.with('step01Private', receiveUndefined))
.with('rootPrivate', receiveTheValue)))
.addStep('step_1', (step2Plan) => step2Plan
.with('step01Private', receiveUndefined)
.with('rootPrivate', receiveTheValue)));

const {
rootPrivate, step01Private,
rootPrivate,
} = execute(plan);

expect(rootPrivate).toBeUndefined();
expect(step01Private).toBeUndefined();

expect(receiveTheValue).toHaveBeenNthCalledWith(1, 'visible only in step_0_1');
expect(receiveTheValue).toHaveBeenNthCalledWith(2, 'value seen by root and sons');
expect(receiveTheValue).toHaveBeenNthCalledWith(3, 'value seen by root and sons');
expect(receiveUndefined).toHaveBeenNthCalledWith(1, undefined);
expect(receiveUndefined).toHaveBeenNthCalledWith(2, undefined);
});

it('not private value is accessible from other steps', () => {
Expand Down

0 comments on commit 84fa7f0

Please sign in to comment.