Skip to content

Commit

Permalink
added cluster tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanWright committed Jun 9, 2016
1 parent baab2d8 commit 9a75bdd
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 51 deletions.
46 changes: 22 additions & 24 deletions src/redux/actions/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,23 @@ export function fetchCluster(id) {
}

export function fetchClusters(type) {
const action = netActions.addNetworkCall('fetch_clusters', 'Retreive clusters');
dispatch(pendingNetworkCall(true));
client.listClusters(type)
.then(
resp => {
dispatch(netActions.successNetworkCall(action.id, resp));
dispatch(updateClusters(resp.data));
dispatch(pendingNetworkCall(false));
},
err => {
dispatch(netActions.errorNetworkCall(action.id, err));
dispatch(pendingNetworkCall(false));
});
return dispatch => {
const action = netActions.addNetworkCall('fetch_clusters', 'Retreive clusters');
dispatch(pendingNetworkCall(true));
client.listClusters(type)
.then(
resp => {
dispatch(netActions.successNetworkCall(action.id, resp));
dispatch(updateClusters(resp.data));
dispatch(pendingNetworkCall(false));
},
err => {
dispatch(netActions.errorNetworkCall(action.id, err));
dispatch(pendingNetworkCall(false));
});

return action;
return action;
};
}

export function fetchClusterPresets() {
Expand All @@ -190,7 +192,7 @@ export function fetchClusterPresets() {
};
}

// removes a cluster from the preferences page
// removes a cluster from the preferences page and deletes it if has _id
export function removeCluster(index, cluster) {
return dispatch => {
if (cluster._id) {
Expand Down Expand Up @@ -276,16 +278,14 @@ export function updateCluster(cluster) {
}

export function testCluster(index, cluster) {
if (!cluster._id) {
return { type: 'NOOP', message: 'Cluster is not available on server yet' };
}
return dispatch => {
if (!cluster._id) {
return { type: 'NO_OP', message: 'Cluster is not available on server yet' };
}

const action = netActions.addNetworkCall('test_cluster', 'Test cluster');
dispatch(pendingNetworkCall(true));
dispatch(action);

client.testCluster(cluster._id)
dispatch({ type: TESTING_CLUSTER, index });
return client.testCluster(cluster._id)
.then(
resp => {
dispatch(netActions.successNetworkCall(action.id, resp));
Expand All @@ -296,8 +296,6 @@ export function testCluster(index, cluster) {
dispatch(netActions.errorNetworkCall(action.id, error));
dispatch(pendingNetworkCall(false));
});

return { type: TESTING_CLUSTER, index };
};
}

Expand Down
8 changes: 8 additions & 0 deletions test/helpers/complete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function (done) {
return (err) => {
if (err) {
done.fail(err);
}
done();
};
}
2 changes: 1 addition & 1 deletion test/karma.redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function(config) {
webpack: wpConfig,
files: [
'../node_modules/babel-polyfill/dist/polyfill.js',
'tests.webpack.js',
'tests.webpack.js'
],
});
};
168 changes: 168 additions & 0 deletions test/redux/clusters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import * as Actions from '../../src/redux/actions/clusters';
import clustersReducer, { initialState } from '../../src/redux/reducers/clusters';
import client from '../../src/network';
import * as ClusterHelpers from '../../src/network/helpers/clusters';

// import tradCluster from '../sampleData/projectsData';
// import ec2Cluster from '../sampleData/simulationsForProj1';

import expect from 'expect';
import thunk from 'redux-thunk';
import complete from '../helpers/complete';
import { registerMiddlewares } from 'redux-actions-assertions';
import { registerAssertions } from 'redux-actions-assertions/expect';
/* global describe it afterEach */

registerMiddlewares([thunk]);
registerAssertions();

function setSpy(target, method, data) {
expect.spyOn(target, method)
.andReturn(Promise.resolve({ data }));
}

describe('cluster actions', () => {
describe('simple actions', () => {
it('should add a fresh cluster', (done) => {
const expectedAction = { type: Actions.ADD_CLUSTER };
expect(Actions.addCluster())
.toDispatchActions(expectedAction, complete(done));
});

// FIXME, needs sample data
it('should add an existing cluster', (done) => {
const expectedAction = { type: Actions.ADD_EXISTING_CLUSTER, cluster: { a: 1 } };
expect(Actions.addExistingCluster({ a: 1 }))
.toDispatchActions(expectedAction, complete(done));
});

it('should apply cluster preset', (done) => {
const expectedAction = { type: Actions.CLUSTER_APPLY_PRESET, index: 0, name: 'myCluster' };
expect(Actions.applyPreset(0, 'myCluster'))
.toDispatchActions(expectedAction, complete(done));
});

// FIXME, needs sample data
it('should remove cluster given an id', (done) => {
const expectedAction = { type: Actions.REMOVE_CLUSTER_BY_ID, id: 'a1' };
expect(Actions.removeClusterById('a1'))
.toDispatchActions(expectedAction, complete(done));
});

it('update the active cluster in a list', (done) => {
const expectedAction = { type: Actions.UPDATE_ACTIVE_CLUSTER, index: 0 };
expect(Actions.updateActiveCluster(0))
.toDispatchActions(expectedAction, complete(done));
});

// FIXME, needs sample data
it('update an existing cluster', (done) => {
const expectedAction = { type: Actions.UPDATE_EXISTING_CLUSTER, cluster: { a: 1 } };
expect(Actions.updateExistingCluster({ a: 1 }))
.toDispatchActions(expectedAction, complete(done));
});

it('should update the list of clusters', (done) => {
const clusters = [{ _id: 'a1' }, { _id: 'b2' }];
const expectedAction = { type: Actions.UPDATE_CLUSTERS, clusters };
expect(Actions.updateClusters(clusters))
.toDispatchActions(expectedAction, complete(done));
});

it('should update cluster presets', (done) => {
const presets = [{ name: 'myCluster' }, { name: 'myOtherCluster' }];
const expectedAction = { type: Actions.UPDATE_CLUSTER_PRESETS, presets };
expect(Actions.updateClusterPresets(presets))
.toDispatchActions(expectedAction, complete(done));
});

// FIXME, needs sample data
it('should update a cluster\' log', (done) => {
const log = [{ entry: 'created ...' }];
const expectedAction = { type: Actions.UPDATE_CLUSTER_LOG, id: 'a1', log };
expect(Actions.updateClusterLog('a1', log))
.toDispatchActions(expectedAction, complete(done));
});

// we only test the reducer here
it('should update a cluster\'s status', () => {
const thisState = Object.assign({}, initialState);
thisState.mapById = { a1: { status: 'created' } };
const action = { type: Actions.UPDATE_CLUSTER_STATUS, id: 'a1', status: 'terminated' };
expect(clustersReducer(thisState, action).mapById.a1.status)
.toEqual('terminated');
});
});

describe('async actions', () => {
afterEach(() => {
expect.restoreSpies();
});

it('should fetch cluster log', (done) => {
const id = 'a1';
const log = [{ entry: 'one' }];
setSpy(client, 'getClusterLog', { log });
expect(Actions.getClusterLog(id))
.toDispatchActions({ type: Actions.UPDATE_CLUSTER_LOG, id, log }, complete(done));
});

it('should fetch a cluster', (done) => {
const expectedAction = { type: Actions.ADD_EXISTING_CLUSTER, cluster: { _id: 'myCluster' } };
setSpy(client, 'getCluster', expectedAction.cluster);
expect(Actions.fetchCluster(expectedAction._id))
.toDispatchActions(expectedAction, complete(done));
});

it('should fetch a list of clusters', (done) => {
const clusters = [{ _id: 'a1' }, { _id: 'b2' }];
const expectedAction = { type: Actions.UPDATE_CLUSTERS, clusters };
setSpy(client, 'listClusters', clusters);
expect(Actions.fetchClusters())
.toDispatchActions(expectedAction, complete(done));
});

// removes a cluster from the preferences list and deletes it if has _id
it('should delete a cluster', (done) => {
const expectedAction = { type: Actions.REMOVE_CLUSTER_BY_ID, id: 'a1' };
setSpy(client, 'deleteCluster', null);
expect(Actions.deleteCluster('a1'))
.toDispatchActions(expectedAction, complete(done));
});

it('should save a cluster', (done) => {
const expectedAction = { type: Actions.SAVE_CLUSTER, index: 0, cluster: { _id: 'a1' } };
setSpy(ClusterHelpers, 'saveCluster', true);
expect(Actions.saveCluster(0, { _id: 'a1' }, false))
.toDispatchActions(expectedAction, complete(done));
});

it('should update a cluster', (done) => {
const cluster = { _id: 'a1' };
const expectedAction = { type: Actions.UPDATE_EXISTING_CLUSTER, cluster };
setSpy(client, 'updateCluster', cluster);
expect(Actions.updateCluster(cluster))
.toDispatchActions(expectedAction, complete(done));
});

it('should not test a cluster without an id', (done) => {
const expectedAction = { type: 'NOOP', message: 'Cluster is not available on server yet' };
expect(Actions.testCluster(0, {}))
.toDispatchActions(expectedAction, complete(done));
});

it('should test a cluster', (done) => {
const expectedAction = { type: Actions.TESTING_CLUSTER, index: 0 };
setSpy(client, 'testCluster', null);
expect(Actions.testCluster(0, { _id: 'a1' }))
.toDispatchActions(expectedAction, complete(done));
});

it('should terminate a cluster', (done) => {
const expectedAction = { type: 'NOOP' };
setSpy(client, 'terminateCluster', null);
expect(Actions.terminateCluster('a1'))
.toDispatchActions(expectedAction, complete(done));
});
});
});
37 changes: 11 additions & 26 deletions test/redux/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe('project actions', () => {
// ----------------------------------------------------------------------------
describe('basic actions', () => {
const newState = Object.assign({}, initialState);
it('should update a project list', () => {
it('should update a project list', (done) => {
const expectedAction = { type: Actions.UPDATE_PROJECT_LIST, projects: projectsData };

expect(Actions.updateProjectList(projectsData))
.toDispatchActions(expectedAction);
.toDispatchActions(expectedAction, complete(done));

newState.list = projectsData.map((el) => el._id);
projectsData.forEach((el) => {
Expand All @@ -42,26 +42,26 @@ describe('project actions', () => {
.toEqual(newState);
});

it('should update project simulations', () => {
it('should update project simulations', (done) => {
const id = projectsData[0]._id;
const expectedAction = { type: Actions.UPDATE_PROJECT_SIMULATIONS, id, simulations: simulationData };
expect(Actions.updateProjectSimulations(id, simulationData))
.toDispatchActions(expectedAction);
.toDispatchActions(expectedAction, complete(done));

expect(Object.keys(projectsReducer(initialState, expectedAction).simulations[id]).length)
.toEqual(simulationData.length);
});

it('should update project data', () => {
it('should update project data', (done) => {
const project = projectsData[0];
expect(Actions.updateProject(project))
.toDispatchActions({ type: Actions.UPDATE_PROJECT, project });
.toDispatchActions({ type: Actions.UPDATE_PROJECT, project }, complete(done));
});

it('should update simulation data', () => {
it('should update simulation data', (done) => {
const simulation = simulationData[0];
expect(Actions.updateSimulation(simulation))
.toDispatchActions({ type: Actions.UPDATE_SIMULATION, simulation });
.toDispatchActions({ type: Actions.UPDATE_SIMULATION, simulation }, complete(done));
});
});

Expand All @@ -76,8 +76,7 @@ describe('project actions', () => {
it('should get projects', (done) => {
setSpy(client, 'listProjects', projectsData);
expect(Actions.fetchProjectList())
.toDispatchActions({ type: Actions.UPDATE_PROJECT_LIST, projects: projectsData }, complete(done));
expect(client.listProjects).toHaveBeenCalled();
.toDispatchActions({ type: Actions.UPDATE_PROJECT_LIST }, complete(done));
});

const id = projectsData[0]._id;
Expand Down Expand Up @@ -108,29 +107,15 @@ describe('project actions', () => {

describe('simulation actions', () => {
describe('simple actions', () => {
it('should update Simulation', () => {
it('should update Simulation', (done) => {
const projId = projectsData[0]._id;
const expectedAction = { type: Actions.UPDATE_SIMULATION, simulation: simulationData[0] };
expect(Actions.updateSimulation(simulationData[0]))
.toDispatchActions(expectedAction);
.toDispatchActions(expectedAction, complete(done));

expect(projectsReducer(initialState, expectedAction).simulations[projId].list)
.toContain(simulationData[0]._id);
});

// setActiveSimulation
it('should set active Simulation', () => {
const simId = simulationData[0]._id;
const expectedAction = { type: Actions.UPDATE_ACTIVE_SIMULATION, id: simId };
expect(Actions.setActiveSimulation(simId))
.toDispatchActions(expectedAction);

// if the new active simulation isn't in the list
const newState = Object.assign({}, initialState);
newState.pendingActiveSimulation = simId;
expect(projectsReducer(initialState, expectedAction))
.toEqual(newState);
});
});

describe('async actions', () => {
Expand Down

0 comments on commit 9a75bdd

Please sign in to comment.