Skip to content

Commit 145868e

Browse files
authored
Merge pull request #31 from wix-incubator/migrate-to-jest-snapshots
test: migrate to inbuilt jest snapshots
2 parents 80c8856 + 5b3dd6d commit 145868e

File tree

21 files changed

+312
-389
lines changed

21 files changed

+312
-389
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jest.jestCommandLine": "npx jest"
3+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"clean-generated": "rimraf tests/__generated__ tests/**/__generated__ tests/cli-configs-sandbox/*/dist",
77
"generate-for-type-tests": "ts-node tests/pregenerate.ts",
8-
"generate-snapshot": "ts-node tests/update-snaptshots.ts generate",
8+
"generate-snapshot": "jest -i --updateSnapshot",
99
"lint": "eslint '**/*.{ts,tsx}'",
1010
"lint:fix": "eslint '**/*.{ts,tsx}' --fix",
1111
"typecheck": "tsc --noEmit",
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`complex interpolation case snapshot 1`] = `
4+
"/* eslint-disable */
5+
/* tslint:disable */
6+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
7+
return {
8+
order: {
9+
shippingLabel: {
10+
customerDetailsCard: {
11+
address: (data: Record<'firstName' | 'lastName' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => t('order.shippingLabel.customerDetailsCard.address', data), /* {{firstName}} {{lastName}}, {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, concat ', '}}{{state, suffix ' '}}{{zipCode, suffix ', '}}{{country}} */
12+
},
13+
labelDetailsCard: {
14+
shipFrom: {
15+
addressFormat: (data: Record<'addressLine1' | 'addressLine2' | 'city' | 'state' | 'zipCode' | 'country', unknown>) => t('order.shippingLabel.labelDetailsCard.shipFrom.addressFormat', data), /* {{addressLine1, suffix ', '}}{{addressLine2, suffix ', '}}{{city, suffix ', '}}{{state}} {{zipCode, suffix ', '}}{{country}} */
16+
},
17+
},
18+
},
19+
},
20+
};
21+
}
22+
23+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
24+
"
25+
`;
26+
27+
exports[`data interpolation icu snapshot 1`] = `
28+
"/* eslint-disable */
29+
/* tslint:disable */
30+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
31+
return {
32+
common: {
33+
people: {
34+
message: (data: Record<'numPersons', unknown>) => t('common.people.message', data), /* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
35+
messageComplex: (data: Record<'name' | 'numPersons' | 'productsAmount', unknown>) => t('common.people.messageComplex', data), /* Hey {name}, There are {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} that want to change the {productsAmount, plural, =1 {price of 1 product} other {prices of # products}} */
36+
pluralMessage: (data: Record<'numPeople', unknown>) => t('common.people.pluralMessage', data), /* {numPeople, plural, =0 {No one is} =1 {One person is} other {# people are}} interested */
37+
ordinalMessage: (data: Record<'position', unknown>) => t('common.people.ordinalMessage', data), /* {position, selectordinal, one {You're 1st} two {You're 2nd} few {You're 3rd} other {You're #th}} */
38+
dateMessage: (data: Record<'currentDate', unknown>) => t('common.people.dateMessage', data), /* Today is {currentDate, date, long} */
39+
timeMessage: (data: Record<'currentTime', unknown>) => t('common.people.timeMessage', data), /* The current time is {currentTime, time, short} */
40+
selectMessage: (data: Record<'gender', unknown>) => t('common.people.selectMessage', data), /* {gender, select, male {He is} female {She is} other {They are} } interested */
41+
numberMessage: (data: Record<'numApples', unknown>) => t('common.people.numberMessage', data), /* You have {numApples, number} apples */
42+
},
43+
},
44+
};
45+
}
46+
47+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
48+
"
49+
`;
50+
51+
exports[`data interpolation icu with nested params snapshot 1`] = `
52+
"/* eslint-disable */
53+
/* tslint:disable */
54+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
55+
return {
56+
common: {
57+
people: {
58+
message: (data: Record<'numPersons', unknown>) => t('common.people.message', data), /* Hey, {numPersons, plural, =0 {no one} =1 {one person} other {# persons}} */
59+
messageNestedParams: (data: Record<'name' | 'numPersons', unknown>) => t('common.people.messageNestedParams', data), /* Hey, {numPersons, plural, =0 {No one here.} one {{name}. You are the only person here.} other {{name} and # other persons are here.}} */
60+
},
61+
},
62+
};
63+
}
64+
65+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
66+
"
67+
`;
68+
69+
exports[`data interpolation single quote snapshot 1`] = `
70+
"/* eslint-disable */
71+
/* tslint:disable */
72+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
73+
return {
74+
common: {
75+
loggedIn: {
76+
message: (data: Record<'username', unknown>) => t('common.loggedIn.message', data), /* Hey, {username}, you have successfully logged in! */
77+
},
78+
},
79+
readingWarning: (data: Record<'reader' | 'writer', unknown>) => t('readingWarning', data), /* {reader} reads message from {writer} */
80+
};
81+
}
82+
83+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
84+
"
85+
`;
86+
87+
exports[`flat data snapshot 1`] = `
88+
"/* eslint-disable */
89+
/* tslint:disable */
90+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
91+
return {
92+
common: {
93+
cancel: () => t('common.cancel'), /* Cancel */
94+
},
95+
model: {
96+
player: {
97+
name: () => t('model.player.name'), /* Name */
98+
},
99+
},
100+
};
101+
}
102+
103+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
104+
"
105+
`;
106+
107+
exports[`nested data snapshot 1`] = `
108+
"/* eslint-disable */
109+
/* tslint:disable */
110+
export function LocaleKeys<R extends string>(t: (...args: unknown[]) => R) {
111+
return {
112+
common: {
113+
create: () => t('common.create'), /* Create */
114+
},
115+
model: {
116+
user: {
117+
id: () => t('model.user.id'), /* ID */
118+
},
119+
},
120+
};
121+
}
122+
123+
export type ILocaleKeys = ReturnType<typeof LocaleKeys>;
124+
"
125+
`;

tests/snapshot/react/LocaleKeys.tsx renamed to tests/__snapshots__/reactBindings.spec.tsx.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/* eslint-disable */
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`react file (snapshot) 1`] = `
4+
"/* eslint-disable */
25
/* tslint:disable */
36
import React from 'react';
47
@@ -22,3 +25,5 @@ export const LocaleKeysProvider: React.FC<{ translateFn?: (...args: unknown[]) =
2225
return <LocaleKeysContext.Provider value={value}>{children}</LocaleKeysContext.Provider>;
2326
};
2427
export const useLocaleKeys = () => React.useContext(LocaleKeysContext);
28+
"
29+
`;

tests/config.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ test('should apply params from package.json', async () => {
5454
const driver = new Driver();
5555

5656
driver.given.cwd('tests/cli-configs-sandbox/packageJsonBooleans');
57-
58-
await driver.when.runsCodegenCommand({
57+
driver.given.cliParams({
5958
source: 'source.json',
6059
});
60+
await driver.when.runsCodegenCommand();
6161

6262
const { useLocaleKeys, LocaleKeys } = await driver.get.generatedResults<
6363
GeneratedModule<ResultOne>
@@ -79,13 +79,14 @@ test('should override params in package.json with cli args', async () => {
7979

8080
driver.given.cwd('tests/cli-configs-sandbox/overridePackageJson');
8181

82-
await driver.when.runsCodegenCommand({
82+
driver.given.cliParams({
8383
source: 'source.json',
8484
output: 'dist',
8585
reactHook: false,
8686
showTranslations: false,
8787
singleCurlyBraces: false,
8888
});
89+
await driver.when.runsCodegenCommand();
8990

9091
const { useLocaleKeys, LocaleKeys } = await driver.get.generatedResults<
9192
GeneratedModule<ResultOne>

tests/driver.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import jsonStringify from 'fast-json-stable-stringify';
66
import { Generator } from '../src/Generator';
77
import { CliParams } from '../src/bin';
88
import { DEFAULT_FN_NAME, DEFAULT_OUTPUT } from '../src/constants';
9-
import { EXPORT_SNAPSHOT_DIRECTORY } from './generateFiles';
109
import { getFileExtension } from '../src/utils';
1110

1211
const readFile = util.promisify(fs.readFile);
1312

1413
export class Driver {
1514
private cwd: string = process.cwd();
15+
private cliParams: Partial<CliParams> = {
16+
functionName: DEFAULT_FN_NAME,
17+
};
1618
private namespace: string | undefined;
1719
private functionName: string | undefined;
1820
private isReactFile: boolean | undefined;
@@ -46,19 +48,20 @@ export class Driver {
4648
namespace: (namespace: string): void => {
4749
this.namespace = namespace;
4850
},
51+
cliParams: (values: Partial<CliParams>) => {
52+
Object.assign(this.cliParams, values);
53+
},
4954
};
5055

5156
public when = {
52-
runsCodegenCommand: async (
53-
params: Partial<CliParams> = {}
54-
): Promise<void> => {
57+
runsCodegenCommand: async (): Promise<void> => {
5558
const {
5659
source = this.namespacedSource,
5760
output = this.namespacedOutput,
5861
functionName = DEFAULT_FN_NAME,
5962
reactHook,
6063
...rest
61-
} = params;
64+
} = this.cliParams;
6265

6366
this.functionName = functionName;
6467
this.isReactFile = reactHook;
@@ -128,11 +131,5 @@ export class Driver {
128131
.namespace!}/LocaleKeys.${getFileExtension(this.isReactFile)}`,
129132
'utf8'
130133
),
131-
generatedSnapShotAsStr: (): Promise<string> =>
132-
readFile(
133-
`${EXPORT_SNAPSHOT_DIRECTORY}/${this
134-
.namespace!}/LocaleKeys.${getFileExtension(this.isReactFile)}`,
135-
'utf8'
136-
),
137134
};
138135
}

0 commit comments

Comments
 (0)