Added allowDeprecatedUsage option to makeDecorator

(and enable it for all existing addons)
This commit is contained in:
Tom Coleman 2018-07-04 15:56:17 +10:00
parent 759d741f03
commit aa8a421907
6 changed files with 36 additions and 7 deletions

View File

@ -15,6 +15,7 @@ export const withBackgrounds = makeDecorator({
name: 'backgrounds', name: 'backgrounds',
parameterName: 'backgrounds', parameterName: 'backgrounds',
skipIfNoParametersOrOptions: true, skipIfNoParametersOrOptions: true,
allowDeprecatedUsage: true,
wrapper: (getStory, context, { options, parameters }) => { wrapper: (getStory, context, { options, parameters }) => {
const backgrounds = parameters || options; const backgrounds = parameters || options;

View File

@ -87,6 +87,7 @@ function addInfo(storyFn, context, infoOptions) {
export const withInfo = makeDecorator({ export const withInfo = makeDecorator({
name: 'withInfo', name: 'withInfo',
parameterName: 'info', parameterName: 'info',
allowDeprecatedUsage: true,
wrapper: (getStory, context, { options, parameters }) => { wrapper: (getStory, context, { options, parameters }) => {
const storyOptions = parameters || options; const storyOptions = parameters || options;
const infoOptions = typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions; const infoOptions = typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions;

View File

@ -77,6 +77,7 @@ export const withKnobs = makeDecorator({
name: 'withKnobs', name: 'withKnobs',
parameterName: 'knobs', parameterName: 'knobs',
skipIfNoParametersOrOptions: false, skipIfNoParametersOrOptions: false,
allowDeprecatedUsage: true,
wrapper: (getStory, context, { options, parameters }) => { wrapper: (getStory, context, { options, parameters }) => {
const storyOptions = parameters || options; const storyOptions = parameters || options;
const allOptions = { ...defaultOptions, ...storyOptions }; const allOptions = { ...defaultOptions, ...storyOptions };

View File

@ -10,6 +10,7 @@ export const withNotes = makeDecorator({
name: 'withNotes', name: 'withNotes',
parameterName: 'notes', parameterName: 'notes',
skipIfNoParametersOrOptions: true, skipIfNoParametersOrOptions: true,
allowDeprecatedUsage: true,
wrapper: (getStory, context, { options, parameters }) => { wrapper: (getStory, context, { options, parameters }) => {
const channel = addons.getChannel(); const channel = addons.getChannel();

View File

@ -15,6 +15,7 @@ export const makeDecorator = ({
parameterName, parameterName,
wrapper, wrapper,
skipIfNoParametersOrOptions = false, skipIfNoParametersOrOptions = false,
allowDeprecatedUsage = false,
}) => { }) => {
const decorator = options => (getStory, context) => { const decorator = options => (getStory, context) => {
const parameters = context.parameters && context.parameters[parameterName]; const parameters = context.parameters && context.parameters[parameterName];
@ -44,11 +45,17 @@ export const makeDecorator = ({
return decorator(...args)(...innerArgs); return decorator(...args)(...innerArgs);
} }
// Used to wrap a story directly .add('story', decorator(options)(() => <Story />)) if (allowDeprecatedUsage) {
// This is now deprecated: // Used to wrap a story directly .add('story', decorator(options)(() => <Story />))
return deprecate( // This is now deprecated:
context => decorator(...args)(innerArgs[0], context), return deprecate(
`Passing stories directly into ${name}() is deprecated, instead use addDecorator(${name}) and pass options with the '${parameterName}' parameter` context => decorator(...args)(innerArgs[0], context),
`Passing stories directly into ${name}() is deprecated, instead use addDecorator(${name}) and pass options with the '${parameterName}' parameter`
);
}
throw new Error(
`Passing stories directly into ${name}() is not allowed, instead use addDecorator(${name}) and pass options with the '${parameterName}' parameter`
); );
}; };
}; };

View File

@ -103,10 +103,15 @@ describe('makeDecorator', () => {
expect(story).toHaveBeenCalled(); expect(story).toHaveBeenCalled();
}); });
it('passes options added at story time, but with a deprecation warning', () => { it('passes options added at story time, but with a deprecation warning, if allowed', () => {
deprecatedFns = []; deprecatedFns = [];
const wrapper = jest.fn(); const wrapper = jest.fn();
const decorator = makeDecorator({ wrapper, name: 'test', parameterName: 'test' }); const decorator = makeDecorator({
wrapper,
name: 'test',
parameterName: 'test',
allowDeprecatedUsage: true,
});
const options = 'test-val'; const options = 'test-val';
const story = jest.fn(); const story = jest.fn();
const decoratedStory = decorator(options)(story); const decoratedStory = decorator(options)(story);
@ -121,4 +126,17 @@ describe('makeDecorator', () => {
}); });
expect(deprecatedFns[0].deprecatedFn).toHaveBeenCalled(); expect(deprecatedFns[0].deprecatedFn).toHaveBeenCalled();
}); });
it('throws if options are added at storytime, if not allowed', () => {
const wrapper = jest.fn();
const decorator = makeDecorator({
wrapper,
name: 'test',
parameterName: 'test',
allowDeprecatedUsage: false,
});
const options = 'test-val';
const story = jest.fn();
expect(() => decorator(options)(story)).toThrow(/not allowed/);
});
}); });