Skip to content

Commit

Permalink
Convert test file to TS and Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Oct 24, 2021
1 parent 0f02b48 commit 14e6cdc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module.exports = {
testEnvironment: 'node',
coverageDirectory: './coverage/',
collectCoverage: true,
testRegex: '(/test/.*(js|ts))$',
testRegex: 'test/test.ts',
globals: {
'ts-jest': {
tsconfig: './test/tsconfig.json'
}
}
}
tsconfig: './test/tsconfig.json',
},
},
};
51 changes: 25 additions & 26 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import chai from 'chai';
import { checkDirectory } from 'typings-tester';
import thunkMiddleware from '../src/index';

describe('thunk middleware', () => {
const doDispatch = () => {};
const doGetState = () => {};
const doGetState = () => 42;
const nextHandler = thunkMiddleware({
dispatch: doDispatch,
getState: doGetState,
});

it('must return a function to handle next', () => {
chai.assert.isFunction(nextHandler);
chai.assert.strictEqual(nextHandler.length, 1);
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();

chai.assert.isFunction(actionHandler);
chai.assert.strictEqual(actionHandler.length, 1);
expect(actionHandler).toBeInstanceOf(Function)
expect(actionHandler.length).toBe(1)
});

describe('handle action', () => {
it('must run the given action function with dispatch and getState', (done) => {
// @ts-ignore
const actionHandler = nextHandler();

actionHandler((dispatch, getState) => {
chai.assert.strictEqual(dispatch, doDispatch);
chai.assert.strictEqual(getState, doGetState);
actionHandler((dispatch: any, getState: any) => {
expect(dispatch).toBe(doDispatch)
expect(getState).toBe(doGetState)
done();
});
});

it('must pass action to next if not a function', (done) => {
const actionObj = {};

// @ts-ignore
const actionHandler = nextHandler((action) => {
chai.assert.strictEqual(action, actionObj);
expect(action).toBe(actionObj)
done();
});

Expand All @@ -47,34 +48,39 @@ describe('thunk middleware', () => {

it('must return the return value of next if not a function', () => {
const expected = 'redux';
// @ts-ignore
const actionHandler = nextHandler(() => expected);

// @ts-ignore
const outcome = actionHandler();
chai.assert.strictEqual(outcome, expected);
expect(outcome).toBe(expected)
});

it('must return value as expected if a function', () => {
const expected = 'rocks';
// @ts-ignore
const actionHandler = nextHandler();

const outcome = actionHandler(() => expected);
chai.assert.strictEqual(outcome, expected);
expect(outcome).toBe(expected)
});

it('must be invoked synchronously if a function', () => {
// @ts-ignore
const actionHandler = nextHandler();
let mutated = 0;

// eslint-disable-next-line no-plusplus
actionHandler(() => mutated++);
chai.assert.strictEqual(mutated, 1);
expect(mutated).toBe(1)
});
});
});

describe('handle errors', () => {
it('must throw if argument is non-object', (done) => {
try {
// @ts-expect-error
thunkMiddleware();
} catch (err) {
done();
Expand All @@ -85,23 +91,16 @@ describe('thunk middleware', () => {
describe('withExtraArgument', () => {
it('must pass the third argument', (done) => {
const extraArg = { lol: true };
// @ts-ignore
thunkMiddleware.withExtraArgument(extraArg)({
dispatch: doDispatch,
getState: doGetState,
})()((dispatch, getState, arg) => {
chai.assert.strictEqual(dispatch, doDispatch);
chai.assert.strictEqual(getState, doGetState);
chai.assert.strictEqual(arg, extraArg);
})()((dispatch: any, getState: any, arg: any) => {
expect(dispatch).toBe(doDispatch)
expect(getState).toBe(doGetState)
expect(arg).toBe(extraArg)
done();
});
});
});

describe('TypeScript definitions', function test() {
this.timeout(0);

it('should compile against index.d.ts', () => {
checkDirectory(__dirname);
});
});
});
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"strict": true,
Expand Down

0 comments on commit 14e6cdc

Please sign in to comment.