Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uuid for action IDs instead of Math.random (fixes #1109) #1347

Merged
merged 3 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion addons/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"deep-equal": "^1.0.1",
"json-stringify-safe": "^5.0.1",
"prop-types": "^15.5.8",
"react-inspector": "^2.0.0"
"react-inspector": "^2.0.0",
"uuid": "^3.1.0"
},
"devDependencies": {
"react": "^15.5.4",
Expand Down
5 changes: 3 additions & 2 deletions addons/actions/src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import addons from '@storybook/addons';
import stringify from 'json-stringify-safe';
import uuid from 'uuid/v1';
import { EVENT_ID } from './';

function _format(arg) {
Expand All @@ -16,9 +17,9 @@ export function action(name) {
const handler = function(..._args) {
const args = Array.from(_args).map(_format);
const channel = addons.getChannel();
const randomId = Math.random().toString(16).slice(2);
const id = uuid();
channel.emit(EVENT_ID, {
id: randomId,
id,
data: { name, args },
});
};
Expand Down
27 changes: 27 additions & 0 deletions addons/actions/src/preview.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import addons from '@storybook/addons';
import uuid from 'uuid/v1';
import { action } from './preview';

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

describe('preview', () => {
describe('action()', () => {
it('should use a uuid for action ids', () => {
const channel = { emit: jest.fn() };
const uuidGenerator = (function*() {
yield '42';
yield '24';
})();
uuid.mockImplementation(() => uuidGenerator.next().value);
addons.getChannel.mockReturnValue(channel);
const fn = action('foo');

fn();
fn();
expect(channel.emit).toHaveBeenCalledTimes(2);
expect(channel.emit.mock.calls[0][1].id).toBe('42');
expect(channel.emit.mock.calls[1][1].id).toBe('24');
});
});
});