Skip to content

Commit f347470

Browse files
authored
Fix and add tests for function props (#36)
1 parent 7c048fc commit f347470

File tree

5 files changed

+61
-24
lines changed

5 files changed

+61
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"react": "^16.13.1",
7171
"react-dom": ">=16.13.1",
7272
"tsdx": "^0.14.0",
73-
"typescript": "^4.0.3"
73+
"typescript": "^3"
7474
},
7575
"dependencies": {}
7676
}

setupTests.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TesterConfig } from './src';
77
enzyme.configure({ adapter: new Adapter() });
88

99
const TestHookComponent = ({ propOne, propTwo, ...rest }: any) => (
10-
<div id="test-hook-unique">
10+
<div id="test-hook-unique" className={`${propOne} ${propTwo}`}>
1111
<div className="test-hook-component" {...rest} />
1212
</div>
1313
);
@@ -23,10 +23,10 @@ TesterConfig.configure(enzyme, {
2323
onInit: tester => {
2424
(tester as any).testHookOnInit = true;
2525
},
26-
props: {
26+
props: (_tester: any) => ({
2727
propOne: 'un',
2828
propTwo: 'deux',
29-
},
29+
}),
3030
},
3131
],
3232
});

src/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ export function getInstance(component: any) {
77
return instance && (instance.wrappedInstance || instance);
88
}
99

10-
export async function getValue(tester: any, value: unknown) {
11-
return typeof value === 'function' ? await value(tester) : value;
10+
export function isFunction(value: unknown): value is Function {
11+
return typeof value === 'function';
12+
}
13+
14+
export function getValue(tester: any, value: unknown) {
15+
return isFunction(value) ? value(tester) : value;
1216
}
1317

1418
export async function sleep(ms: number = 0) {

test/tester.test.tsx

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React, { Component } from 'react';
22
import { Tester } from '../src';
3-
import { sleep } from '../src/utils';
3+
import { isFunction, sleep } from '../src/utils';
4+
import { IProps } from '../src/interfaces';
45

56
const COMPONENT_ID = 'testing-component';
67

78
const MyTestingComponent = (props: any) => <div id={COMPONENT_ID} {...props} />;
89

9-
class AsyncComponent extends Component<{}, { status: string }> {
10-
public constructor(props: object) {
10+
interface IAsyncComponentProps {
11+
label?: string;
12+
}
13+
class AsyncComponent extends Component<IAsyncComponentProps, { status: string }> {
14+
public constructor(props: IAsyncComponentProps) {
1115
super(props);
1216
this.state = { status: 'loading' };
1317
}
@@ -20,7 +24,12 @@ class AsyncComponent extends Component<{}, { status: string }> {
2024
}
2125

2226
public render() {
23-
return <div>{this.state.status}</div>;
27+
return (
28+
<div>
29+
{this.props.label}
30+
{this.state.status}
31+
</div>
32+
);
2433
}
2534
}
2635

@@ -42,12 +51,41 @@ describe('Tester', () => {
4251
expect(tester.text()).toContain('done');
4352
});
4453

45-
it('Awaits async props function', async () => {
46-
const props = jest.fn(),
47-
tester = await new Tester(AsyncComponent, { props });
54+
const label = '7a30534a-62dc-42c6-a9e9-62be0e0a713c'; // Random uuid
55+
const propTypes: Array<[string, IProps]> = [
56+
['object', { label }],
57+
['function', jest.fn().mockImplementation(() => ({ label }))],
58+
['promise', jest.fn().mockResolvedValue({ label })],
59+
];
60+
propTypes.forEach(([type, props]) => {
61+
it(`Handles ${type} props`, async () => {
62+
const tester = await new Tester(AsyncComponent, { props });
63+
64+
if (isFunction(props)) {
65+
expect(props).not.toBeCalled();
66+
}
67+
68+
await tester.mount();
69+
70+
if (isFunction(props)) {
71+
expect(props).toBeCalled();
72+
}
73+
expect(tester.text()).toContain(label);
74+
});
75+
});
76+
77+
it('Properly calls hooks from setupTests.ts', async () => {
78+
const tester = await new Tester(AsyncComponent);
79+
80+
expect(!!(tester as any).testHookOnInit).toBe(true);
81+
expect(!!(tester as any).testHookOnBeforeMount).toBe(false);
4882

49-
expect(props).not.toBeCalled();
5083
await tester.mount();
51-
expect(props).toBeCalled();
84+
85+
expect(!!(tester as any).testHookOnInit).toBe(true);
86+
expect(!!(tester as any).testHookOnBeforeMount).toBe(true);
87+
88+
expect(tester.find('#test-hook-unique').length).toBe(1);
89+
expect(tester.find('.un.deux').length).toBe(1);
5290
});
5391
});

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,9 @@ camelcase@^6.0.0:
20892089
integrity sha512-8KMDF1Vz2gzOq54ONPJS65IvTUaB1cHJ2DMM7MbPmLZljDH1qpzzLsWdiN9pHh6qvkRVDTi/07+eNGch/oLU4w==
20902090

20912091
caniuse-lite@^1.0.30001135:
2092-
version "1.0.30001137"
2093-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001137.tgz#6f0127b1d3788742561a25af3607a17fc778b803"
2094-
integrity sha512-54xKQZTqZrKVHmVz0+UvdZR6kQc7pJDgfhsMYDG19ID1BWoNnDMFm5Q3uSBSU401pBvKYMsHAt9qhEDcxmk8aw==
2092+
version "1.0.30001140"
2093+
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001140.tgz#30dae27599f6ede2603a0962c82e468bca894232"
2094+
integrity sha512-xFtvBtfGrpjTOxTpjP5F2LmN04/ZGfYV8EQzUIC/RmKpdrmzJrjqlJ4ho7sGuAMPko2/Jl08h7x9uObCfBFaAA==
20952095

20962096
capture-exit@^2.0.0:
20972097
version "2.0.0"
@@ -7468,16 +7468,11 @@ typedarray-to-buffer@^3.1.5:
74687468
dependencies:
74697469
is-typedarray "^1.0.0"
74707470

7471-
typescript@^3.7.3:
7471+
typescript@^3, typescript@^3.7.3:
74727472
version "3.9.7"
74737473
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa"
74747474
integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==
74757475

7476-
typescript@^4.0.3:
7477-
version "4.0.3"
7478-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
7479-
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==
7480-
74817476
unicode-canonical-property-names-ecmascript@^1.0.4:
74827477
version "1.0.4"
74837478
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)