-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c477613
commit cfd2844
Showing
7 changed files
with
101 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,106 @@ | ||
import thunkMiddleware from '../src/index'; | ||
import thunkMiddleware from '../src/index' | ||
|
||
describe('thunk middleware', () => { | ||
const doDispatch = () => {}; | ||
const doGetState = () => 42; | ||
const doDispatch = () => {} | ||
const doGetState = () => 42 | ||
const nextHandler = thunkMiddleware({ | ||
dispatch: doDispatch, | ||
getState: doGetState, | ||
}); | ||
getState: doGetState | ||
}) | ||
|
||
it('must return a function to handle next', () => { | ||
expect(nextHandler).toBeInstanceOf(Function) | ||
expect(nextHandler.length).toBe(1) | ||
}); | ||
}) | ||
|
||
describe('handle next', () => { | ||
it('must return a function to handle action', () => { | ||
// @ts-ignore | ||
const actionHandler = nextHandler(); | ||
const actionHandler = nextHandler() | ||
|
||
expect(actionHandler).toBeInstanceOf(Function) | ||
expect(actionHandler.length).toBe(1) | ||
}); | ||
}) | ||
|
||
describe('handle action', () => { | ||
it('must run the given action function with dispatch and getState', (done) => { | ||
it('must run the given action function with dispatch and getState', done => { | ||
// @ts-ignore | ||
const actionHandler = nextHandler(); | ||
const actionHandler = nextHandler() | ||
|
||
actionHandler((dispatch: any, getState: any) => { | ||
expect(dispatch).toBe(doDispatch) | ||
expect(getState).toBe(doGetState) | ||
done(); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
|
||
it('must pass action to next if not a function', (done) => { | ||
const actionObj = {}; | ||
it('must pass action to next if not a function', done => { | ||
const actionObj = {} | ||
|
||
// @ts-ignore | ||
const actionHandler = nextHandler((action) => { | ||
const actionHandler = nextHandler(action => { | ||
expect(action).toBe(actionObj) | ||
done(); | ||
}); | ||
done() | ||
}) | ||
|
||
actionHandler(actionObj); | ||
}); | ||
actionHandler(actionObj) | ||
}) | ||
|
||
it('must return the return value of next if not a function', () => { | ||
const expected = 'redux'; | ||
const expected = 'redux' | ||
// @ts-ignore | ||
const actionHandler = nextHandler(() => expected); | ||
const actionHandler = nextHandler(() => expected) | ||
|
||
// @ts-ignore | ||
const outcome = actionHandler(); | ||
const outcome = actionHandler() | ||
expect(outcome).toBe(expected) | ||
}); | ||
}) | ||
|
||
it('must return value as expected if a function', () => { | ||
const expected = 'rocks'; | ||
const expected = 'rocks' | ||
// @ts-ignore | ||
const actionHandler = nextHandler(); | ||
const actionHandler = nextHandler() | ||
|
||
const outcome = actionHandler(() => expected); | ||
const outcome = actionHandler(() => expected) | ||
expect(outcome).toBe(expected) | ||
}); | ||
}) | ||
|
||
it('must be invoked synchronously if a function', () => { | ||
// @ts-ignore | ||
const actionHandler = nextHandler(); | ||
let mutated = 0; | ||
const actionHandler = nextHandler() | ||
let mutated = 0 | ||
|
||
// eslint-disable-next-line no-plusplus | ||
actionHandler(() => mutated++); | ||
actionHandler(() => mutated++) | ||
expect(mutated).toBe(1) | ||
}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('handle errors', () => { | ||
it('must throw if argument is non-object', (done) => { | ||
it('must throw if argument is non-object', done => { | ||
try { | ||
// @ts-expect-error | ||
thunkMiddleware(); | ||
thunkMiddleware() | ||
} catch (err) { | ||
done(); | ||
done() | ||
} | ||
}); | ||
}); | ||
}) | ||
}) | ||
|
||
describe('withExtraArgument', () => { | ||
it('must pass the third argument', (done) => { | ||
const extraArg = { lol: true }; | ||
it('must pass the third argument', done => { | ||
const extraArg = { lol: true } | ||
// @ts-ignore | ||
thunkMiddleware.withExtraArgument(extraArg)({ | ||
dispatch: doDispatch, | ||
getState: doGetState, | ||
getState: doGetState | ||
})()((dispatch: any, getState: any, arg: any) => { | ||
expect(dispatch).toBe(doDispatch) | ||
expect(getState).toBe(doGetState) | ||
expect(arg).toBe(extraArg) | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters