Skip to content

Commit 055f0e2

Browse files
avocadowastakentrojanowski
authored andcommitted
refactor: Rewrite in TypeScript. (#39)
BREAKING CHANGE: `useApolloClient` throws if the client is not available in the context instead of returning null
1 parent e1a9b6f commit 055f0e2

24 files changed

+594
-862
lines changed

.babelrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module.exports = {
22
presets: [
33
'@babel/preset-react',
4+
'@babel/preset-typescript',
45
[
56
'@babel/preset-env',
67
{
78
loose: true,
89
modules: process.env.ES_MODULES ? false : 'commonjs',
10+
targets: process.env.NODE_ENV !== 'test' ? undefined : { node: true },
911
},
1012
],
1113
],

.circleci/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ jobs:
2828
- run:
2929
name: Check prettier
3030
command: yarn format-check
31+
- run:
32+
name: Check typings
33+
command: yarn typings-check
3134
- run:
3235
name: Lint code
33-
command: yarn lint-ci
36+
command: yarn lint
3437
- run:
3538
name: test
3639
command: yarn test

.eslintignore

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

.eslintrc.js

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

package.json

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
"description": "",
55
"main": "lib/index.js",
66
"module": "es/index.js",
7+
"typings": "lib/index.d.ts",
78
"files": [
89
"es",
9-
"lib",
10-
"src/index.d.ts"
10+
"lib"
1111
],
1212
"scripts": {
13-
"build:cjs": "rimraf lib && babel src --ignore '**/__tests__' --ignore '**/__testutils__' -d lib",
14-
"build:es": "rimraf es && cross-env ES_MODULES=true babel src --ignore '**/__tests__' --ignore '**/__testutils__' -d es",
15-
"build": "npm run build:cjs && npm run build:es",
13+
"build:cjs": "rimraf lib && babel src --extensions '.ts' --extensions '.tsx' --ignore '**/__tests__' --ignore '**/__testutils__' -d lib",
14+
"build:es": "rimraf es && cross-env ES_MODULES=true babel src --extensions '.ts' --extensions '.tsx' --ignore '**/__tests__' --ignore '**/__testutils__' -d es",
15+
"build:typings": "tsc -p tsconfig.typings.json",
16+
"build": "npm run build:cjs && npm run build:es && npm run build:typings",
17+
"typings-check": "tsc --noEmit",
1618
"format-check": "prettier --list-different \"**/*.{js,ts,tsx}\"",
1719
"format": "prettier --write \"**/*.{js,ts,tsx}\"",
18-
"lint-ci": "eslint --max-warnings=0 .",
19-
"lint": "eslint .",
20+
"lint": "tslint --project . \"src/**/*.{ts,tsx}\"",
2021
"prepublishOnly": "npm run build",
2122
"test": "jest"
2223
},
@@ -27,13 +28,26 @@
2728
"type": "git",
2829
"url": "https://github.com/trojanowski/react-apollo-hooks.git"
2930
},
30-
"types": "./src/index.d.ts",
3131
"jest": {
32-
"setupTestFrameworkScriptFile": "./src/__testutils__/setupTests.js",
32+
"setupTestFrameworkScriptFile": "./src/__testutils__/setupTests.ts",
33+
"transform": {
34+
"^.+\\.(ts|tsx)$": "babel-jest"
35+
},
36+
"testMatch": [
37+
"**/__tests__/**/*-test.ts?(x)"
38+
],
3339
"testPathIgnorePatterns": [
3440
"/<rootDir>/es/",
3541
"/<rootDir>/lib/",
3642
"/node_modules/"
43+
],
44+
"moduleFileExtensions": [
45+
"ts",
46+
"tsx",
47+
"js",
48+
"json",
49+
"jsx",
50+
"node"
3751
]
3852
},
3953
"husky": {
@@ -42,12 +56,14 @@
4256
}
4357
},
4458
"lint-staged": {
45-
"**/*.{js,ts,tsx}": [
46-
"prettier --single-quote --write",
59+
"**/*.js": [
60+
"prettier --write",
4761
"git add"
4862
],
49-
"**/*.js": [
50-
"eslint --max-warnings=0"
63+
"**/*.{ts,tsx}": [
64+
"tslint --fix --project .",
65+
"prettier --write",
66+
"git add"
5167
]
5268
},
5369
"peerDependencies": {
@@ -64,24 +80,18 @@
6480
"@babel/core": "^7.1.5",
6581
"@babel/preset-env": "^7.1.0",
6682
"@babel/preset-react": "^7.0.0",
83+
"@babel/preset-typescript": "^7.1.0",
6784
"@types/graphql": "^14.0.3",
68-
"@types/react": "^16.4.18",
85+
"@types/jest": "^23.3.10",
86+
"@types/lodash": "^4.14.119",
87+
"@types/react": "^16.7.17",
6988
"apollo-cache-inmemory": "^1.3.11",
7089
"apollo-client": "^2.4.7",
7190
"apollo-link": "^1.2.4",
7291
"apollo-link-mock": "^1.0.1",
73-
"babel-core": "^7.0.0-bridge",
74-
"babel-eslint": "9.x",
92+
"babel-core": "^7.0.0-bridge.0",
7593
"babel-jest": "^23.6.0",
7694
"cross-env": "^5.2.0",
77-
"eslint": "5.x",
78-
"eslint-config-prettier": "^3.3.0",
79-
"eslint-config-react-app": "^3.0.5",
80-
"eslint-plugin-flowtype": "2.x",
81-
"eslint-plugin-import": "2.x",
82-
"eslint-plugin-jsx-a11y": "6.x",
83-
"eslint-plugin-react": "7.x",
84-
"eslint-plugin-react-hooks": "^0.0.0",
8595
"graphql": "^14.0.2",
8696
"graphql-tag": "^2.10.0",
8797
"husky": "^1.2.0",
@@ -94,6 +104,9 @@
94104
"react-testing-library": "^5.3.1",
95105
"rimraf": "^2.6.2",
96106
"standard-version": "^4.4.0",
97-
"typescript": "^3.1.3"
107+
"tslint": "^5.12.0",
108+
"tslint-config-prettier": "^1.17.0",
109+
"tslint-react": "^3.6.0",
110+
"typescript": "^3.2.2"
98111
}
99112
}

src/__tests__/useApolloClient-test.js renamed to src/__tests__/useApolloClient-test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22
import { cleanup, render } from 'react-testing-library';
33

4-
import createClient from '../__testutils__/createClient';
54
import { ApolloProvider, useApolloClient } from '..';
5+
import createClient from '../__testutils__/createClient';
66

77
afterEach(cleanup);
88

src/__tests__/useMutation-test.js renamed to src/__tests__/useMutation-test.tsx

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { ApolloProvider, useMutation, useQuery } from '..';
1111
import createClient from '../__testutils__/createClient';
1212
import { SAMPLE_TASKS } from '../__testutils__/data';
13+
import noop from '../__testutils__/noop';
1314
import waitForNextTick from '../__testutils__/waitForNextTick';
1415

1516
const TASKS_MOCKS = [
@@ -29,8 +30,8 @@ const TASKS_MOCKS = [
2930
},
3031
result: {
3132
data: {
32-
tasks: [...SAMPLE_TASKS],
3333
__typename: 'Query',
34+
tasks: [...SAMPLE_TASKS],
3435
},
3536
},
3637
},
@@ -50,12 +51,12 @@ const TASKS_MOCKS = [
5051
},
5152
result: {
5253
data: {
54+
__typename: 'Mutation',
5355
toggleTask: {
54-
id: '1',
55-
completed: false,
5656
__typename: 'Task',
57+
completed: false,
58+
id: '1',
5759
},
58-
__typename: 'Mutation',
5960
},
6061
},
6162
},
@@ -76,13 +77,13 @@ const TASKS_MOCKS = [
7677
},
7778
result: {
7879
data: {
80+
__typename: 'Mutation',
7981
addTask: {
80-
id: '4',
82+
__typename: 'Task',
8183
completed: false,
84+
id: '4',
8285
text: 'Learn Jest',
83-
__typename: 'Task',
8486
},
85-
__typename: 'Mutation',
8687
},
8788
},
8889
},
@@ -117,7 +118,19 @@ const ADD_TASK_MUTATION = gql`
117118
}
118119
`;
119120

120-
function Task({ onChange, task }) {
121+
interface TaskFragment {
122+
id: number;
123+
text: string;
124+
completed: boolean;
125+
}
126+
127+
function Task({
128+
onChange,
129+
task,
130+
}: {
131+
task: TaskFragment;
132+
onChange: (task: TaskFragment) => void;
133+
}) {
121134
return (
122135
<li>
123136
<input
@@ -130,7 +143,13 @@ function Task({ onChange, task }) {
130143
);
131144
}
132145

133-
function TaskList({ onChange, tasks }) {
146+
function TaskList({
147+
onChange,
148+
tasks,
149+
}: {
150+
tasks: TaskFragment[];
151+
onChange: (task: TaskFragment) => void;
152+
}) {
134153
return (
135154
<ul>
136155
{tasks.map(task => (
@@ -163,7 +182,7 @@ it('should create a function to perform mutations', async () => {
163182
const { container } = render(
164183
<ApolloProvider client={client}>
165184
<Suspense fallback={<div>Loading</div>}>
166-
<TasksWithMutation query={TASKS_QUERY} />
185+
<TasksWithMutation />
167186
</Suspense>
168187
</ApolloProvider>
169188
);
@@ -174,41 +193,47 @@ it('should create a function to perform mutations', async () => {
174193
flushEffects();
175194
await waitForNextTick();
176195

177-
const firstCheckbox = container.querySelector('input:checked');
196+
const firstCheckbox = container.querySelector<HTMLInputElement>(
197+
'input:checked'
198+
)!;
178199
expect(firstCheckbox.checked).toBeTruthy();
179200

180201
fireEvent.click(firstCheckbox);
181202
await waitForNextTick();
182203
flushEffects();
183204

184-
expect(container.querySelector('input').checked).toBeFalsy();
205+
expect(container.querySelector('input')!.checked).toBeFalsy();
185206
});
186207

187208
it('should allow to pass options forwarded to the mutation', async () => {
188209
function TasksWithMutation() {
189210
const { data, error } = useQuery(TASKS_QUERY);
190-
const addTask = useMutation(ADD_TASK_MUTATION, {
191-
update: (proxy, mutationResult) => {
192-
const previousData = proxy.readQuery({ query: TASKS_QUERY });
193-
previousData.tasks.push(mutationResult.data.addTask);
194-
proxy.writeQuery({ data: previousData, query: TASKS_QUERY });
195-
},
196-
variables: {
197-
input: {
198-
text: 'Learn Jest',
211+
const addTask = useMutation<any, { input: Partial<TaskFragment> }>(
212+
ADD_TASK_MUTATION,
213+
{
214+
update: (proxy, mutationResult) => {
215+
const previousData = proxy.readQuery<{ tasks: TaskFragment[] }>({
216+
query: TASKS_QUERY,
217+
});
218+
previousData!.tasks.push(mutationResult!.data!.addTask);
219+
proxy.writeQuery({ data: previousData, query: TASKS_QUERY });
199220
},
200-
},
201-
});
202-
const onChange = () => {};
221+
variables: {
222+
input: {
223+
text: 'Learn Jest',
224+
},
225+
},
226+
}
227+
);
203228

204229
if (error) {
205230
throw error;
206231
}
207232

208233
return (
209234
<>
210-
<TaskList onChange={onChange} tasks={data.tasks} />
211-
<button data-testid="add-task-button" onClick={addTask}>
235+
<TaskList onChange={noop} tasks={data.tasks} />
236+
<button data-testid="add-task-button" onClick={() => addTask()}>
212237
Add new task
213238
</button>
214239
</>
@@ -219,7 +244,7 @@ it('should allow to pass options forwarded to the mutation', async () => {
219244
const { container, getByTestId } = render(
220245
<ApolloProvider client={client}>
221246
<Suspense fallback={<div>Loading</div>}>
222-
<TasksWithMutation query={TASKS_QUERY} />
247+
<TasksWithMutation />
223248
</Suspense>
224249
</ApolloProvider>
225250
);

0 commit comments

Comments
 (0)