Skip to content

Commit

Permalink
Fix and add tests for function props (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuykendall authored Oct 1, 2020
1 parent 7c048fc commit f347470
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"react": "^16.13.1",
"react-dom": ">=16.13.1",
"tsdx": "^0.14.0",
"typescript": "^4.0.3"
"typescript": "^3"
},
"dependencies": {}
}
6 changes: 3 additions & 3 deletions setupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TesterConfig } from './src';
enzyme.configure({ adapter: new Adapter() });

const TestHookComponent = ({ propOne, propTwo, ...rest }: any) => (
<div id="test-hook-unique">
<div id="test-hook-unique" className={`${propOne} ${propTwo}`}>
<div className="test-hook-component" {...rest} />
</div>
);
Expand All @@ -23,10 +23,10 @@ TesterConfig.configure(enzyme, {
onInit: tester => {
(tester as any).testHookOnInit = true;
},
props: {
props: (_tester: any) => ({
propOne: 'un',
propTwo: 'deux',
},
}),
},
],
});
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ export function getInstance(component: any) {
return instance && (instance.wrappedInstance || instance);
}

export async function getValue(tester: any, value: unknown) {
return typeof value === 'function' ? await value(tester) : value;
export function isFunction(value: unknown): value is Function {
return typeof value === 'function';
}

export function getValue(tester: any, value: unknown) {
return isFunction(value) ? value(tester) : value;
}

export async function sleep(ms: number = 0) {
Expand Down
56 changes: 47 additions & 9 deletions test/tester.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { Component } from 'react';
import { Tester } from '../src';
import { sleep } from '../src/utils';
import { isFunction, sleep } from '../src/utils';
import { IProps } from '../src/interfaces';

const COMPONENT_ID = 'testing-component';

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

class AsyncComponent extends Component<{}, { status: string }> {
public constructor(props: object) {
interface IAsyncComponentProps {
label?: string;
}
class AsyncComponent extends Component<IAsyncComponentProps, { status: string }> {
public constructor(props: IAsyncComponentProps) {
super(props);
this.state = { status: 'loading' };
}
Expand All @@ -20,7 +24,12 @@ class AsyncComponent extends Component<{}, { status: string }> {
}

public render() {
return <div>{this.state.status}</div>;
return (
<div>
{this.props.label}
{this.state.status}
</div>
);
}
}

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

it('Awaits async props function', async () => {
const props = jest.fn(),
tester = await new Tester(AsyncComponent, { props });
const label = '7a30534a-62dc-42c6-a9e9-62be0e0a713c'; // Random uuid
const propTypes: Array<[string, IProps]> = [
['object', { label }],
['function', jest.fn().mockImplementation(() => ({ label }))],
['promise', jest.fn().mockResolvedValue({ label })],
];
propTypes.forEach(([type, props]) => {
it(`Handles ${type} props`, async () => {
const tester = await new Tester(AsyncComponent, { props });

if (isFunction(props)) {
expect(props).not.toBeCalled();
}

await tester.mount();

if (isFunction(props)) {
expect(props).toBeCalled();
}
expect(tester.text()).toContain(label);
});
});

it('Properly calls hooks from setupTests.ts', async () => {
const tester = await new Tester(AsyncComponent);

expect(!!(tester as any).testHookOnInit).toBe(true);
expect(!!(tester as any).testHookOnBeforeMount).toBe(false);

expect(props).not.toBeCalled();
await tester.mount();
expect(props).toBeCalled();

expect(!!(tester as any).testHookOnInit).toBe(true);
expect(!!(tester as any).testHookOnBeforeMount).toBe(true);

expect(tester.find('#test-hook-unique').length).toBe(1);
expect(tester.find('.un.deux').length).toBe(1);
});
});
13 changes: 4 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2089,9 +2089,9 @@ camelcase@^6.0.0:
integrity sha512-8KMDF1Vz2gzOq54ONPJS65IvTUaB1cHJ2DMM7MbPmLZljDH1qpzzLsWdiN9pHh6qvkRVDTi/07+eNGch/oLU4w==

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

capture-exit@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -7468,16 +7468,11 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

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

typescript@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==

unicode-canonical-property-names-ecmascript@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"
Expand Down

0 comments on commit f347470

Please sign in to comment.