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 support for Storybook #742

Merged
merged 37 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f3c4aa3
Add storybook.
peterp Jun 16, 2020
134bbfb
Add configuration to start Storybook.
peterp Jun 23, 2020
430d687
Add Storybook command.
peterp Jun 23, 2020
61b1cbb
Remove logging messages.
peterp Jun 23, 2020
bdaef74
Clean up Storybook command.
peterp Jun 23, 2020
21228b5
Support TS extensions.
peterp Jun 23, 2020
3329a97
Load users webpack configuration.
peterp Jun 25, 2020
8ae6ed2
Merge remote-tracking branch 'origin/main' into pp-add-storybook
peterp Jun 26, 2020
98e7367
Add .eslintignore file.
peterp Jun 26, 2020
bbd8843
Also watch template files.
peterp Jun 26, 2020
c3c2b8a
Convert testing to typescript.
peterp Jun 26, 2020
8b2d549
Do not allow JS in testing.
peterp Jun 26, 2020
4bff933
Export MockProviders from testing.
peterp Jun 26, 2020
cef3ff3
Disable no-var-requires rule.
peterp Jun 26, 2020
c2b6765
Use the Providers from testing.
peterp Jun 26, 2020
b28d4b2
Add __REDWOOD__ global.
peterp Jun 26, 2020
f13e8ad
Add main export.
peterp Jun 26, 2020
dd303f0
Remove jest, since we share with storybook.
peterp Jun 26, 2020
b475e72
Make jest and storybook share Providers.
peterp Jun 26, 2020
34fa2cb
Fix this. We need types for these.
peterp Jun 26, 2020
4dcf936
Build tsx files.
peterp Jun 26, 2020
692c0d8
Clean up preview.
peterp Jun 26, 2020
890e5d8
Generate component stories.
peterp Jun 26, 2020
ab6cf67
Fix Cell story tests.
peterp Jun 26, 2020
e201cfb
Upgrade new-project app.
peterp Jun 26, 2020
13f44a5
Add Cell Storybook.
peterp Jun 27, 2020
dfd947c
Test Cell Storybook generation.
peterp Jun 27, 2020
9edc88e
Nest components in Storybook.
peterp Jun 27, 2020
af1d2d4
Make the page template link to itself.
peterp Jun 27, 2020
a70bcd5
Generate Storybook for pages.
peterp Jun 27, 2020
09ce23f
Do not bail storybook.
peterp Jun 27, 2020
bcaf7bb
Fix Cell test wording.
peterp Jun 27, 2020
5cfc19d
Fix hot reloading in storybook.
peterp Jun 29, 2020
94249b6
Add Storybook to Layout generator.
peterp Jun 29, 2020
83be4de
Append "Page" to page stories.
peterp Jun 29, 2020
ef525f7
Update packages/testing/src/MockProviders.tsx
peterp Jun 29, 2020
e269e1b
Merge branch 'main' into pp-add-storybook
peterp Jun 29, 2020
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
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
packages/api/importAll.macro.js
packages/core/src/__tests__/__fixtures__/**/*
packages/core/config/storybook/**/*
2 changes: 1 addition & 1 deletion __fixtures__/new-project/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"@redwoodjs/api": "^0.11.0"
"@redwoodjs/api": "^0.12.0"
}
}
2 changes: 1 addition & 1 deletion __fixtures__/new-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
]
},
"devDependencies": {
"@redwoodjs/core": "^0.11.0",
"@redwoodjs/core": "^0.12.0",
"netlify-plugin-prisma-provider": "^0.3.0"
},
"eslintConfig": {
Expand Down
4 changes: 2 additions & 2 deletions __fixtures__/new-project/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"defaults"
],
"dependencies": {
"@redwoodjs/router": "^0.11.0",
"@redwoodjs/web": "^0.11.0",
"@redwoodjs/router": "^0.12.0",
"@redwoodjs/web": "^0.12.0",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1"
Expand Down
478 changes: 154 additions & 324 deletions __fixtures__/new-project/yarn.lock

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
"fixtures"
]
},
"eslintIgnore": [
"dist",
"packages/api/importAll.macro.js",
"packages/core/src/__tests__/__fixtures__"
],
"scripts": {
"build": "yarn build:clean && yarn build:js",
"build:clean": "lerna run build:clean",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"build:js": "yarn cross-env NODE_ENV=production yarn babel src -d dist --extensions \".js,.ts\" --delete-dir-on-start --copy-files --no-copy-ignored",
"build:clean-dist": "yarn rimraf 'dist/**/__tests__'",
"prepublishOnly": "yarn build",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
"build:watch": "nodemon --watch src --ext \"js,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
"test": "jest",
"test:watch": "yarn test --watch"
},
Expand Down
14 changes: 10 additions & 4 deletions packages/cli/src/commands/generate/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import {} from 'src/lib/test'

import * as helpers from '../helpers'

const PAGE_TEMPLATE_OUTPUT = `const FooBarPage = () => {
const PAGE_TEMPLATE_OUTPUT = `import { Link } from '@redwoodjs/router'

const FooBarPage = () => {
return (
<div>
<>
<h1>FooBarPage</h1>
<p>Find me in ./web/src/pages/FooBarPage/FooBarPage.js</p>
</div>
<p>Find me in "./web/src/pages/FooBarPage/FooBarPage.js"</p>
<p>
My default route is named "fooBar", link to me with \`
<Link to="fooBar">routes.fooBar()</Link>\`
</p>
</>
)
}

Expand Down
24 changes: 22 additions & 2 deletions packages/cli/src/commands/generate/cell/__tests__/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ beforeAll(() => {
multiWordFiles = cell.files({ name: 'UserProfile' })
})

test('returns exactly 2 files', () => {
expect(Object.keys(singleWordFiles).length).toEqual(2)
test('returns exactly 3 files', () => {
expect(Object.keys(singleWordFiles).length).toEqual(3)
})

test('creates a cell component with a single word name', () => {
Expand All @@ -34,6 +34,16 @@ test('creates a cell test with a single word name', () => {
).toEqual(loadGeneratorFixture('cell', 'singleWordCell.test.js'))
})

test('creates a cell stories with a single word name', () => {
expect(
singleWordFiles[
path.normalize(
'/path/to/project/web/src/components/UserCell/UserCell.stories.js'
)
]
).toEqual(loadGeneratorFixture('cell', 'singleWordCell.stories.js'))
})

test('creates a cell component with a multi word name', () => {
expect(
multiWordFiles[
Expand All @@ -53,3 +63,13 @@ test('creates a cell test with a multi word name', () => {
]
).toEqual(loadGeneratorFixture('cell', 'multiWordCell.test.js'))
})

test('creates a cell stories with a multi word name', () => {
expect(
multiWordFiles[
path.normalize(
'/path/to/project/web/src/components/UserProfileCell/UserProfileCell.stories.js'
)
]
).toEqual(loadGeneratorFixture('cell', 'multiWordCell.stories.js'))
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Loading, Empty, Failure, Success } from './UserProfileCell'

export const loading = () => {
return Loading ? <Loading /> : null
}

export const empty = () => {
return Empty ? <Empty /> : null
}

export const failure = () => {
return Failure ? <Failure error={new Error('Oh no')} /> : null
}

export const success = () => {
return Success ? <Success userProfile={{ mock: 'data' }} /> : null
}

export default { title: 'Cells/UserProfileCell' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Loading, Empty, Failure, Success } from './UserCell'

export const loading = () => {
return Loading ? <Loading /> : null
}

export const empty = () => {
return Empty ? <Empty /> : null
}

export const failure = () => {
return Failure ? <Failure error={new Error('Oh no')} /> : null
}

export const success = () => {
return Success ? <Success user={{ mock: 'data' }} /> : null
}

export default { title: 'Cells/UserCell' }
23 changes: 17 additions & 6 deletions packages/cli/src/commands/generate/cell/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@ export const files = ({ name }) => {
generator: 'cell',
templatePath: 'test.js.template',
})
const storiesFile = templateForComponentFile({
name,
suffix: COMPONENT_SUFFIX,
extension: '.stories.js',
webPathSection: REDWOOD_WEB_PATH_NAME,
generator: 'cell',
templatePath: 'stories.js.template',
})

// Returns
// {
// "path/to/fileA": "<<<template>>>",
// "path/to/fileB": "<<<template>>>",
// }
return [cellFile, testFile].reduce((acc, [outputPath, content]) => {
return {
[outputPath]: content,
...acc,
}
}, {})
return [cellFile, testFile, storiesFile].reduce(
(acc, [outputPath, content]) => {
return {
[outputPath]: content,
...acc,
}
},
{}
)
}

export const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Loading, Empty, Failure, Success } from './${pascalName}Cell'

export const loading = () => {
return Loading ? <Loading /> : null
}

export const empty = () => {
return Empty ? <Empty /> : null
}

export const failure = () => {
return Failure ? <Failure error={new Error("Oh no")} /> : null
}

export const success = () => {
return Success ? <Success ${camelName}={{ mock: 'data' }} /> : null
}

export default { title: 'Cells/${pascalName}Cell' }
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ beforeAll(() => {
})
})

test('returns exactly 2 files', () => {
expect(Object.keys(singleWordDefaultFiles).length).toEqual(2)
test('returns exactly 3 files', () => {
expect(Object.keys(singleWordDefaultFiles).length).toEqual(3)
})

test('creates a single word component', () => {
Expand All @@ -44,6 +44,18 @@ test('creates a single word component test', () => {
).toEqual(loadGeneratorFixture('component', 'singleWordComponent.test.tsx'))
})

test('creates a single word component story', () => {
expect(
singleWordDefaultFiles[
path.normalize(
'/path/to/project/web/src/components/User/User.stories.tsx'
)
]
).toEqual(
loadGeneratorFixture('component', 'singleWordComponent.stories.tsx')
)
})

test('creates a multi word component', () => {
expect(
multiWordDefaultFiles[
Expand All @@ -64,6 +76,16 @@ test('creates a multi word component test', () => {
).toEqual(loadGeneratorFixture('component', 'multiWordComponent.test.tsx'))
})

test('creates a multi word component story', () => {
expect(
multiWordDefaultFiles[
path.normalize(
'/path/to/project/web/src/components/UserProfile/UserProfile.stories.tsx'
)
]
).toEqual(loadGeneratorFixture('component', 'multiWordComponent.stories.tsx'))
})

test('creates JS component files if javacript = true', () => {
expect(
javascriptFiles[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import UserProfile from './UserProfile'

export const generated = () => {
return <UserProfile />
}

export default { title: 'Components/UserProfile' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import User from './User'

export const generated = () => {
return <User />
}

export default { title: 'Components/User' }
28 changes: 19 additions & 9 deletions packages/cli/src/commands/generate/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,32 @@ export const files = ({ name, ...options }) => {
generator: 'component',
templatePath: 'test.tsx.template',
})
const storiesFile = templateForComponentFile({
name,
extension: `.stories.${isJavascript ? 'js' : 'tsx'}`,
webPathSection: REDWOOD_WEB_PATH_NAME,
generator: 'component',
templatePath: 'stories.tsx.template',
})

// Returns
// {
// "path/to/fileA": "<<<template>>>",
// "path/to/fileB": "<<<template>>>",
// }
return [componentFile, testFile].reduce((acc, [outputPath, content]) => {
const template = isJavascript
? transformTSToJS(outputPath, content)
: content
return [componentFile, testFile, storiesFile].reduce(
(acc, [outputPath, content]) => {
const template = isJavascript
? transformTSToJS(outputPath, content)
: content

return {
[outputPath]: template,
...acc,
}
}, {})
return {
[outputPath]: template,
...acc,
}
},
{}
)
}

export const description = 'Generate a component'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ${pascalName} from './${pascalName}'

export const generated = () => {
return <${pascalName} />
}

export default { title: 'Components/${pascalName}' }
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Link } from '@redwoodjs/router'

const ContactUsPage = () => {
return (
<div>
<>
<h1>ContactUsPage</h1>
<p>Find me in ./web/src/pages/ContactUsPage/ContactUsPage.js</p>
</div>
<p>Find me in "./web/src/pages/ContactUsPage/ContactUsPage.js"</p>
<p>
My default route is named "contactUs", link to me with `
<Link to="contactUs">routes.contactUs()</Link>`
</p>
</>
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ContactUsPage from './ContactUsPage'

export const generated = () => {
return <ContactUsPage />
}

export default { title: 'Pages/ContactUs' }
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Link } from '@redwoodjs/router'

const CatsPage = () => {
return (
<div>
<>
<h1>CatsPage</h1>
<p>Find me in ./web/src/pages/CatsPage/CatsPage.js</p>
</div>
<p>Find me in "./web/src/pages/CatsPage/CatsPage.js"</p>
<p>
My default route is named "cats", link to me with `
<Link to="cats">routes.cats()</Link>`
</p>
</>
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Link } from '@redwoodjs/router'

const HomePage = () => {
return (
<div>
<>
<h1>HomePage</h1>
<p>Find me in ./web/src/pages/HomePage/HomePage.js</p>
</div>
<p>Find me in "./web/src/pages/HomePage/HomePage.js"</p>
<p>
My default route is named "home", link to me with `
<Link to="home">routes.home()</Link>`
</p>
</>
)
}

Expand Down
Loading