Skip to content
This repository was archived by the owner on Sep 21, 2019. It is now read-only.

Commit 9bdead0

Browse files
committed
Support jest snapshots
1 parent 779e450 commit 9bdead0

File tree

11 files changed

+99
-70
lines changed
  • test

11 files changed

+99
-70
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
type Foo = {
2-
foo: string;
3-
bar: number;
2+
foo: string,
3+
bar: number
44
};
55
type Bar = {
6-
foo: number;
7-
bar: string;
6+
foo: number,
7+
bar: string
88
};

test/end-to-end/multiple-components/output.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
type HelloProps = {
2-
message?: string;
2+
message?: string
33
};
44
const Hello: React.SFC<HelloProps> = ({ message }) => {
55
return <div>hello {message}</div>;
66
};
77
type HeyProps = {
8-
message?: string;
8+
message?: string
99
};
1010
const Hey: React.SFC<HeyProps> = ({ name }) => {
1111
return <div>hey, {name}</div>;
1212
};
1313
type MyComponentState = {
14-
foo: number;
15-
bar: number;
14+
foo: number,
15+
bar: number
1616
};
17-
export default class MyComponent extends React.Component<{
18-
}, MyComponentState> {
17+
export default class MyComponent extends React.Component<{}, MyComponentState> {
1918
render() {
20-
return <button onClick={this.onclick.bind(this)}/>;
19+
return <button onClick={this.onclick.bind(this)} />;
2120
}
2221
onclick() {
2322
this.setState({ foo: 1, bar: 2 });
2423
}
2524
}
2625
type AnotherComponentProps = {
27-
foo: string;
26+
foo: string
2827
};
29-
export class AnotherComponent extends React.Component<AnotherComponentProps, {
30-
}> {
28+
export class AnotherComponent extends React.Component<
29+
AnotherComponentProps,
30+
{}
31+
> {
3132
render() {
3233
return <div />;
3334
}

test/react-js-make-props-and-state-transform/multiple-components/output.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import * as React from 'react';
2-
type MyComponentState = { foo: number; bar: number; };
3-
export default class MyComponent extends React.Component<{
4-
}, MyComponentState> {
2+
type MyComponentState = { foo: number, bar: number };
3+
export default class MyComponent extends React.Component<{}, MyComponentState> {
54
render() {
6-
return <button onClick={this.onclick.bind(this)}/>;
5+
return <button onClick={this.onclick.bind(this)} />;
76
}
87
onclick() {
98
this.setState({ foo: 1, bar: 2 });
109
}
1110
}
1211
type AnotherComponentProps = {
13-
foo: string;
12+
foo: string
1413
};
15-
export class AnotherComponent extends React.Component<AnotherComponentProps, {
16-
}> {
14+
export class AnotherComponent extends React.Component<
15+
AnotherComponentProps,
16+
{}
17+
> {
1718
static propTypes = {
18-
foo: React.PropTypes.string.isRequired,
19+
foo: React.PropTypes.string.isRequired
1920
};
2021
render() {
2122
return <div />;

test/react-move-prop-types-to-class-transform/multiple-components/output.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
class SomeComponent extends React.Component<{
2-
foo: number;
3-
}, {
4-
bar: string;
5-
}> {
1+
class SomeComponent extends React.Component<
2+
{
3+
foo: number
4+
},
5+
{
6+
bar: string
7+
}
8+
> {
69
static propTypes = { foo: React.PropTypes.string };
710
render() {
811
return null;
912
}
1013
}
1114
SomeComponent.propTypes = { foo: React.PropTypes.string };
1215
class AnotherComponent extends React.Component<{
13-
baz: number;
14-
}> {
16+
baz: number
17+
}> {
1518
static propTypes = { baz: React.PropTypes.string };
1619
render() {
1720
return null;
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
class SomeComponent extends React.Component<{
2-
foo: string;
3-
baz: string;
4-
}, {
5-
bar: string;
6-
}> {
7-
}
1+
class SomeComponent extends React.Component<
2+
{
3+
foo: string,
4+
baz: string
5+
},
6+
{
7+
bar: string
8+
}
9+
> {}
810
class AnotherComponent extends React.Component<{
9-
lol: number;
10-
}> {
11-
}
11+
lol: number
12+
}> {}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React from 'react';
2-
1+
import React from 'react'
32
export const Hello = ({ message }) => {
4-
return <div>hello {message}</div>;
5-
};
3+
return <div>hello {message}</div>
4+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component } from 'react';
2-
32
export class MyComponent extends Component {
4-
render() {
5-
return <div>hello</div>;
6-
}
3+
render() {
4+
return <div>hello</div>;
5+
}
76
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React from 'react';
2-
1+
import React from 'react'
32
export const Hello = ({ message }) => {
4-
return <div>hello {message}</div>;
5-
};
3+
return <div>hello {message}</div>
4+
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
class SomeComponent extends React.Component<{
2-
foo: number;
3-
}, {
4-
bar: string;
5-
}> {
6-
}
7-
class AnotherComponent extends React.Component<{
8-
foo: number;
9-
}, {
10-
bar: string;
11-
}> {
12-
}
1+
class SomeComponent extends React.Component<
2+
{
3+
foo: number
4+
},
5+
{
6+
bar: string
7+
}
8+
> {}
9+
class AnotherComponent extends React.Component<
10+
{
11+
foo: number
12+
},
13+
{
14+
bar: string
15+
}
16+
> {}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
type HelloProps = {
2-
message?: string;
2+
message?: string
33
};
44
const Hello: React.SFC<HelloProps> = ({ message }) => {
5-
return <div>hello {message}</div>;
5+
return <div>hello {message}</div>;
66
};
77
type HeyProps = {
8-
name: string;
8+
name: string
99
};
1010
const Hey: React.SFC<HeyProps> = ({ name }) => {
11-
return <div>hey, {name}</div>;
11+
return <div>hey, {name}</div>;
1212
};
1313
Hey.propTypes = {
14-
name: React.PropTypes.string.isRequired,
14+
name: React.PropTypes.string.isRequired
1515
};
1616
Hello.propTypes = {
17-
message: React.PropTypes.string,
17+
message: React.PropTypes.string
1818
};

test/transformers.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import * as path from 'path';
77
import * as fs from 'fs';
88
import * as ts from 'typescript';
99
import * as chalk from 'chalk';
10+
import * as _ from 'lodash';
11+
1012
import {
1113
reactJSMakePropsAndStateInterfaceTransformFactoryFactory,
1214
reactStatelessFunctionMakePropsTransformFactoryFactory,
@@ -33,15 +35,21 @@ const transformToFolderMap: [string, TransformFactoryFactory[]][] = [
3335
['end-to-end', allTransforms],
3436
];
3537

38+
const isJestUpdateSnapshotEnabled = !!_.intersection(process.argv, ['-u', '--updateSnapshot']).length;
39+
3640
for (const [testFolderName, getFactory] of transformToFolderMap) {
3741
describe(testFolderName.replace(/\-/g, ' ').replace('transform', ''), () => {
3842
for (const folderName of fs.readdirSync(path.join(__dirname, testFolderName))) {
3943
const folder = path.join(path.join(__dirname, testFolderName, folderName));
4044
if (fs.statSync(folder).isDirectory()) {
4145
it(`${testFolderName} ${folderName}`, async () => {
4246
const inputPath = path.join(folder, 'input.tsx');
43-
const output = await readFile(path.join(folder, 'output.tsx'));
47+
const outputPath = path.join(folder, 'output.tsx');
4448
const result = compile(inputPath, getFactory);
49+
if (isJestUpdateSnapshotEnabled) {
50+
await writeFile(outputPath, result);
51+
}
52+
const output = await readFile(outputPath);
4553
expect(stripEmptyLines(result)).toEqual(stripEmptyLines(output));
4654
});
4755
}
@@ -70,3 +78,17 @@ function readFile(pathToFile: string) {
7078
});
7179
});
7280
}
81+
82+
/**
83+
* Read a file
84+
* @param pathToFile Path to a string file
85+
* @param contents Contents of the file
86+
*/
87+
function writeFile(pathToFile: string, contents: string) {
88+
return new Promise<string>((resolve, reject) => {
89+
fs.writeFile(pathToFile, contents, (error) => {
90+
if (error) { return reject(error); }
91+
resolve();
92+
});
93+
});
94+
}

0 commit comments

Comments
 (0)