Decouple initial viewports from SelectViewport component

This commit is contained in:
Mohamed Shaaban 2018-02-25 23:10:35 +01:00
parent e2daecf4b4
commit 7ad2e8cc9f
6 changed files with 40 additions and 17 deletions

View File

@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { baseFonts } from '@storybook/components'; import { baseFonts } from '@storybook/components';
import { document } from 'global'; import { document } from 'global';
import { viewports, defaultViewport, resetViewport } from './viewportInfo'; import { initialViewports, defaultViewport, resetViewport } from './viewportInfo';
import { SelectViewport } from './SelectViewport'; import { SelectViewport } from './SelectViewport';
import { RotateViewport } from './RotateViewport'; import { RotateViewport } from './RotateViewport';
import { UPDATE_VIEWPORT_EVENT_ID } from '../../shared'; import { UPDATE_VIEWPORT_EVENT_ID } from '../../shared';
@ -27,6 +27,7 @@ export class Panel extends Component {
super(props, context); super(props, context);
this.state = { this.state = {
viewport: defaultViewport, viewport: defaultViewport,
viewports: initialViewports,
isLandscape: false, isLandscape: false,
}; };
@ -64,7 +65,7 @@ export class Panel extends Component {
}; };
updateIframe = () => { updateIframe = () => {
const { viewport: viewportKey, isLandscape } = this.state; const { viewports, viewport: viewportKey, isLandscape } = this.state;
const viewport = viewports[viewportKey] || resetViewport; const viewport = viewports[viewportKey] || resetViewport;
if (!this.iframe) { if (!this.iframe) {
@ -82,7 +83,7 @@ export class Panel extends Component {
}; };
render() { render() {
const { isLandscape, viewport } = this.state; const { isLandscape, viewport, viewports } = this.state;
const disableDefault = viewport === defaultViewport; const disableDefault = viewport === defaultViewport;
const disabledStyles = disableDefault ? styles.disabled : {}; const disabledStyles = disableDefault ? styles.disabled : {};
@ -97,6 +98,8 @@ export class Panel extends Component {
return ( return (
<div style={containerStyles}> <div style={containerStyles}>
<SelectViewport <SelectViewport
viewports={viewports}
defaultViewport={defaultViewport}
activeViewport={viewport} activeViewport={viewport}
onChange={e => this.changeViewport(e.target.value)} onChange={e => this.changeViewport(e.target.value)}
/> />

View File

@ -1,10 +1,9 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { viewports, defaultViewport } from './viewportInfo';
import * as styles from './styles'; import * as styles from './styles';
export function SelectViewport({ activeViewport, onChange }) { export function SelectViewport({ viewports, defaultViewport, activeViewport, onChange }) {
return ( return (
<div style={styles.row}> <div style={styles.row}>
<label htmlFor="device" style={styles.label}> <label htmlFor="device" style={styles.label}>
@ -25,4 +24,6 @@ export function SelectViewport({ activeViewport, onChange }) {
SelectViewport.propTypes = { SelectViewport.propTypes = {
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
activeViewport: PropTypes.string.isRequired, activeViewport: PropTypes.string.isRequired,
viewports: PropTypes.shape({}).isRequired,
defaultViewport: PropTypes.string.isRequired,
}; };

View File

@ -3,7 +3,7 @@ import { shallow } from 'enzyme';
import { document } from 'global'; import { document } from 'global';
import { Panel } from '../Panel'; import { Panel } from '../Panel';
import { viewports, defaultViewport, resetViewport } from '../viewportInfo'; import { initialViewports, defaultViewport, resetViewport } from '../viewportInfo';
import * as styles from '../styles'; import * as styles from '../styles';
describe('Viewport/Panel', () => { describe('Viewport/Panel', () => {
@ -24,6 +24,7 @@ describe('Viewport/Panel', () => {
it('creates the initial state', () => { it('creates the initial state', () => {
expect(subject.instance().state).toEqual({ expect(subject.instance().state).toEqual({
viewport: defaultViewport, viewport: defaultViewport,
viewports: initialViewports,
isLandscape: false, isLandscape: false,
}); });
}); });
@ -76,13 +77,13 @@ describe('Viewport/Panel', () => {
describe('new viewport', () => { describe('new viewport', () => {
beforeEach(() => { beforeEach(() => {
subject.instance().changeViewport(viewports[0]); subject.instance().changeViewport(initialViewports[0]);
}); });
it('sets the state with the new information', () => { it('sets the state with the new information', () => {
expect(subject.instance().setState).toHaveBeenCalledWith( expect(subject.instance().setState).toHaveBeenCalledWith(
{ {
viewport: viewports[0], viewport: initialViewports[0],
isLandscape: false, isLandscape: false,
}, },
subject.instance().updateIframe subject.instance().updateIframe
@ -204,6 +205,22 @@ describe('Viewport/Panel', () => {
); );
}); });
it('passes the defaultViewport', () => {
expect(select.props()).toEqual(
expect.objectContaining({
defaultViewport,
})
);
});
it('passes the initialViewports', () => {
expect(select.props()).toEqual(
expect.objectContaining({
viewports: initialViewports,
})
);
});
it('onChange it updates the viewport', () => { it('onChange it updates the viewport', () => {
const e = { target: { value: 'iphone6' } }; const e = { target: { value: 'iphone6' } };
select.simulate('change', e); select.simulate('change', e);

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { SelectViewport } from '../SelectViewport'; import { SelectViewport } from '../SelectViewport';
import { viewports, defaultViewport } from '../viewportInfo'; import { initialViewports, defaultViewport } from '../viewportInfo';
import * as styles from '../styles'; import * as styles from '../styles';
describe('Viewport/SelectViewport', () => { describe('Viewport/SelectViewport', () => {
@ -12,6 +12,8 @@ describe('Viewport/SelectViewport', () => {
props = { props = {
onChange: jest.fn(), onChange: jest.fn(),
activeViewport: defaultViewport, activeViewport: defaultViewport,
viewports: initialViewports,
defaultViewport,
}; };
subject = shallow(<SelectViewport {...props} />); subject = shallow(<SelectViewport {...props} />);
@ -35,7 +37,7 @@ describe('Viewport/SelectViewport', () => {
}); });
describe('dynamic options', () => { describe('dynamic options', () => {
const viewportKeys = Object.keys(viewports); const viewportKeys = Object.keys(initialViewports);
it('has at least 1 option', () => { it('has at least 1 option', () => {
expect(viewportKeys.length).toBeGreaterThan(0); expect(viewportKeys.length).toBeGreaterThan(0);
}); });
@ -43,9 +45,9 @@ describe('Viewport/SelectViewport', () => {
viewportKeys.forEach(key => { viewportKeys.forEach(key => {
let option; let option;
it(`renders an option for ${viewports[key].name}`, () => { it(`renders an option for ${initialViewports[key].name}`, () => {
option = subject.find(`[value="${key}"]`); option = subject.find(`[value="${key}"]`);
expect(option.text()).toEqual(viewports[key].name); expect(option.text()).toEqual(initialViewports[key].name);
}); });
}); });
}); });

View File

@ -1,12 +1,12 @@
import { viewports, resetViewport, configuredStyles } from '../viewportInfo'; import { initialViewports, resetViewport, configuredStyles } from '../viewportInfo';
describe('Viewport/constants', () => { describe('Viewport/constants', () => {
describe('viewports', () => { describe('initialViewports', () => {
it('includes the default styles on every custom viewport', () => { it('includes the default styles on every custom viewport', () => {
const keys = Object.keys(viewports); const keys = Object.keys(initialViewports);
keys.forEach(key => { keys.forEach(key => {
expect(viewports[key].styles).toEqual(expect.objectContaining(configuredStyles)); expect(initialViewports[key].styles).toEqual(expect.objectContaining(configuredStyles));
}); });
}); });
}); });

View File

@ -18,7 +18,7 @@ export const resetViewport = {
}, },
}; };
export const viewports = { export const initialViewports = {
iphone5: { iphone5: {
name: 'iPhone 5', name: 'iPhone 5',
styles: { styles: {