Skip to content

convert the createStore test to typescript - and run the tests #3510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
]
},
"jest": {
"testRegex": "(/test/.*\\.spec\\.js)$"
"testRegex": "(/test/.*\\.spec\\.[tj]s)$"
},
"sideEffects": false
}
85 changes: 56 additions & 29 deletions test/createStore.spec.js → test/createStore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createStore, combineReducers } from '../'
import {
createStore,
combineReducers,
Reducer,
StoreEnhancer,
Action,
Store
} from '..'
import {
addTodo,
dispatchInMiddle,
Expand All @@ -9,7 +16,7 @@ import {
unknownAction
} from './helpers/actionCreators'
import * as reducers from './helpers/reducers'
import { from } from 'rxjs'
import { from, ObservableInput } from 'rxjs'
import { map } from 'rxjs/operators'
import $$observable from 'symbol-observable'

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

it('throws if reducer is not a function', () => {
expect(() => createStore()).toThrow()
expect(() => createStore(undefined)).toThrow()

expect(() => createStore('test')).toThrow()
expect(() => createStore(('test' as unknown) as Reducer)).toThrow()

expect(() => createStore({})).toThrow()
expect(() => createStore(({} as unknown) as Reducer)).toThrow()

expect(() => createStore(() => {})).not.toThrow()
})
Expand Down Expand Up @@ -568,13 +575,23 @@ describe('createStore', () => {
})

it('throws if enhancer is neither undefined nor a function', () => {
expect(() => createStore(reducers.todos, undefined, {})).toThrow()
expect(() =>
createStore(reducers.todos, undefined, ({} as unknown) as StoreEnhancer)
).toThrow()

expect(() => createStore(reducers.todos, undefined, [])).toThrow()
expect(() =>
createStore(reducers.todos, undefined, ([] as unknown) as StoreEnhancer)
).toThrow()

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

expect(() => createStore(reducers.todos, undefined, false)).toThrow()
expect(() =>
createStore(
reducers.todos,
undefined,
(false as unknown) as StoreEnhancer
)
).toThrow()

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

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

expect(() => createStore(reducers.todos, {})).not.toThrow()
expect(() =>
createStore<{}, Action, {}, {}>(reducers.todos, {})
).not.toThrow()
})

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

expect(() => store.replaceReducer()).toThrow(
expect(() => store.replaceReducer(undefined)).toThrow(
'Expected the nextReducer to be a function.'
)

expect(() => store.replaceReducer(() => {})).not.toThrow()
expect(() => store.replaceReducer(() => [])).not.toThrow()
})

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

expect(() => store.subscribe()).toThrow()
expect(() => store.subscribe(undefined)).toThrow()

expect(() => store.subscribe('')).toThrow()
expect(() => store.subscribe(('' as unknown) as () => void)).toThrow()

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

Expand Down Expand Up @@ -654,11 +673,11 @@ describe('createStore', () => {
})

it('should pass an integration test with no unsubscribe', () => {
function foo(state = 0, action) {
function foo(state = 0, action: Action) {
return action.type === 'foo' ? 1 : state
}

function bar(state = 0, action) {
function bar(state = 0, action: Action) {
return action.type === 'bar' ? 2 : state
}

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

observable.subscribe({
next(state) {
next(state: any) {
results.push(state)
}
})
Expand All @@ -683,11 +702,11 @@ describe('createStore', () => {
})

it('should pass an integration test with an unsubscribe', () => {
function foo(state = 0, action) {
function foo(state = 0, action: Action) {
return action.type === 'foo' ? 1 : state
}

function bar(state = 0, action) {
function bar(state = 0, action: Action) {
return action.type === 'bar' ? 2 : state
}

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

const sub = observable.subscribe({
next(state) {
next(state: any) {
results.push(state)
}
})
Expand All @@ -709,25 +728,26 @@ describe('createStore', () => {
})

it('should pass an integration test with a common library (RxJS)', () => {
function foo(state = 0, action) {
function foo(state = 0, action: Action) {
return action.type === 'foo' ? 1 : state
}

function bar(state = 0, action) {
function bar(state = 0, action: Action) {
return action.type === 'bar' ? 2 : state
}

const store = createStore(combineReducers({ foo, bar }))
const store: ObservableInput<{ foo: number; bar: number }> = createStore(
combineReducers({ foo, bar })
)
const observable = from(store)
const results = []

const sub = observable
.pipe(map(state => ({ fromRx: true, ...state })))
.subscribe(state => results.push(state))

store.dispatch({ type: 'foo' })
;(store as Store).dispatch({ type: 'foo' })
sub.unsubscribe()
store.dispatch({ type: 'bar' })
;(store as Store).dispatch({ type: 'bar' })

expect(results).toEqual([
{ foo: 0, bar: 0, fromRx: true },
Expand Down Expand Up @@ -758,23 +778,30 @@ describe('createStore', () => {
})
)

expect(console.error.mock.calls.length).toBe(0)
expect((console.error as any).mock.calls.length).toBe(0)
console.error = originalConsoleError
})

it('throws if passing several enhancer functions without preloaded state', () => {
const rootReducer = combineReducers(reducers)
const dummyEnhancer = f => f
expect(() =>
createStore(rootReducer, dummyEnhancer, dummyEnhancer)
// use a fake pre-loaded state to get a valid createStore signature
createStore(rootReducer, (dummyEnhancer as unknown) as {}, dummyEnhancer)
).toThrow()
})

it('throws if passing several enhancer functions with preloaded state', () => {
const rootReducer = combineReducers(reducers)
const dummyEnhancer = f => f
const dummyEnhancer = (f: any) => f
expect(() =>
createStore(rootReducer, { todos: [] }, dummyEnhancer, dummyEnhancer)
// work around the type so we can call this poorly
(createStore as any)(
rootReducer,
{ todos: [] },
dummyEnhancer,
dummyEnhancer
)
).toThrow()
})
})