Skip to content

Commit 3727b53

Browse files
[Canvas] Storybook for testing and development (#29072)
## Summary This PR adds [Storybook](https://storybook.js.org/) to our testing and development suite. ![screen shot 2019-01-21 at 4 35 32 pm](https://user-images.githubusercontent.com/297604/51502196-9f856780-1d9a-11e9-97bf-07c99c3f279b.png) This will allow us to: 1. create a site outlining all components within Canvas, including their TS type information; 2. demonstrate usage of all components by example; 3. allow for individual component testing, both manually and by Jest; 4. iterate and fix bugs on individual components *without* having to start up Kibana, in a [HMR](https://webpack.js.org/concepts/hot-module-replacement/) environment; 5. automatically generate [snapshots](https://jestjs.io/docs/en/snapshot-testing) based on any examples written; This PR also converts a few components to Typescript and adds examples. ## How this can help us, (with examples) I was inspired to add this when I was fixing #25342. In order to fix my changes, I had to run elasticsearch and kibana, as well as refresh my page whenever I needed to test a change. Had I had a Storybook instance, I would have been done much faster. In this PR, you'll see I converted `AdvancedFilter` from `renderers` and `FontPicker` and `ImageUpload` from `public/components`. Would you believe I discovered and fixed bugs just by converting to Typescript and writing examples? ### `AdvancedFilter` - `onChange` and `commit` are not marked as required in `propTypes`, but the component will error out if they're not supplied. - `commit` was actually being called twice when 'Apply' was clicked. This was shown in the 'Actions' panel when I was testing it. ### `FontPicker` - The `fonts` collection was not strongly-typed, therefore any string could be passed to the `value` parameter without error. - While the code allows for any font string to be given to the component, there is no way to currently select that value, nor type it in within the control. This is likely a bug in design. - The `aria-labeledby` attribute in the drop down includes `undefined`. This is likely a bug in EUI: ![screen shot 2019-01-21 at 4 25 58 pm](https://user-images.githubusercontent.com/297604/51501908-5ed91e80-1d99-11e9-913a-ce1bb5f4e352.png) ## How to use - `cd x-pack/plugins/canvas/` - Run `node scripts/storybook` to start up a local development version, with HMR. - Run `node scripts/storybook_build` to build a complete static version of the book. - Run `node scripts/jest` which will run the Storyshots test; run `node scripts/jest --updateSnapshot` if source code has changed as expected. ## Future Work - Adding Jest coverage and output to the info panels, ([this](https://www.npmjs.com/package/@storybook/addon-jest) is *sick* functionality). - Adding automatic [a11y testing](https://www.npmjs.com/package/@storybook/addon-a11y), (currently [blocked](storybookjs/storybook#4889)). - Adding generic knobs for stories - Adding more example info, (e.g. who edited last, descriptions, etc).
1 parent 716c66f commit 3727b53

File tree

24 files changed

+2755
-216
lines changed

24 files changed

+2755
-216
lines changed

x-pack/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/.env
1111
/.kibana-plugin-helpers.dev.*
1212
!/plugins/infra/**/target
13+
.cache
1314

1415
# We don't want any yarn.lock files in here
1516
/yarn.lock

x-pack/package.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
"@kbn/es": "1.0.0",
3131
"@kbn/plugin-helpers": "9.0.2",
3232
"@kbn/test": "1.0.0",
33+
"@storybook/addon-actions": "^4.1.7",
34+
"@storybook/addon-console": "^1.1.0",
35+
"@storybook/addon-info": "^4.0.7",
36+
"@storybook/addon-knobs": "^4.0.7",
37+
"@storybook/addon-options": "^4.1.7",
38+
"@storybook/addon-storyshots": "^4.1.7",
39+
"@storybook/react": "^4.0.7",
3340
"@types/angular": "1.6.50",
3441
"@types/d3-array": "^1.2.1",
3542
"@types/d3-scale": "^2.0.0",
@@ -52,8 +59,12 @@
5259
"@types/react-dom": "^16.0.5",
5360
"@types/react-redux": "^6.0.6",
5461
"@types/react-router-dom": "^4.3.1",
62+
"@types/recompose": "^0.30.2",
5563
"@types/reduce-reducers": "^0.1.3",
5664
"@types/sinon": "^5.0.1",
65+
"@types/storybook__addon-actions": "^3.4.1",
66+
"@types/storybook__addon-info": "^3.4.2",
67+
"@types/storybook__react": "^4.0.0",
5768
"@types/supertest": "^2.0.5",
5869
"@types/uuid": "^3.4.4",
5970
"abab": "^1.0.4",
@@ -64,6 +75,7 @@
6475
"babel-jest": "^23.6.0",
6576
"babel-plugin-inline-react-svg": "^0.5.4",
6677
"babel-plugin-mock-imports": "^0.0.5",
78+
"babel-plugin-require-context-hook": "^1.0.0",
6779
"babel-plugin-transform-react-remove-prop-types": "^0.4.14",
6880
"chalk": "^2.4.1",
6981
"chance": "1.0.10",
@@ -76,6 +88,7 @@
7688
"enzyme-adapter-react-16": "^1.6.0",
7789
"enzyme-adapter-utils": "^1.8.1",
7890
"enzyme-to-json": "^3.3.4",
91+
"execa": "^1.0.0",
7992
"expect.js": "0.3.1",
8093
"fancy-log": "^1.3.2",
8194
"fetch-mock": "7.3.0",
@@ -99,13 +112,17 @@
99112
"pdfjs-dist": "^2.0.943",
100113
"pixelmatch": "4.0.2",
101114
"proxyquire": "1.7.11",
102-
"react-test-renderer": "^16.2.0",
115+
"react-docgen-typescript-loader": "^3.0.0",
116+
"react-docgen-typescript-webpack-plugin": "^1.1.0",
117+
"react-test-renderer": "^16.7.0",
103118
"redux-test-utils": "0.2.2",
104119
"rsync": "0.4.0",
105120
"run-sequence": "^2.2.1",
106121
"sass-loader": "^7.1.0",
122+
"sass-resources-loader": "^2.0.0",
107123
"simple-git": "1.37.0",
108124
"sinon": "^5.0.7",
125+
"string-replace-loader": "^2.1.1",
109126
"supertest": "^3.1.0",
110127
"supertest-as-promised": "^4.0.2",
111128
"tmp": "0.0.31",

x-pack/plugins/canvas/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ public/style/index.css
5454
canvas_plugin/*
5555

5656
# Don't commit the Webpack statistics
57-
webpack_stats.json
57+
webpack_stats.json
58+
59+
# Don't commit storybook builds
60+
storybook
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"test": {
4+
"plugins": [
5+
"require-context-hook"
6+
]
7+
}
8+
}
9+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { compose, withState } from 'recompose';
8-
import { AdvancedFilter as Component } from './advanced_filter';
9-
10-
export const AdvancedFilter = compose(withState('value', 'onChange', ({ filter }) => filter || ''))(
11-
Component
12-
);
7+
import '@storybook/addon-options/register';
8+
import '@storybook/addon-actions/register';
9+
import '@storybook/addon-knobs/register';
10+
import '@storybook/addon-console';
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { configure, addDecorator } from '@storybook/react';
8+
import { withKnobs } from '@storybook/addon-knobs/react';
9+
import { withInfo } from '@storybook/addon-info';
10+
import { withOptions } from '@storybook/addon-options';
11+
12+
// Import dependent CSS
13+
require('@elastic/eui/dist/eui_theme_light.css');
14+
require('@kbn/ui-framework/dist/kui_light.css');
15+
require('../../../../src/legacy/ui/public/styles/bootstrap_light.less');
16+
17+
// If we're running Storyshots, be sure to register the require context hook.
18+
// Otherwise, add the other decorators.
19+
if (process.env.NODE_ENV === 'test') {
20+
require('babel-plugin-require-context-hook/register')();
21+
} else {
22+
// Customize the info for each story.
23+
addDecorator(
24+
withInfo({
25+
inline: true,
26+
styles: {
27+
infoBody: {
28+
margin: 20
29+
},
30+
infoStory: {
31+
margin: '40px 60px'
32+
}
33+
}
34+
})
35+
);
36+
37+
// Add optional knobs to customize each story.
38+
addDecorator(withKnobs);
39+
}
40+
41+
function loadStories() {
42+
// Pull in the built CSS produced by the Kibana server
43+
const css = require.context('../../../../built_assets/css', true, /light.css$/);
44+
css.keys().forEach(filename => css(filename));
45+
46+
// Include the legacy styles
47+
const uiStyles = require.context('../../../../src/legacy/ui/public/styles', false, /[\/\\](?!mixins|variables|_|\.|bootstrap_(light|dark))[^\/\\]+\.less/);
48+
uiStyles.keys().forEach(key => uiStyles(key));
49+
50+
// Find all files ending in *.examples.ts
51+
const req = require.context('./..', true, /.examples.tsx$/);
52+
req.keys().forEach(filename => req(filename));
53+
}
54+
55+
// Set up the Storybook environment with custom settings.
56+
addDecorator(
57+
withOptions({
58+
goFullScreen: false,
59+
name: 'Canvas Storybook',
60+
showAddonsPanel: true,
61+
url: 'https://github.com/elastic/kibana/tree/master/x-pack/plugins/canvas'
62+
})
63+
);
64+
65+
configure(loadStories, module);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
const serve = require('serve-static');
8+
const path = require('path');
9+
10+
// Extend the Storybook Middleware to include a route to access ui assets
11+
module.exports = function (router) {
12+
router.get('/ui', serve(path.resolve(__dirname, '../../../../src/legacy/ui/public/assets')));
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import path from 'path';
8+
import initStoryshots, { multiSnapshotWithOptions } from '@storybook/addon-storyshots';
9+
import styleSheetSerializer from 'jest-styled-components/src/styleSheetSerializer';
10+
import { addSerializer } from 'jest-specific-snapshot'
11+
12+
jest.mock(`@elastic/eui/lib/components/form/form_row/make_id`, () => () => `generated-id`);
13+
14+
addSerializer(styleSheetSerializer);
15+
initStoryshots({
16+
configPath: path.resolve(__dirname, './../.storybook'),
17+
test: multiSnapshotWithOptions({}),
18+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
const path = require('path');
8+
const TSDocgenPlugin = require('react-docgen-typescript-webpack-plugin');
9+
10+
// Extend the Storybook Webpack config with some customizations;
11+
module.exports = (_baseConfig, _env, config) => {
12+
13+
// Include the React preset for Storybook JS files.
14+
config.module.rules.push(
15+
{
16+
test: /\.js$/,
17+
include: /\.storybook/,
18+
loaders: 'babel-loader',
19+
options: {
20+
presets: [require.resolve('babel-preset-react')],
21+
},
22+
},
23+
);
24+
25+
// Find and alter the CSS rule to replace the Kibana public path string with a path
26+
// to the route we've added in middleware.js
27+
const cssRule = config.module.rules.find(rule => rule.test.source.includes('.css$'));
28+
cssRule.use.push({
29+
loader: 'string-replace-loader',
30+
options: {
31+
search: '__REPLACE_WITH_PUBLIC_PATH__',
32+
replace: '/',
33+
flags: 'g'
34+
}
35+
});
36+
37+
// Configure loading LESS files from Kibana
38+
config.module.rules.push({
39+
test: /\.less$/,
40+
use: [
41+
{ loader: 'style-loader' },
42+
{ loader: 'css-loader', options: { importLoaders: 2 } },
43+
{
44+
loader: 'postcss-loader',
45+
options: {
46+
config: { path: path.resolve(__dirname, './../../../../src/optimize/postcss.config.js') },
47+
},
48+
},
49+
{ loader: 'less-loader' },
50+
],
51+
});
52+
53+
// Support .ts/x files using the tsconfig from Kibana
54+
config.module.rules.push({
55+
test: /\.tsx?$/,
56+
use: [
57+
{
58+
loader: 'ts-loader',
59+
options: {
60+
transpileOnly: true,
61+
experimentalWatchApi: true,
62+
onlyCompileBundledFiles: true,
63+
configFile: require.resolve('../../../../tsconfig.json'),
64+
compilerOptions: {
65+
sourceMap: true,
66+
},
67+
},
68+
},
69+
require.resolve('react-docgen-typescript-loader'),
70+
],
71+
});
72+
73+
// Include the TSDocgen plugin to display Typescript param comments in the stories.
74+
config.plugins.push(new TSDocgenPlugin());
75+
76+
// Tell Webpack about the ts/x extensions
77+
config.resolve.extensions.push('.ts', '.tsx');
78+
79+
// Alias the any imports from ui/ to the proper directory.
80+
config.resolve.alias.ui = path.resolve(__dirname, './../../../../src/legacy/ui/public');
81+
82+
return config;
83+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Storyshots renderers/AdvancedFilter default 1`] = `
4+
<form
5+
className="canvasAdvancedFilter"
6+
onSubmit={[Function]}
7+
>
8+
<div
9+
className="euiFlexGroup euiFlexGroup--gutterExtraSmall euiFlexGroup--directionRow euiFlexGroup--responsive"
10+
>
11+
<div
12+
className="euiFlexItem"
13+
>
14+
<input
15+
className="canvasAdvancedFilter__input"
16+
onChange={[Function]}
17+
placeholder="Enter filter expression"
18+
type="text"
19+
value=""
20+
/>
21+
</div>
22+
<div
23+
className="euiFlexItem euiFlexItem--flexGrowZero"
24+
>
25+
<button
26+
className="canvasAdvancedFilter__button"
27+
type="submit"
28+
>
29+
Apply
30+
</button>
31+
</div>
32+
</div>
33+
</form>
34+
`;
35+
36+
exports[`Storyshots renderers/AdvancedFilter with value 1`] = `
37+
<form
38+
className="canvasAdvancedFilter"
39+
onSubmit={[Function]}
40+
>
41+
<div
42+
className="euiFlexGroup euiFlexGroup--gutterExtraSmall euiFlexGroup--directionRow euiFlexGroup--responsive"
43+
>
44+
<div
45+
className="euiFlexItem"
46+
>
47+
<input
48+
className="canvasAdvancedFilter__input"
49+
onChange={[Function]}
50+
placeholder="Enter filter expression"
51+
type="text"
52+
value="expression"
53+
/>
54+
</div>
55+
<div
56+
className="euiFlexItem euiFlexItem--flexGrowZero"
57+
>
58+
<button
59+
className="canvasAdvancedFilter__button"
60+
type="submit"
61+
>
62+
Apply
63+
</button>
64+
</div>
65+
</div>
66+
</form>
67+
`;

0 commit comments

Comments
 (0)