Skip to content

Commit 753ce47

Browse files
authored
Merge pull request #3 from netceteragroup/typescript-rewrite
Typescript rewrite
2 parents 46e22b4 + 6a33fea commit 753ce47

23 files changed

+463
-363
lines changed

.babelrc.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

.eslintignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 26 deletions
This file was deleted.

package.json

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"author": "Netcetera AG",
1313
"license": "MIT",
1414
"repository": "https://github.com/netceteragroup/react-message-source",
15-
"main": "dist/index.js",
16-
"module": "dist/index.es.js",
17-
"jsnext:main": "dist/index.es.js",
15+
"main": "dist/react-message-source.js",
16+
"module": "dist/react-message-source.es.js",
17+
"jsnext:main": "dist/react-message-source.es.js",
1818
"engines": {
1919
"node": ">=8",
2020
"npm": ">=5"
@@ -23,6 +23,7 @@
2323
"test": "cross-env CI=1 react-scripts test --env=jsdom",
2424
"test:watch": "react-scripts test --env=jsdom",
2525
"coverage": "cross-env CI=1 react-scripts test --coverage --coverageReporters=text-lcov | coveralls",
26+
"prebuild": "rimraf dist",
2627
"build": "rollup -c",
2728
"start": "rollup -c -w",
2829
"prepare": "yarn run build",
@@ -37,31 +38,34 @@
3738
"react": "^16.8.0"
3839
},
3940
"devDependencies": {
40-
"@babel/core": "^7.3.4",
41-
"@babel/plugin-proposal-class-properties": "^7.3.4",
42-
"@babel/preset-env": "^7.3.4",
43-
"@babel/preset-react": "^7.0.0",
41+
"@types/hoist-non-react-statics": "^3.3.0",
42+
"@types/invariant": "^2.2.29",
43+
"@types/jest": "^24.0.11",
44+
"@types/react": "^16.8.8",
45+
"@types/react-dom": "^16.8.2",
46+
"@types/react-test-renderer": "^16.8.1",
4447
"coveralls": "^3.0.3",
4548
"cross-env": "^5.2.0",
46-
"eslint-config-airbnb": "^17.1.0",
47-
"eslint-config-prettier": "^4.1.0",
48-
"eslint-plugin-import": "^2.16.0",
49-
"eslint-plugin-jsx-a11y": "^6.2.1",
50-
"eslint-plugin-prettier": "^3.0.1",
51-
"eslint-plugin-react": "^7.12.4",
52-
"eslint-plugin-react-hooks": "^1.5.1",
5349
"prettier": "^1.16.4",
5450
"prop-types": "^15.7.2",
5551
"react": "^16.8.4",
5652
"react-dom": "^16.8.4",
5753
"react-scripts": "2.1.8",
5854
"react-test-renderer": "^16.8.4",
5955
"react-testing-library": "^6.0.0",
56+
"rimraf": "^2.6.3",
6057
"rollup": "^1.6.0",
61-
"rollup-plugin-babel": "^4.3.2",
6258
"rollup-plugin-commonjs": "^9.2.1",
6359
"rollup-plugin-node-resolve": "^4.0.1",
64-
"rollup-plugin-peer-deps-external": "^2.2.0"
60+
"rollup-plugin-peer-deps-external": "^2.2.0",
61+
"rollup-plugin-typescript2": "^0.20.1",
62+
"tslib": "^1.9.3",
63+
"tslint": "^5.14.0",
64+
"tslint-config-prettier": "^1.18.0",
65+
"tslint-plugin-prettier": "^2.0.1",
66+
"tslint-react": "^3.6.0",
67+
"tslint-react-hooks": "^2.0.0",
68+
"typescript": "^3.3.3333"
6569
},
6670
"files": [
6771
"dist"

rollup.config.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
import babel from 'rollup-plugin-babel';
1+
import typescript from 'rollup-plugin-typescript2';
22
import commonjs from 'rollup-plugin-commonjs';
33
import external from 'rollup-plugin-peer-deps-external';
44
import resolve from 'rollup-plugin-node-resolve';
55

66
import pkg from './package.json';
77

88
module.exports = {
9-
input: 'src/index.js',
9+
input: 'src/index.ts',
1010
output: [
1111
{
1212
file: pkg.main,
1313
format: 'cjs',
14-
sourcemap: true,
14+
exports: 'named',
15+
sourcemap: true
1516
},
1617
{
1718
file: pkg.module,
1819
format: 'es',
19-
sourcemap: true,
20-
},
20+
exports: 'named',
21+
sourcemap: true
22+
}
2123
],
2224
plugins: [
2325
external(),
24-
babel(),
2526
resolve(),
27+
typescript({
28+
rollupCommonJSResolveHack: true,
29+
clean: true,
30+
}),
2631
commonjs()
27-
].filter(Boolean),
32+
],
2833
};
File renamed without changes.
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import * as RTL from 'react-testing-library';
33
import { FetchingProvider } from './FetchingProvider';
44
import { useMessageSource } from './useMessageSource';
55

66
describe('FetchingProvider', () => {
7-
const Spy = () => {
7+
function Spy() {
88
const { getMessage } = useMessageSource();
9-
return getMessage('hello.world');
10-
};
9+
return <span>{getMessage('hello.world')}</span>;
10+
}
1111

1212
beforeEach(() => {
1313
// mock impl of fetch() API
14+
// @ts-ignore
1415
global.fetch = jest.fn(() =>
1516
Promise.resolve({
1617
json: () =>
@@ -47,17 +48,18 @@ describe('FetchingProvider', () => {
4748
expect(transform).toHaveBeenCalled();
4849
expect(onFetchingStart).toHaveBeenCalled();
4950
expect(onFetchingEnd).toHaveBeenCalled();
51+
// @ts-ignore
5052
expect(global.fetch).toHaveBeenCalledTimes(1);
5153
});
5254

5355
it('fetches text resources when url prop changes', async () => {
5456
const transform = jest.fn(x => x);
5557
const onFetchingStart = jest.fn();
5658
const onFetchingEnd = jest.fn();
57-
function TestComponent(props) {
59+
function TestComponent(props: { url: string }) {
5860
return (
5961
<FetchingProvider
60-
url={props.url} // eslint-disable-line react/prop-types
62+
url={props.url}
6163
transform={transform}
6264
onFetchingStart={onFetchingStart}
6365
onFetchingEnd={onFetchingEnd}
@@ -70,30 +72,27 @@ describe('FetchingProvider', () => {
7072
const { getByText, rerender } = RTL.render(<TestComponent url="http://any.uri/texts?lang=en" />);
7173
await RTL.waitForElement(() => getByText('Hello world'));
7274

73-
RTL.act(() => {
74-
rerender(<TestComponent url="http://any.uri/texts?lang=de" />);
75-
});
75+
const fetchNewLanguage = async () => {
76+
RTL.act(() => {
77+
rerender(<TestComponent url="http://any.uri/texts?lang=de" />);
78+
});
7679

77-
await RTL.wait(
78-
() =>
79-
new Promise(resolve => {
80-
// simulate network request
81-
setTimeout(() => resolve(), 300);
82-
}),
83-
);
80+
return await RTL.waitForElement(() => getByText('Hello world'));
81+
};
82+
83+
await fetchNewLanguage();
8484

85+
// @ts-ignore
8586
expect(global.fetch).toHaveBeenCalledTimes(2);
86-
expect(transform).toHaveBeenCalledTimes(2);
87-
expect(onFetchingStart).toHaveBeenCalledTimes(2);
88-
expect(onFetchingEnd).toHaveBeenCalledTimes(2);
8987
});
9088

9189
it('invokes onFetchingError lifecycle on network failure', async () => {
9290
const onFetchingError = jest.fn();
9391
const faultyFetch = jest.fn(() => Promise.reject(new Error('Failure')));
92+
// @ts-ignore
9493
global.fetch = faultyFetch;
9594

96-
RTL.render(<FetchingProvider url="http://any.uri/texts" onFetchingError={onFetchingError} />);
95+
RTL.render(<FetchingProvider url="http://any.uri/texts" onFetchingError={onFetchingError} children={null} />);
9796
await RTL.wait(); // until fetch() rejects
9897

9998
expect(faultyFetch).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)