Skip to content

Commit

Permalink
Add addon-jest to examples/cra-kitchen-sink
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudtertrais committed Nov 16, 2017
1 parent 1073760 commit d185d1a
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 156 deletions.
6 changes: 0 additions & 6 deletions addons/jest/example/.storybook/addons.js

This file was deleted.

17 changes: 0 additions & 17 deletions addons/jest/example/.storybook/config.js

This file was deleted.

9 changes: 0 additions & 9 deletions addons/jest/example/List.js

This file was deleted.

12 changes: 0 additions & 12 deletions addons/jest/example/List.story.js

This file was deleted.

13 changes: 0 additions & 13 deletions addons/jest/example/List.test.js

This file was deleted.

9 changes: 0 additions & 9 deletions addons/jest/example/withTests.js

This file was deleted.

3 changes: 2 additions & 1 deletion addons/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"dependencies": {
"@storybook/addons": "^3.2.15",
"global": "^4.3.2",
"prop-types": "^15.6.0"
"prop-types": "^15.6.0",
"styled-components": "^2.2.3"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down
10 changes: 8 additions & 2 deletions addons/jest/src/components/Indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ const Indicator = ({ color, size, children = '', right }) => (
{children}
</div>
);

Indicator.defaultProps = {
right: false,
children: null,
};

Indicator.propTypes = {
color: PropTypes.string.isRequired,
size: PropTypes.number.isRequired,
children: PropTypes.node.isRequired,
right: PropTypes.bool.isRequired,
children: PropTypes.node,
right: PropTypes.bool,
};

export default Indicator;
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from 'react';
import PropTypes from 'prop-types';

import Indicator from './Indicator';
import provideTests from '../hoc/provideTests';
import Result from './Result';
import provideJestResult from '../hoc/provideJestResult';
import colors from '../colors';

const TestsPanel = ({ tests }) => {
const Panel = ({ tests }) => {
const style = {
padding: '10px 20px',
flex: 1,
Expand Down Expand Up @@ -104,58 +105,29 @@ const TestsPanel = ({ tests }) => {
</div>
</h2>
<ul style={{ listStyle: 'none', fontSize: 14 }}>
{result.assertionResults.map(({ fullName, status, failureMessages }) => {
const color = status === 'passed' ? colors.success : colors.error;
return (
<li key={fullName}>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<div style={{ display: 'flex', alignItems: 'center' }}>
<Indicator color={color} size={10} />
<div>{fullName}</div>
</div>
<div style={{ display: 'flex', alignItems: 'center' }}>
<Indicator color={color} size={14} right>
{status}
</Indicator>
</div>
</div>
{/* eslint-disable react/no-array-index-key */}
{failureMessages &&
failureMessages.map((msg, i) => (
<pre
key={i}
style={{ marginTop: 5, whiteSpace: 'pre-wrap' }}
dangerouslySetInnerHTML={{
__html: msg
.replace(/\[22?m?/g, '')
.replace(/\[31m/g, `<strong style="color: ${colors.error}">`)
.replace(/\[32m/g, `<strong style="color: ${colors.success}">`)
.replace(/\[39m/g, '</strong>'),
}}
/>
))}
</li>
);
})}
{result.assertionResults.map(res => (
<li key={res.fullName || res.title}>
<Result {...res} />
</li>
))}
</ul>
</section>
);
})}
</div>
);
};
TestsPanel.propTypes = {

Panel.defaultProps = {
tests: null,
};

Panel.propTypes = {
tests: PropTypes.arrayOf(
PropTypes.shape({
result: PropTypes.object,
})
).isRequired,
),
};

export default provideTests(TestsPanel);
export default provideJestResult(Panel);
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';

import provideTests from '../hoc/provideTests';
import provideJestResult from '../hoc/provideJestResult';
import Indicator from './Indicator';
import colors from '../colors';

const TestPanelTitle = ({ tests }) => {
const PanelTitle = ({ tests }) => {
if (!tests) {
return (
<div style={{ display: 'flex', alignItems: 'center' }}>
Expand All @@ -26,12 +26,17 @@ const TestPanelTitle = ({ tests }) => {
</div>
);
};
TestPanelTitle.propTypes = {

PanelTitle.defaultProps = {
tests: null,
};

PanelTitle.propTypes = {
tests: PropTypes.arrayOf(
PropTypes.shape({
result: PropTypes.object,
})
).isRequired,
),
};

export default provideTests(TestPanelTitle);
export default provideJestResult(PanelTitle);
65 changes: 65 additions & 0 deletions addons/jest/src/components/Result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';

import Indicator from './Indicator';
import colors from '../colors';

const FlexContainer = styled.div`
display: flex;
align-items: center;
`;

const Result = ({ fullName, title, status, failureMessages }) => {
const color = status === 'passed' ? colors.success : colors.error;
return (
<div>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<FlexContainer>
<Indicator color={color} size={10} />
<div>{fullName || title}</div>
</FlexContainer>
<FlexContainer>
<Indicator color={color} size={14} right>
{status}
</Indicator>
</FlexContainer>
</div>
{/* eslint-disable react/no-array-index-key, react/no-danger */}
{failureMessages &&
failureMessages.map((msg, i) => (
<pre
key={i}
style={{ marginTop: 5, whiteSpace: 'pre-wrap' }}
dangerouslySetInnerHTML={{
__html: msg
.replace(/\[22?m?/g, '')
.replace(/\[31m/g, `<strong style="color: ${colors.error}">`)
.replace(/\[32m/g, `<strong style="color: ${colors.success}">`)
.replace(/\[39m/g, '</strong>'),
}}
/>
))}
</div>
);
};

Result.defaultProps = {
fullName: '',
title: '',
};

Result.propTypes = {
fullName: PropTypes.string,
title: PropTypes.string,
status: PropTypes.string.isRequired,
failureMessages: PropTypes.arrayOf(PropTypes.string).isRequired,
};

export default Result;
File renamed without changes.
36 changes: 35 additions & 1 deletion addons/jest/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
export { default } from './withTests';
import addons from '@storybook/addons';

const findTestResults = (testFiles, jestTestResults, jestTestFilesExt) =>
testFiles.map(name => {
if (jestTestResults && jestTestResults.testResults) {
return {
name,
result: jestTestResults.testResults.find(t =>
new RegExp(`${name}${jestTestFilesExt}`).test(t.name)
),
};
}
return { name };
});

const emitAddTests = ({ kind, story, testFiles, options }) => {
addons.getChannel().emit('storybook/tests/add_tests', {
kind,
storyName: story,
tests: findTestResults(testFiles, options.results, options.filesExt),
});
};

export const withTests = userOptions => {
const defaultOptions = {
filesExt: '((\\.specs?)|(\\.tests?))?(\\.js)?$',
};
const options = Object.assign({}, defaultOptions, userOptions);

return (...testFiles) => (storyFn, { kind, story }) => {
emitAddTests({ kind, story, testFiles, options });

return storyFn();
};
};
8 changes: 4 additions & 4 deletions addons/jest/src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import React from 'react';
import addons from '@storybook/addons';

import TestPanelTitle from './components/TestsPanelTitle';
import TestsPanel from './components/TestsPanel';
import PanelTitle from './components/PanelTitle';
import Panel from './components/Panel';

// Register the addon with a unique name.
addons.register('storybook/tests', api => {
// Also need to set a unique name to the panel.
addons.addPanel('storybook/tests/panel', {
title: <TestPanelTitle channel={addons.getChannel()} api={api} />,
render: () => <TestsPanel channel={addons.getChannel()} api={api} />,
title: <PanelTitle channel={addons.getChannel()} api={api} />,
render: () => <Panel channel={addons.getChannel()} api={api} />,
});
});
31 changes: 0 additions & 31 deletions addons/jest/src/withTests.js

This file was deleted.

4 changes: 4 additions & 0 deletions examples/cra-kitchen-sink/.jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
2 changes: 2 additions & 0 deletions examples/cra-kitchen-sink/.storybook/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ import '@storybook/addon-options/register';
import '@storybook/addon-knobs/register';
import '@storybook/addon-backgrounds/register';
import '@storybook/addon-a11y/register';
import '@storybook/addon-jest/register';
import '@storybook/addon-jest/styles';
1 change: 1 addition & 0 deletions examples/cra-kitchen-sink/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'file-stub';
1 change: 1 addition & 0 deletions examples/cra-kitchen-sink/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
Loading

0 comments on commit d185d1a

Please sign in to comment.