Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Nov 29, 2018
1 parent 646893a commit 73dd0fd
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/__mocks__/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const storage = {
set: (key, value) => {
storeMap.set(key, value);
},
__reset
__reset,
__clear: () => {
storeMap.clear();
}
};

export default storage;
3 changes: 0 additions & 3 deletions app/reducers/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ const entities = (state = {}, action: Action) => {
};
case JOB_CLOSE:
case JOB_EXIT:
if (!state[action.job.id].running) {
return state;
}
return {
...state,
[action.job.id]: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"collectCoverage": true,
"collectCoverageFrom": [
"app/actions/*.js",
"app/reducers/*.js"
"app/reducers/*.js",
"app/utils/*.js"
]
},
"devDependencies": {
Expand Down
63 changes: 63 additions & 0 deletions test/actions/jobs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ describe('jobs actions', () => {
removeTaskByJobId(2);
});

it('create addJob action when job is empty', () => {
// eslint-disable-next-line no-underscore-dangle
storage.__clear();
const fn = actions.addJob({
name: 'test job for addJob action',
cmd: 'ls -l',
cwd: '/tmp',
subPackageDir: 'sub/pkg'
});
expect(fn).toBeInstanceOf(Function);

const dispatch = spy();
fn(dispatch);

expect(
dispatch.calledWith({
type: actions.ADD_JOB,
job: {
id: 1,
name: 'test job for addJob action',
cmd: 'ls -l',
cwd: '/tmp',
subPackageDir: 'sub/pkg'
}
})
).toBe(true);
});

it('create addJob action', () => {
const fn = actions.addJob({
name: 'test job for addJob action',
Expand Down Expand Up @@ -131,6 +159,23 @@ describe('jobs actions', () => {
).toBe(true);
});

it('do nothing if apply startJob action to an already started job', () => {
const job = {
id: 2,
name: 'test job for startJob action',
cmd: 'ls -l',
cwd: '/tmp',
starting: true
};
const fn = actions.startJob(job);
expect(fn).toBeInstanceOf(Function);

const dispatch = spy();
fn(dispatch);

expect(dispatch.called).toBe(false);
});

it('create startJob action', () => {
const spawnEvent = new EventEmitter();
spawnEvent.stdout = new EventEmitter();
Expand Down Expand Up @@ -222,6 +267,24 @@ describe('jobs actions', () => {
sandbox.restore();
});

it('do nothing if apply stopJob action when task not found', () => {
const mockKill = sandbox.stub(process, 'kill');

const fn = actions.stopJob({
id: 3,
name: 'test job',
running: true
});
expect(fn).toBeInstanceOf(Function);

const dispatch = spy();
fn(dispatch);

expect(mockKill.called).toBe(false);

sandbox.restore();
});

it('create stopJob action', () => {
const mockKill = sandbox.stub(process, 'kill');

Expand Down
16 changes: 16 additions & 0 deletions test/reducers/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reducers root should handle initial state 1`] = `
Object {
"jobs": Object {
"active": null,
"entities": Object {},
"ids": Array [],
"outputs": Object {},
},
"router": Object {
"action": undefined,
"location": "/",
},
}
`;
9 changes: 9 additions & 0 deletions test/reducers/__snapshots__/jobs.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Object {
}
`;

exports[`reducers jobs should handle ACTIVATE_JOB again 1`] = `
Object {
"active": null,
"entities": Object {},
"ids": Array [],
"outputs": Object {},
}
`;

exports[`reducers jobs should handle ADD_JOB 1`] = `
Object {
"active": null,
Expand Down
16 changes: 16 additions & 0 deletions test/reducers/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/first */
jest.mock('../../app/storage');

import rootReducers from '../../app/reducers';

describe('reducers', () => {
describe('root', () => {
it('should handle initial state', () => {
expect(
rootReducers({
location: '/'
})(undefined, {})
).toMatchSnapshot();
});
});
});
31 changes: 26 additions & 5 deletions test/reducers/jobs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,33 @@ describe('reducers', () => {

it('should handle ACTIVATE_JOB', () => {
expect(
jobs(undefined, {
type: ACTIVATE_JOB,
job: {
id: 1
jobs(
{
active: null
},
{
type: ACTIVATE_JOB,
job: {
id: 1
}
}
})
)
).toMatchSnapshot();
});

it('should handle ACTIVATE_JOB again', () => {
expect(
jobs(
{
active: 1
},
{
type: ACTIVATE_JOB,
job: {
id: 1
}
}
)
).toMatchSnapshot();
});

Expand Down

0 comments on commit 73dd0fd

Please sign in to comment.