Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Pass computed default value to slate.config.js functions #756

Merged
merged 1 commit into from
Sep 14, 2018
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
23 changes: 22 additions & 1 deletion packages/slate-config/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,35 @@ describe('SlateConfig.get()', () => {
expect(config.get('some.other.key')).toBe(schema['some.other.key']);
});

test('if the value is a function, the function is executed with the config instance as the only argument', () => {
test('if the value is a function and not specified in this.userConfig, the function is executed with the config instance as the only argument', () => {
const SlateConfig = require('../index');
const config = new SlateConfig(schema);
const value = config.get('some.function');

expect(schema['some.function']).toBeCalledWith(config);
expect(value).toBe(schema['some.other.key']);
});

test('if the value is specified in this.userConfig and is a function, it is executed with the config instance and the computed default value as arguments', () => {
const userConfigValue = 'someNewValue';
const userConfigFunction = jest.fn(() => userConfigValue);

const SlateConfig = require('../index');
const config = new SlateConfig(schema);
const defaultValue = config.get('some.function');

global.slateUserConfig = {
'some.function': userConfigFunction,
};

const value = config.get('some.function');

expect(global.slateUserConfig['some.function']).toBeCalledWith(
config,
defaultValue,
);
expect(value).toBe(userConfigValue);
});
});

describe('SlateConfig.set()', () => {
Expand Down
31 changes: 21 additions & 10 deletions packages/slate-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,31 @@ module.exports = class SlateConfig {
}

get(key) {
const userConfig = this.userConfig;
const value =
typeof userConfig[key] === 'undefined'
? this.schema[key]
: userConfig[key];

if (typeof value === 'function') {
return value(this);
} else if (typeof value === 'undefined') {
const defaultValue = this.schema[key];
const userConfigValue = this.userConfig[key];
let computedDefaultValue;

if (
typeof defaultValue === 'undefined' &&
typeof userConfigValue === 'undefined'
) {
throw new Error(
`[slate-config]: A value has not been defined for the key '${key}'`,
);
}

if (typeof defaultValue === 'function') {
computedDefaultValue = defaultValue(this);
} else {
computedDefaultValue = defaultValue;
}

if (typeof userConfigValue === 'undefined') {
return computedDefaultValue;
} else if (typeof userConfigValue === 'function') {
return userConfigValue(this, computedDefaultValue);
} else {
return value;
return userConfigValue;
}
}
};
Expand Down