Skip to content

Commit

Permalink
Fix for issue 5932, adding default options to action(s) (#6438)
Browse files Browse the repository at this point in the history
Fix for issue 5932, adding default options to action(s)
  • Loading branch information
ndelangen authored and shilman committed Jun 13, 2019
1 parent 7f253f5 commit 8bad7be
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 68 deletions.
130 changes: 64 additions & 66 deletions addons/actions/src/preview/__tests__/action.test.js
@@ -1,6 +1,5 @@
import addons from '@storybook/addons';
import { action } from '../..';
// import { configureActions } from '../..';
import { action, configureActions } from '../..';

jest.mock('@storybook/addons');

Expand Down Expand Up @@ -29,67 +28,66 @@ describe('Action', () => {
});
});

// TODO: This functionality is removed, unsure if to add back or remove
// describe('Depth config', () => {
// it('with global depth configuration', () => {
// const channel = createChannel();

// const depth = 1;

// configureActions({
// depth,
// });

// action('test-action')({
// root: {
// one: {
// two: 'foo',
// },
// },
// });

// expect(getChannelData(channel)[0]).toEqual({
// root: {
// one: {
// two: 'foo',
// },
// },
// });
// });

// it('per action depth option overrides global config', () => {
// const channel = createChannel();

// configureActions({
// depth: 1,
// });

// action('test-action', { depth: 3 })({
// root: {
// one: {
// two: {
// three: {
// four: {
// five: 'foo',
// },
// },
// },
// },
// },
// });

// expect(getChannelData(channel)[0]).toEqual({
// root: {
// one: {
// two: {
// three: {
// four: {
// five: 'foo',
// },
// },
// },
// },
// },
// });
// });
// });
describe('Depth config', () => {
it('with global depth configuration', () => {
const channel = createChannel();

const depth = 1;

configureActions({
depth,
});

action('test-action')({
root: {
one: {
two: 'foo',
},
},
});

expect(getChannelData(channel)[0]).toEqual({
root: {
one: {
two: 'foo',
},
},
});
});

it('per action depth option overrides global config', () => {
const channel = createChannel();

configureActions({
depth: 1,
});

action('test-action', { depth: 3 })({
root: {
one: {
two: {
three: {
four: {
five: 'foo',
},
},
},
},
},
});

expect(getChannelData(channel)[0]).toEqual({
root: {
one: {
two: {
three: {
four: {
five: 'foo',
},
},
},
},
},
});
});
});
2 changes: 2 additions & 0 deletions addons/actions/src/preview/action.ts
Expand Up @@ -2,9 +2,11 @@ import uuid from 'uuid/v1';
import { addons } from '@storybook/addons';
import { EVENT_ID } from '../constants';
import { ActionDisplay, ActionOptions, HandlerFunction } from '../models';
import { config } from './configureActions';

export function action(name: string, options: ActionOptions = {}): HandlerFunction {
const actionOptions = {
...config,
...options,
};

Expand Down
8 changes: 6 additions & 2 deletions addons/actions/src/preview/actions.ts
@@ -1,12 +1,16 @@
import { action } from './action';
import { ActionOptions, ActionsMap } from '../models';
import { config } from './configureActions';

export function actions(...args: any[]): ActionsMap {
let options: ActionOptions = {};
let options: ActionOptions = config;
const names = args;
// last argument can be options
if (names.length !== 1 && typeof args[args.length - 1] !== 'string') {
options = names.pop();
options = {
...config,
...names.pop(),
};
}

let namesObject = names[0];
Expand Down

0 comments on commit 8bad7be

Please sign in to comment.