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

Migrate app-react to typescript #7054

Merged
merged 10 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/knobs/src/KnobStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class KnobStore {

callbacks: Callback[] = [];

timer: number;
timer: NodeJS.Timeout;

has(key: string) {
return this.store[key] !== undefined;
Expand Down
3 changes: 2 additions & 1 deletion addons/options/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src"
"rootDir": "./src",
"types": ["webpack-env"]
},
"include": [
"src/**/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
onClick={[Function]}
>
<button
onClick={[Function]}
style={
Object {
"backgroundColor": "transparent",
Expand Down Expand Up @@ -273,7 +272,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
"textDecoration": "none",
}
}
target="_blank"
>
Writing Stories
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
<p>
See these sample
<button
onClick={[Function]}
style={
Object {
"backgroundColor": "transparent",
Expand Down Expand Up @@ -234,7 +233,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
"textDecoration": "none",
}
}
target="_blank"
>
Writing Stories
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
<p>
See these sample
<button
onClick={[Function]}
style={
Object {
"backgroundColor": "transparent",
Expand Down Expand Up @@ -138,7 +137,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
"textDecoration": "none",
}
}
target="_blank"
>
Writing Stories
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
<p>
See these sample
<button
onClick={[Function]}
style={
Object {
"backgroundColor": "transparent",
Expand Down Expand Up @@ -138,7 +137,6 @@ exports[`Storyshots Welcome to Storybook 1`] = `
"textDecoration": "none",
}
}
target="_blank"
>
Writing Stories
</a>
Expand Down
2 changes: 2 additions & 0 deletions addons/storyshots/storyshots-core/stories/storyshot.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import initStoryshots, { multiSnapshotWithOptions } from '../src';

jest.mock('@storybook/node-logger');

// with react-test-renderer
initStoryshots({
framework: 'react',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { constructUrl } from '../url';

jest.mock('@storybook/node-logger');

describe('Construct URL for Storyshots', () => {
it('can use a url without path and without query params', () => {
expect(constructUrl('http://localhost:9001', 'someKind', 'someStory')).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock('fs', () => require('../../../../__mocks__/fs'));
jest.mock('path', () => ({
resolve: () => 'tsconfig.json',
}));
jest.mock('@storybook/node-logger');

const setupFiles = files => {
// eslint-disable-next-line no-underscore-dangle, global-require
Expand Down
1 change: 1 addition & 0 deletions app/angular/src/server/ts_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jest.mock('fs', () => require('../../../../__mocks__/fs'));
jest.mock('path', () => ({
resolve: () => 'tsconfig.json',
}));
jest.mock('@storybook/node-logger');

const setupFiles = files => {
// eslint-disable-next-line no-underscore-dangle, global-require
Expand Down
9 changes: 8 additions & 1 deletion app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"license": "MIT",
"main": "dist/client/index.js",
"jsnext:main": "src/client/index.js",
"types": "dist/public_api.d.ts",
"bin": {
"build-storybook": "./bin/build.js",
"start-storybook": "./bin/index.js",
Expand All @@ -29,6 +29,7 @@
"@babel/plugin-transform-react-constant-elements": "^7.2.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@storybook/addons": "5.2.0-alpha.38",
"@storybook/core": "5.2.0-alpha.38",
"@storybook/node-logger": "5.2.0-alpha.38",
"@svgr/webpack": "^4.0.3",
Expand All @@ -49,9 +50,15 @@
},
"peerDependencies": {
"babel-loader": "^7.0.0 || ^8.0.0",
"@babel/core": "^7.0.1",
"react": "*",
"react-dom": "*"
},
"devDependencies": {
"@types/mini-css-extract-plugin": "^0.2.1",
"@types/node": "^12.0.8",
"@types/webpack": "^4.4.32"
},
"engines": {
"node": ">=8.0.0"
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ describe('element_check.isReactRenderable', () => {
const number = 1337;
const element = <span>what's up</span>;
const array = [string, number, element];
const object = { key: null };
const object = { key: null } as any;

it('allows rendering React elements only prior to React Fiber', () => {
// mutate version for the purpose of the test
// @ts-ignore
React.version = '15.5.4';

expect(isReactRenderable(string)).toBe(false);
Expand All @@ -75,6 +76,7 @@ describe('element_check.isReactRenderable', () => {

it('allows rendering string, numbers, arrays and React elements with React Fiber', () => {
// mutate version for the purpose of the test
// @ts-ignore
React.version = '16.0.0-alpha.13';

expect(isReactRenderable(string)).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import React from 'react';
import flattenDeep from 'lodash/flattenDeep';

// return true if the element is renderable with react fiber
export const isValidFiberElement = element =>
export const isValidFiberElement = (element: React.ReactElement) =>
typeof element === 'string' || typeof element === 'number' || React.isValidElement(element);

export const isPriorToFiber = version => {
export const isPriorToFiber = (version: string) => {
const [majorVersion] = version.split('.');

return Number(majorVersion) < 16;
};

// accepts an element and return true if renderable else return false
const isReactRenderable = element => {
const isReactRenderable = (element: React.ReactElement): boolean => {
// storybook is running with a version prior to fiber,
// run a simple check on the element
if (isPriorToFiber(React.version)) {
Expand Down
22 changes: 0 additions & 22 deletions app/react/src/client/preview/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('preview', () => {
jest.resetModules();
});

const isFunction = value => typeof value === 'function';
const isFunction = (value: unknown) => typeof value === 'function';

it('should return the client api in a browser environment', () => {
const api = require('.');
Expand All @@ -13,10 +13,14 @@ describe('preview', () => {
});

it('should return the client api in a node.js environment', () => {
jest.mock('global', () => ({
document: undefined,
window: undefined,
}));
jest.mock(
'global',
() =>
({
document: undefined,
window: undefined,
} as any)
);
const api = require('.');
expect(Object.keys(api).length).toBeGreaterThan(0);
expect(Object.values(api).every(isFunction)).toEqual(true);
Expand Down
37 changes: 37 additions & 0 deletions app/react/src/client/preview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable prefer-destructuring */
import { start } from '@storybook/core/client';
import { ClientStoryApi } from '@storybook/addons';

import './globals';
import render from './render';
import { IStorybookSection, StoryFnReactReturnType } from './types';

const framework = 'react';

interface ClientApi extends ClientStoryApi<StoryFnReactReturnType> {
setAddon(addon: any): void;
configure(loaders: () => void, module: NodeModule): void;
getStorybook(): IStorybookSection[];
clearDecorators(): void;
forceReRender(): void;
raw: () => any; // todo add type
load: (...args: any[]) => void;
}

const api = start(render);

export const storiesOf: ClientApi['storiesOf'] = (kind, m) => {
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({
framework,
});
};

export const load: ClientApi['load'] = (...args) => api.load(...args, framework);
export const addDecorator: ClientApi['addDecorator'] = api.clientApi.addDecorator;
export const addParameters: ClientApi['addParameters'] = api.clientApi.addParameters;
export const clearDecorators: ClientApi['clearDecorators'] = api.clientApi.clearDecorators;
export const setAddon: ClientApi['setAddon'] = api.clientApi.setAddon;
export const configure: ClientApi['configure'] = api.configApi.configure;
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender;
export const getStorybook: ClientApi['getStorybook'] = api.clientApi.getStorybook;
export const raw: ClientApi['raw'] = api.clientApi.raw;
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { stripIndents } from 'common-tags';
import isReactRenderable from './element_check';
import { RenderMainArgs } from './types';

const rootEl = document ? document.getElementById('root') : null;

function render(node, el) {
function render(node: React.ReactElement, el: Element) {
ReactDOM.render(
process.env.STORYBOOK_EXAMPLE_APP ? <React.StrictMode>{node}</React.StrictMode> : node,
el
Expand All @@ -20,7 +21,7 @@ export default function renderMain({
showMain,
showError,
forceRender,
}) {
}: RenderMainArgs) {
const element = storyFn();

if (!element) {
Expand Down
31 changes: 31 additions & 0 deletions app/react/src/client/preview/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

export interface ShowErrorArgs {
title: string;
description: string;
}

export interface RenderMainArgs {
storyFn: () => React.ReactElement | undefined;
selectedKind: string;
selectedStory: string;
showMain: () => void;
showError: (args: ShowErrorArgs) => void;
forceRender: boolean;
}

export type StoryFnReactReturnType = React.ReactElement<unknown>;

export interface ICollection {
[p: string]: any;
}

export interface IStorybookStory {
name: string;
render: () => any;
}

export interface IStorybookSection {
kind: string;
stories: IStorybookStory[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const styles = {
margin: 10,
};

const Button = ({ children, onClick }) => (
const Button = ({
children,
onClick,
}: {
children: React.ReactChildren;
onClick: (event: React.MouseEvent<HTMLElement>) => void;
}) => (
<button onClick={onClick} style={styles} type="button">
{children}
</button>
Expand Down
Loading