Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript CRA example #5011

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/cra-ts-kitchen-sink/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SKIP_PREFLIGHT_CHECK=true
NODE_PATH=src
2 changes: 2 additions & 0 deletions examples/cra-ts-kitchen-sink/.storybook/addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@storybook/addon-actions/register';
import '@storybook/addon-options/register';
19 changes: 19 additions & 0 deletions examples/cra-ts-kitchen-sink/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { configure, addDecorator } from '@storybook/react';
import { withOptions } from '@storybook/addon-options';
import { withInfo } from '@storybook/addon-info';

addDecorator(
withOptions({
name: 'CRA Kitchen Sink',
url: 'https://github.com/storybooks/storybook/tree/master/examples/cra-ts-kitchen-sink',
})
);
addDecorator(withInfo());

function loadStories() {
// automatically import all story js files that end with *.stories.tsx
const req = require.context('../src', true, /\.stories\.tsx$/);
req.keys().forEach(filename => req(filename));
}

configure(loadStories, module);
17 changes: 17 additions & 0 deletions examples/cra-ts-kitchen-sink/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = (_baseCnig, _env, config) => ({
...config,
module: {
rules: [
...config.module.rules,
{
test: /\.tsx?$/,
include: path.resolve(__dirname, '../src'),
use: [require.resolve('react-docgen-typescript-loader')],
},
],
},
plugins: [...config.plugins, new ForkTsCheckerWebpackPlugin()],
});
42 changes: 42 additions & 0 deletions examples/cra-ts-kitchen-sink/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "cra-ts-kitchen-sink",
"version": "4.2.0-alpha.4",
"private": true,
"scripts": {
"build:storybook": "build-storybook -s public",
"start": "start-storybook -p 9010 -s public",
"test": "react-scripts test --env=jsdom",
"lint": "tslint src/**/*.ts{,x}"
},
"dependencies": {
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"devDependencies": {
"@storybook/addon-actions": "4.2.0-alpha.4",
"@storybook/addon-info": "4.2.0-alpha.4",
"@storybook/addon-options": "4.2.0-alpha.4",
"@storybook/addons": "4.2.0-alpha.4",
"@storybook/react": "4.2.0-alpha.4",
"@types/enzyme": "^3.1.15",
"@types/react": "^16.7.17",
"@types/react-dom": "^16.0.11",
"@types/storybook__addon-actions": "^3.4.1",
"@types/storybook__addon-info": "^3.4.2",
"@types/storybook__react": "^4.0.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-to-json": "^3.3.5",
"react-docgen-typescript-loader": "^3.0.0",
"react-scripts": "^2.1.0",
"tslint": "^5.11.0",
"tslint-config-airbnb": "^5.11.1",
"fork-ts-checker-webpack-plugin": "^0.5.2",
"typescript": "^3.2.2"
},
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
}
Binary file added examples/cra-ts-kitchen-sink/public/favicon.ico
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/cra-ts-kitchen-sink/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<!--
Notice the use of %PUBLIC_URL% in the tag above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start`.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
12 changes: 12 additions & 0 deletions examples/cra-ts-kitchen-sink/src/components/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import Button from './Button';

storiesOf('Button', module).add(
'simple button',
() => <Button onClick={action('button clicked')}>OK</Button>,
{
info: { inline: true },
},
);
23 changes: 23 additions & 0 deletions examples/cra-ts-kitchen-sink/src/components/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import Button from './Button';
import { shallow } from 'enzyme';

describe('Button', () => {
it('renders', () => {
const wrapper = shallow(<Button>OK</Button>);
expect(wrapper).toMatchInlineSnapshot(`
<button
type="button"
>
OK
</button>
`);
});

it('calls onClick on button click', () => {
const handleClick = jest.fn();
const wrapper = shallow(<Button onClick={handleClick}>OK</Button>);
wrapper.find('button').simulate('click');
expect(handleClick).toBeCalled();
});
});
17 changes: 17 additions & 0 deletions examples/cra-ts-kitchen-sink/src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';

interface Props {
/**
* Click event handler
* @default null
*/
onClick?: () => void;
}

const Button: React.SFC<Props> = ({ children, onClick }) => (
<button type="button" onClick={onClick}>
{children}
</button>
);

export default Button;
1 change: 1 addition & 0 deletions examples/cra-ts-kitchen-sink/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
5 changes: 5 additions & 0 deletions examples/cra-ts-kitchen-sink/src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
21 changes: 21 additions & 0 deletions examples/cra-ts-kitchen-sink/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"lib" : ["esnext", "dom"],
"jsx": "preserve",
"moduleResolution": "node",
"esModuleInterop": true,
"allowJs": true,
"skipLibCheck": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": [
"src"
]
}
6 changes: 6 additions & 0 deletions examples/cra-ts-kitchen-sink/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "tslint-config-airbnb",
"rules": {
"variable-name": [true, "ban-keywords", "check-format", "allow-pascal-case"]
}
}