|
1 | 1 | import * as React from 'react'
|
2 | 2 | import { ReactWrapper } from 'enzyme'
|
3 | 3 | import { mountWithProvider } from 'test/utils'
|
4 |
| -import { Props } from '../../../types/utils' |
| 4 | +import { Props, PropsOf } from '../../../types/utils' |
5 | 5 |
|
6 |
| -export type ShorthandTestOptions = { |
7 |
| - mapsValueToProp?: string |
| 6 | +export type ShorthandTestOptions<TProps = any> = { |
| 7 | + mapsValueToProp: keyof (TProps & React.HTMLProps<HTMLElement>) | false |
8 | 8 | }
|
9 | 9 |
|
10 | 10 | export const DefaultShorthandTestOptions: ShorthandTestOptions = {
|
11 | 11 | mapsValueToProp: 'content',
|
12 | 12 | }
|
13 | 13 |
|
14 |
| -export default Component => { |
| 14 | +export type ShorthandPropTestsRunner<TComponent> = < |
| 15 | + TShorthandComponent extends React.ComponentType |
| 16 | +>( |
| 17 | + shorthandProp: keyof PropsOf<TComponent>, |
| 18 | + ShorthandComponent: TShorthandComponent, |
| 19 | + options?: ShorthandTestOptions<PropsOf<TShorthandComponent>>, |
| 20 | +) => any |
| 21 | + |
| 22 | +export type ShorthandPropTestsFactory = <TComponent extends React.ComponentType>( |
| 23 | + Component: TComponent, |
| 24 | +) => ShorthandPropTestsRunner<TComponent> |
| 25 | + |
| 26 | +export default ((Component: React.ComponentType) => { |
15 | 27 | return function implementsShorthandProp(
|
16 | 28 | shorthandProp: string,
|
17 | 29 | ShorthandComponent: React.ComponentType,
|
18 | 30 | options: ShorthandTestOptions = DefaultShorthandTestOptions,
|
19 | 31 | ) {
|
20 |
| - const { mapsValueToProp } = options |
| 32 | + const mapsValueToProp = options.mapsValueToProp as string |
21 | 33 | const { displayName } = ShorthandComponent
|
22 | 34 |
|
23 | 35 | const checkPropsMatch = (props: Props, matchedProps: Props) =>
|
@@ -46,13 +58,15 @@ export default Component => {
|
46 | 58 | expect(Component.propTypes[shorthandProp]).toBeTruthy()
|
47 | 59 | })
|
48 | 60 |
|
49 |
| - test(`string value is handled as ${displayName}'s ${mapsValueToProp}`, () => { |
50 |
| - expectShorthandPropsAreHandled('shorthand prop value') |
51 |
| - }) |
| 61 | + if (options.mapsValueToProp) { |
| 62 | + test(`string value is handled as ${displayName}'s ${mapsValueToProp}`, () => { |
| 63 | + expectShorthandPropsAreHandled('shorthand prop value') |
| 64 | + }) |
| 65 | + } |
52 | 66 |
|
53 | 67 | test(`object value is spread as ${displayName}'s props`, () => {
|
54 | 68 | expectShorthandPropsAreHandled({ foo: 'foo value', bar: 'bar value' })
|
55 | 69 | })
|
56 | 70 | })
|
57 | 71 | }
|
58 |
| -} |
| 72 | +}) as ShorthandPropTestsFactory |
0 commit comments