Skip to content

Commit e48c389

Browse files
cellogtimdorr
authored andcommitted
convert the createStore test to typescript - and run the tests (#3510)
1 parent f6a2122 commit e48c389

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
]
107107
},
108108
"jest": {
109-
"testRegex": "(/test/.*\\.spec\\.js)$"
109+
"testRegex": "(/test/.*\\.spec\\.[tj]s)$"
110110
},
111111
"sideEffects": false
112112
}

test/createStore.spec.js renamed to test/createStore.spec.ts

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { createStore, combineReducers } from '../'
1+
import {
2+
createStore,
3+
combineReducers,
4+
Reducer,
5+
StoreEnhancer,
6+
Action,
7+
Store
8+
} from '..'
29
import {
310
addTodo,
411
dispatchInMiddle,
@@ -9,7 +16,7 @@ import {
916
unknownAction
1017
} from './helpers/actionCreators'
1118
import * as reducers from './helpers/reducers'
12-
import { from } from 'rxjs'
19+
import { from, ObservableInput } from 'rxjs'
1320
import { map } from 'rxjs/operators'
1421
import $$observable from 'symbol-observable'
1522

@@ -26,11 +33,11 @@ describe('createStore', () => {
2633
})
2734

2835
it('throws if reducer is not a function', () => {
29-
expect(() => createStore()).toThrow()
36+
expect(() => createStore(undefined)).toThrow()
3037

31-
expect(() => createStore('test')).toThrow()
38+
expect(() => createStore(('test' as unknown) as Reducer)).toThrow()
3239

33-
expect(() => createStore({})).toThrow()
40+
expect(() => createStore(({} as unknown) as Reducer)).toThrow()
3441

3542
expect(() => createStore(() => {})).not.toThrow()
3643
})
@@ -568,13 +575,23 @@ describe('createStore', () => {
568575
})
569576

570577
it('throws if enhancer is neither undefined nor a function', () => {
571-
expect(() => createStore(reducers.todos, undefined, {})).toThrow()
578+
expect(() =>
579+
createStore(reducers.todos, undefined, ({} as unknown) as StoreEnhancer)
580+
).toThrow()
572581

573-
expect(() => createStore(reducers.todos, undefined, [])).toThrow()
582+
expect(() =>
583+
createStore(reducers.todos, undefined, ([] as unknown) as StoreEnhancer)
584+
).toThrow()
574585

575586
expect(() => createStore(reducers.todos, undefined, null)).toThrow()
576587

577-
expect(() => createStore(reducers.todos, undefined, false)).toThrow()
588+
expect(() =>
589+
createStore(
590+
reducers.todos,
591+
undefined,
592+
(false as unknown) as StoreEnhancer
593+
)
594+
).toThrow()
578595

579596
expect(() =>
580597
createStore(reducers.todos, undefined, undefined)
@@ -586,25 +603,27 @@ describe('createStore', () => {
586603

587604
expect(() => createStore(reducers.todos, [])).not.toThrow()
588605

589-
expect(() => createStore(reducers.todos, {})).not.toThrow()
606+
expect(() =>
607+
createStore<{}, Action, {}, {}>(reducers.todos, {})
608+
).not.toThrow()
590609
})
591610

592611
it('throws if nextReducer is not a function', () => {
593612
const store = createStore(reducers.todos)
594613

595-
expect(() => store.replaceReducer()).toThrow(
614+
expect(() => store.replaceReducer(undefined)).toThrow(
596615
'Expected the nextReducer to be a function.'
597616
)
598617

599-
expect(() => store.replaceReducer(() => {})).not.toThrow()
618+
expect(() => store.replaceReducer(() => [])).not.toThrow()
600619
})
601620

602621
it('throws if listener is not a function', () => {
603622
const store = createStore(reducers.todos)
604623

605-
expect(() => store.subscribe()).toThrow()
624+
expect(() => store.subscribe(undefined)).toThrow()
606625

607-
expect(() => store.subscribe('')).toThrow()
626+
expect(() => store.subscribe(('' as unknown) as () => void)).toThrow()
608627

609628
expect(() => store.subscribe(null)).toThrow()
610629

@@ -654,11 +673,11 @@ describe('createStore', () => {
654673
})
655674

656675
it('should pass an integration test with no unsubscribe', () => {
657-
function foo(state = 0, action) {
676+
function foo(state = 0, action: Action) {
658677
return action.type === 'foo' ? 1 : state
659678
}
660679

661-
function bar(state = 0, action) {
680+
function bar(state = 0, action: Action) {
662681
return action.type === 'bar' ? 2 : state
663682
}
664683

@@ -667,7 +686,7 @@ describe('createStore', () => {
667686
const results = []
668687

669688
observable.subscribe({
670-
next(state) {
689+
next(state: any) {
671690
results.push(state)
672691
}
673692
})
@@ -683,11 +702,11 @@ describe('createStore', () => {
683702
})
684703

685704
it('should pass an integration test with an unsubscribe', () => {
686-
function foo(state = 0, action) {
705+
function foo(state = 0, action: Action) {
687706
return action.type === 'foo' ? 1 : state
688707
}
689708

690-
function bar(state = 0, action) {
709+
function bar(state = 0, action: Action) {
691710
return action.type === 'bar' ? 2 : state
692711
}
693712

@@ -696,7 +715,7 @@ describe('createStore', () => {
696715
const results = []
697716

698717
const sub = observable.subscribe({
699-
next(state) {
718+
next(state: any) {
700719
results.push(state)
701720
}
702721
})
@@ -709,25 +728,26 @@ describe('createStore', () => {
709728
})
710729

711730
it('should pass an integration test with a common library (RxJS)', () => {
712-
function foo(state = 0, action) {
731+
function foo(state = 0, action: Action) {
713732
return action.type === 'foo' ? 1 : state
714733
}
715734

716-
function bar(state = 0, action) {
735+
function bar(state = 0, action: Action) {
717736
return action.type === 'bar' ? 2 : state
718737
}
719738

720-
const store = createStore(combineReducers({ foo, bar }))
739+
const store: ObservableInput<{ foo: number; bar: number }> = createStore(
740+
combineReducers({ foo, bar })
741+
)
721742
const observable = from(store)
722743
const results = []
723744

724745
const sub = observable
725746
.pipe(map(state => ({ fromRx: true, ...state })))
726747
.subscribe(state => results.push(state))
727-
728-
store.dispatch({ type: 'foo' })
748+
;(store as Store).dispatch({ type: 'foo' })
729749
sub.unsubscribe()
730-
store.dispatch({ type: 'bar' })
750+
;(store as Store).dispatch({ type: 'bar' })
731751

732752
expect(results).toEqual([
733753
{ foo: 0, bar: 0, fromRx: true },
@@ -758,23 +778,30 @@ describe('createStore', () => {
758778
})
759779
)
760780

761-
expect(console.error.mock.calls.length).toBe(0)
781+
expect((console.error as any).mock.calls.length).toBe(0)
762782
console.error = originalConsoleError
763783
})
764784

765785
it('throws if passing several enhancer functions without preloaded state', () => {
766786
const rootReducer = combineReducers(reducers)
767787
const dummyEnhancer = f => f
768788
expect(() =>
769-
createStore(rootReducer, dummyEnhancer, dummyEnhancer)
789+
// use a fake pre-loaded state to get a valid createStore signature
790+
createStore(rootReducer, (dummyEnhancer as unknown) as {}, dummyEnhancer)
770791
).toThrow()
771792
})
772793

773794
it('throws if passing several enhancer functions with preloaded state', () => {
774795
const rootReducer = combineReducers(reducers)
775-
const dummyEnhancer = f => f
796+
const dummyEnhancer = (f: any) => f
776797
expect(() =>
777-
createStore(rootReducer, { todos: [] }, dummyEnhancer, dummyEnhancer)
798+
// work around the type so we can call this poorly
799+
(createStore as any)(
800+
rootReducer,
801+
{ todos: [] },
802+
dummyEnhancer,
803+
dummyEnhancer
804+
)
778805
).toThrow()
779806
})
780807
})

0 commit comments

Comments
 (0)