Skip to content

Commit

Permalink
Refactor sorting implementation to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktvaldez committed Apr 11, 2019
1 parent 6ec3ebb commit 7edfeb0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
17 changes: 12 additions & 5 deletions addons/knobs/src/components/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import PropForm from './PropForm';

const getTimestamp = () => +new Date();

const DEFAULT_GROUP_ID = 'Other';
export const DEFAULT_GROUP_ID = 'Other';

const PanelWrapper = styled(({ children, className }) => (
<ScrollArea horizontal vertical className={className}>
Expand Down Expand Up @@ -198,10 +198,17 @@ export default class KnobPanel extends PureComponent {
}

// Always sort DEFAULT_GROUP_ID (ungrouped) tab last without changing the remaining tabs
const unsortedEntries = Object.entries(groups);
const entries = unsortedEntries
.filter(entry => entry[0] !== DEFAULT_GROUP_ID)
.push(unsortedEntries.find(entry => entry[0] === DEFAULT_GROUP_ID));
const sortEntries = g => {
const unsortedKeys = Object.keys(g);
if (unsortedKeys.indexOf(DEFAULT_GROUP_ID) !== -1) {
const sortedKeys = unsortedKeys.filter(key => key !== DEFAULT_GROUP_ID);
sortedKeys.push(DEFAULT_GROUP_ID);
return sortedKeys.map(key => [key, g[key]]);
}
return Object.entries(g);
};

const entries = sortEntries(groups);

return (
<Fragment>
Expand Down
20 changes: 10 additions & 10 deletions addons/knobs/src/components/__tests__/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STORY_CHANGED } from '@storybook/core-events';
import { TabsState } from '@storybook/components';

import { ThemeProvider, themes, convert } from '@storybook/theming';
import Panel from '../Panel';
import Panel, { DEFAULT_GROUP_ID } from '../Panel';
import { CHANGE, SET } from '../../shared';
import PropForm from '../PropForm';

Expand Down Expand Up @@ -153,7 +153,7 @@ describe('Panel', () => {
root.unmount();
});

it('should have one tab per groupId and an empty Other tab when all are defined', () => {
it('should have one tab per groupId when all are defined', () => {
const root = mount(
<ThemeProvider theme={convert(themes.light)}>
<Panel api={testApi} active />
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('Panel', () => {
root.unmount();
});

it('the Other tab should have its own additional content when there are knobs both with and without a groupId', () => {
it(`the ${DEFAULT_GROUP_ID} tab should have its own additional content when there are knobs both with and without a groupId`, () => {
const root = mount(
<ThemeProvider theme={convert(themes.light)}>
<Panel api={testApi} active />
Expand All @@ -202,18 +202,18 @@ describe('Panel', () => {

testApi.on.mock.calls[0][1]({
knobs: {
foo: {
name: 'foo',
defaultValue: 'test',
used: true,
groupId: 'foo',
},
bar: {
name: 'bar',
defaultValue: 'test2',
used: true,
// no groupId
},
foo: {
name: 'foo',
defaultValue: 'test',
used: true,
groupId: 'foo',
},
},
});

Expand All @@ -223,7 +223,7 @@ describe('Panel', () => {
.find(TabsState)
.find('button')
.map(child => child.prop('children'));
expect(titles).toEqual(['foo', 'Other']);
expect(titles).toEqual(['foo', DEFAULT_GROUP_ID]);

const knobs = wrapper.find(PropForm).map(propForm => propForm.prop('knobs'));
// there are props with no groupId so Other should also have its own PropForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Array [
]
`;

exports[`Panel groups should have one tab per groupId and an empty Other tab when all are defined 1`] = `
exports[`Panel groups should have one tab per groupId when all are defined 1`] = `
Array [
.emotion-2 {
box-sizing: border-box;
Expand Down

0 comments on commit 7edfeb0

Please sign in to comment.