Fix Select array values showing k

This commit is contained in:
Steven Sinatra 2018-10-26 17:22:51 +11:00
parent ca1cd1e2fc
commit a39bec15cc
2 changed files with 38 additions and 16 deletions

View File

@ -5,24 +5,46 @@ import SelectType from '../types/Select';
describe('Select', () => {
let knob;
beforeEach(() => {
knob = {
name: 'Colors',
value: '#00ff00',
options: {
Green: '#00ff00',
Red: '#ff0000',
},
};
describe('Object values', () => {
beforeEach(() => {
knob = {
name: 'Colors',
value: '#00ff00',
options: {
Green: '#00ff00',
Red: '#ff0000',
},
};
});
describe('displays value', () => {
it('correctly maps option keys and values', () => {
const wrapper = shallow(<SelectType knob={knob} />);
const green = wrapper.find('option').first();
expect(green.text()).toEqual('Green');
expect(green.prop('value')).toEqual('Green');
});
});
});
describe('displays value', () => {
it('correctly maps option keys and values', () => {
const wrapper = shallow(<SelectType knob={knob} />);
describe('Array values', () => {
beforeEach(() => {
knob = {
name: 'Colors',
value: 'green',
options: ['green', 'red'],
};
});
const green = wrapper.find('option').first();
expect(green.text()).toEqual('Green');
expect(green.prop('value')).toEqual('Green');
describe('displays value', () => {
it('correctly maps option keys and values', () => {
const wrapper = shallow(<SelectType knob={knob} />);
const green = wrapper.find('option').first();
expect(green.text()).toEqual('green');
expect(green.prop('value')).toEqual('green');
});
});
});
});

View File

@ -6,7 +6,7 @@ import { Select } from '@storybook/components';
const SelectType = ({ knob, onChange }) => {
const { options } = knob;
const entries = Array.isArray(options)
? options.reduce((acc, k) => Object.assign(acc, { k }), {})
? options.reduce((acc, k) => Object.assign(acc, { [k]: k }), {})
: options;
const selectedKey = Object.keys(entries).find(k => entries[k] === knob.value);