Skip to content

Commit bddc675

Browse files
feat: Update react-docgen to add typescript support (#20)
feat: move dependency to devDependency docs: Update README
1 parent 887c6e4 commit bddc675

File tree

12 files changed

+9772
-6919
lines changed

12 files changed

+9772
-6919
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"presets": [
3-
"env"
3+
"@babel/preset-env",
4+
"@babel/preset-typescript"
45
]
56
}

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Magically generate fake props for your React tests 🔮
44
5-
`react-fake-props` parses your Component prop types using [react-docgen](https://github.com/reactjs/react-docgen) and generates fake props. Supports [Flow](https://flow.org) and [PropTypes](https://github.com/facebook/prop-types). Works great with [Jest](https://facebook.github.io/jest/) snapshots and [Enzyme](https://github.com/airbnb/enzyme).
5+
`react-fake-props` parses your Component prop types using [react-docgen](https://github.com/reactjs/react-docgen) and generates fake props. Supports [TypeScript](https://www.typescriptlang.org/), [Flow](https://flow.org) and [PropTypes](https://github.com/facebook/prop-types). Works great with [Jest](https://facebook.github.io/jest/) snapshots and [Enzyme](https://github.com/airbnb/enzyme).
66

77
<a href="https://www.patreon.com/typicode">
88
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
@@ -20,7 +20,20 @@ yarn add react-fake-props --dev
2020

2121
## Example
2222

23-
Assuming the following Component with Flow types:
23+
Assuming the following Component with TypeScript:
24+
25+
```jsx
26+
type Props = {
27+
id: number
28+
name: string
29+
}
30+
31+
class Component extends React.Component<Props> {
32+
// ...
33+
}
34+
```
35+
36+
Or Flow types:
2437

2538
```jsx
2639
// @flow
@@ -74,7 +87,7 @@ To include optional props, pass `{ optional: true }`.
7487

7588
Please note:
7689
- `custom` validators and `PropTypes.instanceOf` aren't supported, you'll still need to set them manually.
77-
- `react-fake-props` requires the component path to be passed, instead of the component itself, to be able to support Flow and PropTypes.
90+
- `react-fake-props` requires the component path to be passed, instead of the component itself, to be able to support TypeScript, Flow and PropTypes.
7891

7992
### For multiple components in single file
8093

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`fakeProps should return an object with all props faked (snapshot) 1`] = `
4+
Object {
5+
"getFriend": [Function],
6+
"onNameChange": [Function],
7+
"person": Object {
8+
"address": Object {
9+
"pincode": 1,
10+
"street": "person.address.street",
11+
"street2": "person.address.street2",
12+
},
13+
"age": 1,
14+
"family": Array [
15+
1,
16+
],
17+
"id": 1,
18+
"name": "person.name",
19+
"repos": Array [
20+
Object {
21+
"commits": Array [
22+
Object {
23+
"id": "person.repos.commits.id",
24+
"message": "person.repos.commits.message",
25+
},
26+
],
27+
"demoSite": "person.repos.demoSite",
28+
"npm": "person.repos.npm",
29+
"url": "person.repos.url",
30+
},
31+
],
32+
},
33+
}
34+
`;
35+
36+
exports[`fakeProps should return an object with no props (snapshot) 1`] = `Object {}`;
37+
38+
exports[`fakeProps should return an object with required props faked (snapshot) 1`] = `
39+
Object {
40+
"onNameChange": [Function],
41+
"person": Object {
42+
"address": Object {
43+
"pincode": 1,
44+
"street": "person.address.street",
45+
},
46+
"age": 1,
47+
"id": 1,
48+
"name": "person.name",
49+
"repos": Array [
50+
Object {
51+
"commits": Array [
52+
Object {
53+
"id": "person.repos.commits.id",
54+
"message": "person.repos.commits.message",
55+
},
56+
],
57+
"url": "person.repos.url",
58+
},
59+
],
60+
},
61+
}
62+
`;

__tests__/typescript.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import path from 'path'
2+
import fakeProps from '../src'
3+
4+
const ComponentFile = path.join(
5+
__dirname,
6+
'../fixtures/typescript/Component.tsx'
7+
)
8+
const SimpleFile = path.join(__dirname, '../fixtures/typescript/Simple.tsx')
9+
const NoProps = require.resolve('../fixtures/typescript/NoProps.tsx')
10+
const Multiple = require.resolve('../fixtures/typescript/Multiple.tsx')
11+
12+
describe('fakeProps', () => {
13+
it('should return an object with no props (snapshot)', () => {
14+
expect(fakeProps(NoProps)).toMatchSnapshot()
15+
})
16+
17+
it('should return an object with required props faked (snapshot)', () => {
18+
expect(fakeProps(ComponentFile)).toMatchSnapshot()
19+
})
20+
21+
it('should return an object with all props faked (snapshot)', () => {
22+
expect(fakeProps(ComponentFile, { optional: true })).toMatchSnapshot()
23+
})
24+
25+
it('should return a person on calling getFriend prop function (test function generation)', () => {
26+
const allProps = fakeProps(ComponentFile, { optional: true })
27+
const friend = allProps.getFriend()
28+
29+
expect(friend.name).toBeDefined()
30+
expect(friend.name).toBe('getFriend.name')
31+
expect(friend.age).toBe(1)
32+
})
33+
34+
it('should return more props with optional', () => {
35+
const allProps = fakeProps(ComponentFile, { optional: true })
36+
const requiredProps = fakeProps(ComponentFile)
37+
38+
const allPropsLength = Object.keys(allProps).length
39+
const requiredPropsLength = Object.keys(requiredProps).length
40+
41+
expect(allPropsLength).toBeGreaterThan(requiredPropsLength)
42+
})
43+
44+
it('should return an object with shape props faked', () => {
45+
const props = fakeProps(SimpleFile)
46+
const { requiredString, requiredObject } = props
47+
48+
expect(props).toEqual({
49+
requiredString,
50+
requiredObject: {
51+
requiredString: requiredObject.requiredString
52+
}
53+
})
54+
})
55+
56+
it('should prefix shape props', () => {
57+
const props = fakeProps(SimpleFile)
58+
expect(props.requiredObject.requiredString).toBe(
59+
'requiredObject.requiredString'
60+
)
61+
})
62+
63+
it('should work with multiple components', () => {
64+
const component = fakeProps(Multiple, { all: true }).find(
65+
({ displayName }) => displayName === 'First'
66+
)
67+
expect(component.props.requiredString).toBe('requiredString')
68+
})
69+
})

fixtures/propTypes/Component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Message {}
66

77
class MyComponent extends React.Component {
88
render () {
9-
// ... do things with the props
9+
return null
1010
}
1111
}
1212

fixtures/typescript/Component.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react'
2+
3+
type Address = {
4+
street: string
5+
street2?: string
6+
pincode: number
7+
}
8+
9+
type Commit = {
10+
id: string
11+
message: string
12+
}
13+
14+
type Repo = {
15+
url: string
16+
demoSite?: string
17+
npm?: string
18+
commits: Array<Commit>
19+
}
20+
21+
type Person = {
22+
id: number
23+
name: string
24+
age: number
25+
address: Address
26+
family?: Array<number>
27+
repos: Array<Repo>
28+
}
29+
30+
type Props = {
31+
person: Person,
32+
onNameChange: (id: number, newName: string) => any
33+
getFriend?: (person: Person) => Person
34+
}
35+
36+
function MyComponent(props: Props) {
37+
return <></>
38+
}
39+
40+
export default MyComponent

fixtures/typescript/Multiple.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
3+
function connect(Component) {
4+
return <div><Component /></div>
5+
}
6+
7+
type Props = {
8+
requiredString: string
9+
}
10+
11+
function First(props: Props) {
12+
return <></>
13+
}
14+
15+
export function Second() {
16+
return <div />
17+
}
18+
19+
export default connect(First)

fixtures/typescript/NoProps.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react'
2+
3+
function NoProps() {
4+
return <></>
5+
}
6+
7+
export default NoProps

fixtures/typescript/Simple.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
3+
type Props = {
4+
requiredString: string
5+
optionalString?: string
6+
requiredObject: {
7+
requiredString: string
8+
optionalString?: string
9+
}
10+
}
11+
12+
function Simple(props: Props) {
13+
return <></>
14+
}
15+
16+
export default Simple

0 commit comments

Comments
 (0)