Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit c4c3dda

Browse files
authored
Merge pull request #102 from skellock/storyshots
Storyshots support
2 parents 70d010d + 6565d08 commit c4c3dda

File tree

23 files changed

+718
-45
lines changed

23 files changed

+718
-45
lines changed

.storybook/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configure } from '@storybook/react'
1+
const { configure } = require('@storybook/react')
22

33
const req = require.context('../src', true, /story\.tsx?$/)
44

.storybook/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = (config, env) => {
1010
exclude: /node_modules/,
1111
include: path.resolve(__dirname, '..', 'src'),
1212
})
13-
13+
1414
myConfig.resolve.extensions.unshift('.tsx')
1515
myConfig.resolve.extensions.unshift('.ts')
1616

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You have to bring your own awesome. But here's a picture* after `npm i` and `np
3939
* lean production bundles
4040
* integrated storybook support
4141
* unit tests with mocking
42-
* 100% code coverage with examples on how to keep it there
42+
* storybook snapshot testing
4343
* code linting & formatting
4444

4545
### Documentation & Samples 🖨

docs/stack.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Start your app only when your gut says, "You'll fuck this up long before your st
8989

9090
Provides a sandbox to work on your components with whatever props you can dream of wanting set to make sure your components are in tip top shape before and during use in your application. If you are writing a component you should be writing stories about it.
9191

92+
Also the storyshots addon for working with `jest` is a great way to add snapshot testing for your React components by using your stories. (Like we needed another reason to use storybook!!)
93+
9294
## Animations
9395

9496
> **react-transition-group**

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
"react-transition-group": "^2.2.1"
8080
},
8181
"devDependencies": {
82+
"@storybook/addon-actions": "^3.2.15",
83+
"@storybook/addon-links": "^3.2.15",
84+
"@storybook/addon-storyshots": "^3.2.15",
8285
"@storybook/react": "^3.2.15",
8386
"@types/electron-is-dev": "^0.3.0",
8487
"@types/electron-store": "^1.2.0",
@@ -99,6 +102,7 @@
99102
"lint-staged": "^5.0.0",
100103
"npm-run-all": "^4.1.2",
101104
"prettier": "^1.8.2",
105+
"react-powerplug": "^0.1.2",
102106
"react-test-renderer": "^16.0.0",
103107
"ts-jest": "^21.2.1",
104108
"ts-loader": "^3.1.1",
@@ -113,19 +117,25 @@
113117
"transform": {
114118
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
115119
},
120+
"moduleNameMapper": {
121+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/mock-file.ts",
122+
"\\.(css|less)$": "<rootDir>/test/mock-style.ts"
123+
},
116124
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
117125
"moduleFileExtensions": [
118126
"ts",
119127
"tsx",
120-
"js"
128+
"js",
129+
"json"
121130
],
122131
"coveragePathIgnorePatterns": [
123-
"/node_modules/",
124-
"/test/",
125-
"/out/",
126-
"/build/",
127-
"/dist/",
128-
"/docs/"
132+
"./node_modules",
133+
"./out",
134+
"./build",
135+
"./dist",
136+
"./test",
137+
"./docs",
138+
"\\.story.tsx$"
129139
],
130140
"coverageThreshold": {
131141
"global": {

src/renderer/features/example-using-tabs/welcome-screen/sample-tabs.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export interface SampleTabsProps {
1919
onChangeTab: (tab: SampleTabType) => void
2020
}
2121

22-
const KEY_1 = `${commandOrControlKey}+1`
23-
const KEY_2 = `${commandOrControlKey}+2`
24-
const KEY_3 = `${commandOrControlKey}+3`
22+
const KEY_1 = `${commandOrControlKey()}+1`
23+
const KEY_2 = `${commandOrControlKey()}+2`
24+
const KEY_3 = `${commandOrControlKey()}+3`
2525

2626
// an extra layer just for the drag style due to electron bug
2727
const ROOT = css(styles.windowDrag)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react'
2+
import { StorybookStory as Story, StorybookGroup as Group } from '../storybook'
3+
import { storiesOf } from '@storybook/react'
4+
import { CenteredContent } from './index'
5+
6+
storiesOf('CenteredContent', module).add('default', () => (
7+
<Story>
8+
<Group title='default'>
9+
<CenteredContent>
10+
<p>i am in the middle</p>
11+
<p>i am also a really strange component and shouldn't exist.</p>
12+
</CenteredContent>
13+
</Group>
14+
</Story>
15+
))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react'
2+
import { StorybookStory as Story, StorybookGroup as Group } from '../storybook'
3+
4+
import { storiesOf } from '@storybook/react'
5+
import { EnterAnimation } from './index'
6+
// import { Value } from 'react-powerplug'
7+
8+
storiesOf('EnterAnimation', module).add('default', () => (
9+
<Story>
10+
<Group title='grow'>
11+
<EnterAnimation animation='grow'>
12+
<p>hi</p>
13+
</EnterAnimation>
14+
</Group>
15+
<Group title='slide'>
16+
<EnterAnimation animation='slide'>
17+
<p>hi</p>
18+
</EnterAnimation>
19+
</Group>
20+
</Story>
21+
))

src/renderer/platform/components/enter-animation/enter-animation.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const FINISH = cssProps({ transform: `translate(0, 0) scale(1, 1)` })
2828
export class EnterAnimation extends React.Component<EnterAnimationProps, EnterAnimationState> {
2929
state: EnterAnimationState = {}
3030

31+
// istanbul ignore next
3132
componentDidMount() {
3233
setTimeout(() => this.setState({ mounted: true }), 1)
3334
}
@@ -55,6 +56,7 @@ export class EnterAnimation extends React.Component<EnterAnimationProps, EnterAn
5556

5657
// style props
5758
const starting = css(cssProps({ transform, transition }))
59+
// istanbul ignore next
5860
const finishing = css(mounted && FINISH)
5961

6062
return (
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as React from 'react'
2+
import { StorybookStory as Story, StorybookGroup as Group } from '../storybook'
3+
import { storiesOf } from '@storybook/react'
4+
import { ScrollableContent } from './index'
5+
6+
storiesOf('ScrollableContent', module).add('default', () => (
7+
<Story>
8+
<Group title='default'>
9+
<ScrollableContent style={{ height: 100 }}>
10+
<p>hi</p>
11+
<p>hi</p>
12+
<p>hi</p>
13+
<p>hi</p>
14+
<p>hi</p>
15+
<p>hi</p>
16+
<p>hi</p>
17+
<p>hi</p>
18+
<p>hi</p>
19+
<p>hi</p>
20+
<p>hi</p>
21+
<p>hi</p>
22+
</ScrollableContent>
23+
</Group>
24+
</Story>
25+
))

src/renderer/platform/components/spin-animation/spin-animation-state.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ test('next', () => {
1010

1111
test('createSpinStates', () => {
1212
const states = createSpinStates({})
13+
expect(typeof states.forward).toBe('function')
1314
expect(typeof states.back).toBe('function')
1415

16+
const forwardResults = states.forward({ value: { get: () => 1 } })
17+
expect(forwardResults.current).toBe(1)
18+
19+
const backResults = states.back({ value: { get: () => 1 } })
20+
expect(backResults.current).toBe(1)
21+
1522
const value: any = () => {}
1623
value.get = () => 1
1724
value.previousState = 'back'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react'
2+
import { StorybookStory as Story, StorybookGroup as Group } from '../storybook'
3+
4+
import { storiesOf } from '@storybook/react'
5+
import { Tab } from './index'
6+
import { Value } from 'react-powerplug'
7+
8+
storiesOf('Tab', module).add('Tab', () => (
9+
<Story>
10+
<Group title='inactive'>
11+
<Value initial='three'>
12+
{({ value, setValue }) => (
13+
<div style={{ flexDirection: 'row', display: 'flex' }}>
14+
<Tab text='One' active={value === 'one'} onClick={() => setValue('one')} />
15+
<Tab text='Two' active={value === 'two'} onClick={() => setValue('two')} />
16+
<Tab text='Three' active={value === 'three'} onClick={() => setValue('three')} />
17+
<Tab text='Four' active={value === 'four'} onClick={() => setValue('four')} />
18+
</div>
19+
)}
20+
</Value>
21+
</Group>
22+
</Story>
23+
))

src/renderer/platform/components/text/text.story.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import { storiesOf } from '@storybook/react'
66
import { Text } from './index'
77

88
storiesOf('Text', module)
9-
.add('text styles', () =>
9+
.add('text styles', () => (
1010
<Story>
1111
<Group title='regular text'>
1212
<Text>Hello World!</Text>
1313
<Text>$123.45</Text>
1414
<Text>The quick brown fox jumped over the slow lazy dog.</Text>
1515
</Group>
16+
<Group title='with text property'>
17+
<Text text='Passed in text property' />
18+
</Group>
19+
1620
<Group title='huge paragraph'>
1721
<Text>
1822
Wayfarers intelligentsia salvia sartorial keffiyeh locavore direct trade flexitarian
@@ -45,9 +49,9 @@ storiesOf('Text', module)
4549
<Group title='style={{ color: &quot;red&quot; }}'>
4650
<Text style={{ color: 'red' }}>Hello World!</Text>
4751
</Group>
48-
</Story>,
49-
)
50-
.add('with nested children', () =>
52+
</Story>
53+
))
54+
.add('with nested children', () => (
5155
<Story>
5256
<Group title='with nested children'>
5357
<Text>
@@ -62,5 +66,5 @@ storiesOf('Text', module)
6266
</p>
6367
</Text>
6468
</Group>
65-
</Story>,
66-
)
69+
</Story>
70+
))

src/renderer/platform/utils/keyboard/keyboard.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jest.mock('../../../../shared', () => {
1111
})
1212

1313
import * as Mousetrap from 'mousetrap'
14-
// import * as platform from '../../../../shared/utils/platform/platform'
1514

1615
test('changes tabs', () => {
1716
const fn = () => true
@@ -25,13 +24,13 @@ test('changes tabs', () => {
2524

2625
test('mac is command key', () => {
2726
const { commandOrControlKey } = require('./keyboard')
28-
expect(commandOrControlKey).toBe('command')
27+
expect(commandOrControlKey()).toBe('command')
2928
})
3029

3130
test('non-mac is control', () => {
32-
jest.resetModules()
31+
// jest.resetModules()
3332
const shared = require('../../../../shared')
3433
shared.isMac = jest.fn().mockReturnValue(false)
3534
const { commandOrControlKey } = require('./keyboard')
36-
expect(commandOrControlKey).toBe('ctrl')
35+
expect(commandOrControlKey()).toBe('ctrl')
3736
})

src/renderer/platform/utils/keyboard/keyboard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export type KeyboardAction = 'keypress' | 'keydown' | 'keyup'
1111
// Mousetrap.prototype.stopCallback = () => false
1212
// }
1313

14-
export const commandOrControlKey = isMac() ? 'command' : 'ctrl'
14+
export const commandOrControlKey = () => (isMac() ? 'command' : 'ctrl')
1515

1616
/**
1717
* Binds a keystroke to a function.
18-
*
18+
*
1919
* @param keys The keystroke.
2020
* @param callback The function to fire.
2121
* @param action Optional keyboard event to further constraint.
@@ -30,7 +30,7 @@ export function bindKey(
3030

3131
/**
3232
* Removes a keybind.
33-
*
33+
*
3434
* @param keys The keystroke.
3535
* @param action Optional keyboard event to further constraint.
3636
*/

src/shared/utils/platform/platform.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export function isLinux() {
2-
return process.platform === 'linux'
1+
export function isLinux(platform = process.platform) {
2+
return platform === 'linux'
33
}
44

5-
export function isWindows() {
6-
return process.platform === 'win32'
5+
export function isWindows(platform = process.platform) {
6+
return platform === 'win32'
77
}
88

9-
export function isMac() {
10-
return process.platform === 'darwin'
9+
export function isMac(platform = process.platform) {
10+
return platform === 'darwin'
1111
}

0 commit comments

Comments
 (0)