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

Commit

Permalink
Merge pull request #756 from Shopify/pass-default-computed-value
Browse files Browse the repository at this point in the history
Pass computed default value to slate.config.js functions
  • Loading branch information
t-kelly authored Sep 14, 2018
2 parents 52f0bb1 + 7fd627e commit 7aed317
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
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

0 comments on commit 7aed317

Please sign in to comment.