Skip to content

Commit f54fe46

Browse files
committed
Fix a bunch of type errors in the tests
Former-commit-id: 66482dd
1 parent 8f13b27 commit f54fe46

8 files changed

+63
-34
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
"format:check": "prettier --list-different \"{src,test}/**/*.{js,ts}\" \"**/*.md\"",
4141
"lint": "eslint --ext js,ts src test",
4242
"check-types": "tsc --noEmit",
43-
"test": "jest",
43+
"test": "jest && tsc -p test/typescript",
4444
"test:types": "tsc -p test/typescript",
45-
"test:watch": "npm test -- --watch",
46-
"test:cov": "npm test -- --coverage",
45+
"test:watch": "jest --watch",
46+
"test:cov": "jest --coverage",
4747
"build": "rollup -c",
4848
"pretest": "npm run build",
4949
"prepublishOnly": "npm run clean && npm run check-types && npm run format:check && npm run lint && npm test",

test/applyMiddleware.spec.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
import { createStore, applyMiddleware, Middleware, AnyAction, Action } from '..'
1+
import {
2+
createStore,
3+
applyMiddleware,
4+
Middleware,
5+
MiddlewareAPI,
6+
AnyAction,
7+
Action,
8+
Store,
9+
Dispatch
10+
} from '..'
211
import * as reducers from './helpers/reducers'
312
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
413
import { thunk } from './helpers/middleware'
514

615
describe('applyMiddleware', () => {
716
it('warns when dispatching during middleware setup', () => {
8-
function dispatchingMiddleware(store) {
9-
store.dispatch(addTodo('Dont dispatch in middleware setup'))
10-
return next => action => next(action)
17+
function dispatchingMiddleware(store: Store) {
18+
store.dispatch(addTodo("Don't dispatch in middleware setup"))
19+
return (next: Dispatch) => (action: Action) => next(action)
1120
}
1221

1322
expect(() =>
14-
applyMiddleware(dispatchingMiddleware)(createStore)(reducers.todos)
23+
applyMiddleware(dispatchingMiddleware as Middleware)(createStore)(
24+
reducers.todos
25+
)
1526
).toThrow()
1627
})
1728

1829
it('wraps dispatch method with middleware once', () => {
19-
function test(spyOnMethods) {
20-
return methods => {
30+
function test(spyOnMethods: any) {
31+
return (methods: any) => {
2132
spyOnMethods(methods)
22-
return next => action => next(action)
33+
return (next: Dispatch) => (action: Action) => next(action)
2334
}
2435
}
2536

@@ -41,8 +52,8 @@ describe('applyMiddleware', () => {
4152
})
4253

4354
it('passes recursive dispatches through the middleware chain', () => {
44-
function test(spyOnMethods) {
45-
return () => next => action => {
55+
function test(spyOnMethods: any) {
56+
return () => (next: Dispatch) => (action: Action) => {
4657
spyOnMethods(action)
4758
return next(action)
4859
}
@@ -53,7 +64,7 @@ describe('applyMiddleware', () => {
5364

5465
// the typing for redux-thunk is super complex, so we will use an as unknown hack
5566
const dispatchedValue = store.dispatch(
56-
addTodoAsync('Use Redux')
67+
addTodoAsync('Use Redux') as any
5768
) as unknown as Promise<void>
5869
return dispatchedValue.then(() => {
5970
expect(spy.mock.calls.length).toEqual(2)
@@ -63,15 +74,15 @@ describe('applyMiddleware', () => {
6374
it('works with thunk middleware', done => {
6475
const store = applyMiddleware(thunk)(createStore)(reducers.todos)
6576

66-
store.dispatch(addTodoIfEmpty('Hello'))
77+
store.dispatch(addTodoIfEmpty('Hello') as any)
6778
expect(store.getState()).toEqual([
6879
{
6980
id: 1,
7081
text: 'Hello'
7182
}
7283
])
7384

74-
store.dispatch(addTodoIfEmpty('Hello'))
85+
store.dispatch(addTodoIfEmpty('Hello') as any)
7586
expect(store.getState()).toEqual([
7687
{
7788
id: 1,
@@ -93,7 +104,7 @@ describe('applyMiddleware', () => {
93104

94105
// the typing for redux-thunk is super complex, so we will use an "as unknown" hack
95106
const dispatchedValue = store.dispatch(
96-
addTodoAsync('Maybe')
107+
addTodoAsync('Maybe') as any
97108
) as unknown as Promise<void>
98109
dispatchedValue.then(() => {
99110
expect(store.getState()).toEqual([
@@ -124,24 +135,25 @@ describe('applyMiddleware', () => {
124135

125136
const multiArgMiddleware: Middleware<MultiDispatch, any, MultiDispatch> =
126137
_store => {
127-
return next => (action, callArgs?: any) => {
138+
return next => (action: any, callArgs?: any) => {
128139
if (Array.isArray(callArgs)) {
129140
return action(...callArgs)
130141
}
131142
return next(action)
132143
}
133144
}
134145

135-
function dummyMiddleware({ dispatch }) {
136-
return _next => action => dispatch(action, testCallArgs)
146+
function dummyMiddleware({ dispatch }: MiddlewareAPI) {
147+
return (_next: Dispatch) => (action: Action) =>
148+
dispatch(action, testCallArgs)
137149
}
138150

139151
const store = createStore(
140152
reducers.todos,
141153
applyMiddleware(multiArgMiddleware, dummyMiddleware)
142154
)
143155

144-
store.dispatch(spy)
156+
store.dispatch(spy as any)
145157
expect(spy.mock.calls[0]).toEqual(testCallArgs)
146158
})
147159
})

test/bindActionCreators.spec.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { bindActionCreators, createStore, ActionCreator } from '..'
1+
import { bindActionCreators, createStore, ActionCreator, Store } from '..'
22
import { todos } from './helpers/reducers'
33
import * as actionCreators from './helpers/actionCreators'
44

55
describe('bindActionCreators', () => {
6-
let store
7-
let actionCreatorFunctions
6+
let store: Store
7+
let actionCreatorFunctions: any
88

99
beforeEach(() => {
1010
store = createStore(todos)
@@ -33,13 +33,13 @@ describe('bindActionCreators', () => {
3333
it('wraps action creators transparently', () => {
3434
const uniqueThis = {}
3535
const argArray = [1, 2, 3]
36-
function actionCreator() {
36+
function actionCreator(this: any) {
3737
return { type: 'UNKNOWN_ACTION', this: this, args: [...arguments] }
3838
}
3939
const boundActionCreator = bindActionCreators(actionCreator, store.dispatch)
4040

41-
const boundAction = boundActionCreator.apply(uniqueThis, argArray)
42-
const action = actionCreator.apply(uniqueThis, argArray)
41+
const boundAction = boundActionCreator.apply(uniqueThis, argArray as [])
42+
const action = actionCreator.apply(uniqueThis, argArray as [])
4343
expect(boundAction).toEqual(action)
4444
expect(boundAction.this).toBe(uniqueThis)
4545
expect(action.this).toBe(uniqueThis)
@@ -75,6 +75,7 @@ describe('bindActionCreators', () => {
7575

7676
it('throws for an undefined actionCreator', () => {
7777
expect(() => {
78+
// @ts-expect-error
7879
bindActionCreators(undefined, store.dispatch)
7980
}).toThrow(
8081
`bindActionCreators expected an object or a function, but instead received: 'undefined'. ` +
@@ -84,6 +85,7 @@ describe('bindActionCreators', () => {
8485

8586
it('throws for a null actionCreator', () => {
8687
expect(() => {
88+
// @ts-expect-error
8789
bindActionCreators(null, store.dispatch)
8890
}).toThrow(
8991
`bindActionCreators expected an object or a function, but instead received: 'null'. ` +
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
74a67c7ab000cd7cee3655199bd2ca5834085126
1+
b012ff5cffff432eca8c941c7dd6b9e6e2ef5d3d

test/compose.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Utils', () => {
2929
expect(() =>
3030
compose(square, add, false as unknown as sFunc)(1, 2)
3131
).toThrow()
32+
// @ts-expect-error
3233
expect(() => compose(square, add, undefined)(1, 2)).toThrow()
3334
expect(() =>
3435
compose(square, add, true as unknown as sFunc)(1, 2)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d104d41480eb4865939c7831851c3ada6cdadc22
1+
f8065aeb618d36e1b5344bd7e730e2450f1dee93

test/tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"allowSyntheticDefaultImports": true,
5+
"esModuleInterop": true,
6+
"module": "esnext",
7+
"moduleResolution": "node",
8+
"emitDeclarationOnly": false,
9+
"strict": true,
10+
"noEmit": true,
11+
"target": "esnext",
12+
"jsx": "react",
13+
"baseUrl": ".",
14+
"skipLibCheck": true,
15+
"noImplicitReturns": false,
16+
"noUnusedLocals": false
17+
},
18+
"include": ["**/*.spec.ts"]
19+
}

test/utils/isPlainObject.spec.ts

-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import vm from 'vm'
44

55
describe('isPlainObject', () => {
66
it('returns true only if plain object', () => {
7-
function Test() {
8-
this.prop = 1
9-
}
10-
117
const sandbox = { fromAnotherRealm: false }
128
vm.runInNewContext('fromAnotherRealm = {}', sandbox)
139

1410
expect(isPlainObject(sandbox.fromAnotherRealm)).toBe(true)
15-
expect(isPlainObject(new Test())).toBe(false)
1611
expect(isPlainObject(new Date())).toBe(false)
1712
expect(isPlainObject([1, 2, 3])).toBe(false)
1813
expect(isPlainObject(null)).toBe(false)

0 commit comments

Comments
 (0)