Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 08c061c

Browse files
authored
Merge pull request #315 from KnisterPeter/robust-tests
test: make tests more robust
2 parents ee135e2 + 636ce45 commit 08c061c

10 files changed

+138
-152
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
declare module 'component' {
2-
import * as React from 'react';
2+
import * as React from 'react';
33

4-
export interface TestProps {
5-
}
4+
export interface TestProps {
5+
}
66

7-
export default class Test extends React.Component<TestProps, any> {
8-
}
9-
10-
export function test(): JSX.Element;
7+
export default class Test extends React.Component<TestProps, any> {
8+
}
119

10+
export function test(): JSX.Element;
1211
}
13-

tests/es6-class.d.ts

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
11
declare module 'component' {
2-
import * as React from 'react';
3-
4-
import Message from './path/to/Message';
5-
6-
export type ComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
7-
8-
export type ComponentOptionalUnion = string | number;
9-
10-
export interface ComponentOptionalObjectWithShape {
11-
color?: string;
12-
fontSize?: number;
13-
}
14-
15-
export type ComponentRequiredUnion = any[] | boolean;
16-
17-
export interface ComponentProps {
18-
/**
19-
* This is a jsdoc comment for optionalAny.
20-
*/
21-
optionalAny?: any;
22-
optionalArray?: any[];
23-
optionalBool?: boolean;
24-
optionalFunc?: (...args: any[])=>any;
25-
optionalNumber?: number;
26-
optionalObject?: Object;
27-
optionalString?: string;
28-
optionalNode?: React.ReactNode;
29-
optionalElement?: React.ReactElement<any>;
30-
optionalMessage?: typeof Message;
31-
optionalEnum?: ComponentOptionalEnum;
32-
optionalUnion?: ComponentOptionalUnion;
33-
optionalArrayOf?: number[];
34-
optionalObjectWithShape?: ComponentOptionalObjectWithShape;
35-
requiredFunc: (...args: any[])=>any;
36-
requiredAny: any;
37-
requiredUnion: ComponentRequiredUnion;
38-
requiredArrayOf: string[];
39-
requiredArrayOfObjectsWithShape: {
40-
color?: string;
41-
fontSize?: number;
42-
}[];
43-
requiredSymbol: typeof Symbol;
44-
}
45-
46-
export class Component extends React.Component<ComponentProps, any> {
47-
}
48-
2+
import * as React from 'react';
3+
4+
import Message from './path/to/Message';
5+
6+
export type ComponentOptionalEnum = 'News' | 'Photos' | 1 | 2;
7+
8+
export type ComponentOptionalUnion = string | number;
9+
10+
export interface ComponentOptionalObjectWithShape {
11+
color?: string;
12+
fontSize?: number;
13+
}
14+
15+
export type ComponentRequiredUnion = any[] | boolean;
16+
17+
export interface ComponentProps {
18+
/**
19+
* This is a jsdoc comment for optionalAny.
20+
*/
21+
optionalAny?: any;
22+
optionalArray?: any[];
23+
optionalBool?: boolean;
24+
optionalFunc?: (...args: any[]) => any;
25+
optionalNumber?: number;
26+
optionalObject?: Object;
27+
optionalString?: string;
28+
optionalNode?: React.ReactNode;
29+
optionalElement?: React.ReactElement<any>;
30+
optionalMessage?: typeof Message;
31+
optionalEnum?: ComponentOptionalEnum;
32+
optionalUnion?: ComponentOptionalUnion;
33+
optionalArrayOf?: number[];
34+
optionalObjectWithShape?: ComponentOptionalObjectWithShape;
35+
requiredFunc: (...args: any[]) => any;
36+
requiredAny: any;
37+
requiredUnion: ComponentRequiredUnion;
38+
requiredArrayOf: string[];
39+
requiredArrayOfObjectsWithShape: {
40+
color?: string;
41+
fontSize?: number;
42+
}[];
43+
requiredSymbol: typeof Symbol;
44+
}
45+
46+
export class Component extends React.Component<ComponentProps, any> {
47+
}
4948
}
50-
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
declare module 'component' {
2-
import * as React from 'react';
2+
import * as React from 'react';
33

4-
export interface ComponentProps {
5-
optionalAny?: any;
6-
}
7-
8-
export default class Component extends React.Component<ComponentProps, any> {
9-
}
4+
export interface ComponentProps {
5+
optionalAny?: any;
6+
}
107

8+
export default class Component extends React.Component<ComponentProps, any> {
9+
}
1110
}
12-
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import * as React from 'react';
2-
32
import Message from './path/to/Message';
43

54
export type ComponentOptionalUnion = string | number;
65

76
export type ComponentRequiredUnion = any[] | boolean;
87

98
export interface ComponentProps {
10-
/**
11-
* This is a jsdoc comment for optionalAny.
12-
*/
13-
optionalAny?: any;
14-
optionalArray?: any[];
15-
optionalBool?: boolean;
16-
optionalFunc?: (...args: any[])=>any;
17-
optionalNumber?: number;
18-
optionalObject?: Object;
19-
optionalString?: string;
20-
optionalNode?: React.ReactNode;
21-
optionalElement?: React.ReactElement<any>;
22-
optionalMessage?: typeof Message;
23-
optionalUnion?: ComponentOptionalUnion;
24-
optionalArrayOf?: number[];
25-
requiredFunc: (...args: any[])=>any;
26-
requiredAny: any;
27-
requiredUnion: ComponentRequiredUnion;
28-
requiredArrayOf: string[];
9+
/**
10+
* This is a jsdoc comment for optionalAny.
11+
*/
12+
optionalAny?: any;
13+
optionalArray?: any[];
14+
optionalBool?: boolean;
15+
optionalFunc?: (...args: any[]) => any;
16+
optionalNumber?: number;
17+
optionalObject?: Object;
18+
optionalString?: string;
19+
optionalNode?: React.ReactNode;
20+
optionalElement?: React.ReactElement<any>;
21+
optionalMessage?: typeof Message;
22+
optionalUnion?: ComponentOptionalUnion;
23+
optionalArrayOf?: number[];
24+
requiredFunc: (...args: any[]) => any;
25+
requiredAny: any;
26+
requiredUnion: ComponentRequiredUnion;
27+
requiredArrayOf: string[];
2928
}
3029

3130
export default class Component extends React.Component<ComponentProps, any> {
3231
}
33-

tests/es7-class.d.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
declare module 'component' {
2-
import * as React from 'react';
2+
import * as React from 'react';
3+
import Message from './path/to/Message';
34

4-
import Message from './path/to/Message';
5+
export type ComponentOptionalUnion = string | number;
56

6-
export type ComponentOptionalUnion = string | number;
7+
export type ComponentRequiredUnion = any[] | boolean;
78

8-
export type ComponentRequiredUnion = any[] | boolean;
9-
10-
export interface ComponentProps {
11-
/**
12-
* This is a jsdoc comment for optionalAny.
13-
*/
14-
optionalAny?: any;
15-
optionalArray?: any[];
16-
optionalBool?: boolean;
17-
optionalFunc?: (...args: any[])=>any;
18-
optionalNumber?: number;
19-
optionalObject?: Object;
20-
optionalString?: string;
21-
optionalNode?: React.ReactNode;
22-
optionalElement?: React.ReactElement<any>;
23-
optionalMessage?: typeof Message;
24-
optionalUnion?: ComponentOptionalUnion;
25-
optionalArrayOf?: number[];
26-
requiredFunc: (...args: any[])=>any;
27-
requiredAny: any;
28-
requiredUnion: ComponentRequiredUnion;
29-
requiredArrayOf: string[];
30-
}
31-
32-
export default class Component extends React.Component<ComponentProps, any> {
33-
}
9+
export interface ComponentProps {
10+
/**
11+
* This is a jsdoc comment for optionalAny.
12+
*/
13+
optionalAny?: any;
14+
optionalArray?: any[];
15+
optionalBool?: boolean;
16+
optionalFunc?: (...args: any[]) => any;
17+
optionalNumber?: number;
18+
optionalObject?: Object;
19+
optionalString?: string;
20+
optionalNode?: React.ReactNode;
21+
optionalElement?: React.ReactElement<any>;
22+
optionalMessage?: typeof Message;
23+
optionalUnion?: ComponentOptionalUnion;
24+
optionalArrayOf?: number[];
25+
requiredFunc: (...args: any[]) => any;
26+
requiredAny: any;
27+
requiredUnion: ComponentRequiredUnion;
28+
requiredArrayOf: string[];
29+
}
3430

31+
export default class Component extends React.Component<ComponentProps, any> {
32+
}
3533
}
36-

tests/import-react-component.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
declare module 'component' {
2-
import * as React from 'react';
3-
2+
import * as React from 'react';
43
}
5-
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
declare module 'component' {
2-
import * as React from 'react';
2+
import * as React from 'react';
3+
import Member from './member';
34

4-
import Member from './member';
5-
6-
export interface TestProps {
7-
test?: typeof Member;
8-
}
9-
10-
export class Test extends React.Component<TestProps, any> {
11-
}
5+
export interface TestProps {
6+
test?: typeof Member;
7+
}
128

9+
export class Test extends React.Component<TestProps, any> {
10+
}
1311
}
14-

tests/parsing-test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import * as react2dts from '../src/index';
99

1010
const basedir = path.join(__dirname, '..', '..', 'tests');
1111

12+
function normalize(input: string): string {
13+
return input
14+
.replace(/\s+/g, ' ')
15+
.replace(/ => /g, '=>');
16+
}
17+
1218
function textDiff(t: ContextualTestContext, actual: string, expected: string): void {
13-
const differences = diff.diffChars(expected, actual);
14-
if (differences.length > 1) {
19+
if (diff.diffChars(normalize(expected), normalize(actual)).length > 1) {
20+
const differences = diff.diffChars(expected, actual);
1521
const result = differences
1622
.map(part => {
1723
const value = part.value.trim() ? part.value : (part.added ? '+' : '-') + part.value;
@@ -26,8 +32,8 @@ function compare(t: ContextualTestContext, moduleName: string|null, file1: strin
2632
opts: react2dts.IOptions = {}): void {
2733
textDiff(
2834
t,
29-
react2dts.generateFromFile(moduleName, path.join(basedir, file1), opts).replace(/\r\n/g, '\n'),
30-
fs.readFileSync(path.join(basedir, file2)).toString().replace(/\r\n/g, '\n')
35+
react2dts.generateFromFile(moduleName, path.join(basedir, file1), opts),
36+
fs.readFileSync(path.join(basedir, file2)).toString()
3137
);
3238
}
3339

tests/references-in-proptypes.d.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
declare module 'component' {
2-
import * as React from 'react';
2+
import * as React from 'react';
33

4-
export type SomeComponentSomeOneOf = 'foo' | 'bar';
4+
export type SomeComponentSomeOneOf = 'foo' | 'bar';
55

6-
export interface SomeComponentSomeShape {
7-
string?: string;
8-
}
6+
export interface SomeComponentSomeShape {
7+
string?: string;
8+
}
99

10-
export interface SomeComponentProps {
11-
someOneOf?: SomeComponentSomeOneOf;
12-
someShape?: SomeComponentSomeShape;
13-
}
14-
15-
export default class SomeComponent extends React.Component<SomeComponentProps, any> {
16-
}
10+
export interface SomeComponentProps {
11+
someOneOf?: SomeComponentSomeOneOf;
12+
someShape?: SomeComponentSomeShape;
13+
}
1714

15+
export default class SomeComponent extends React.Component<SomeComponentProps, any> {
16+
}
1817
}
19-

tests/stateless.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
declare module 'component' {
2-
export interface ComponentProps {
3-
optionalAny?: any;
4-
}
2+
export interface ComponentProps {
3+
optionalAny?: any;
4+
}
55

6-
export function Component(props: ComponentProps): JSX.Element;
7-
8-
export function Component2(): JSX.Element;
6+
export function Component(props: ComponentProps): JSX.Element;
97

8+
export function Component2(): JSX.Element;
109
}
11-

0 commit comments

Comments
 (0)