Skip to content

Commit

Permalink
#39 Add training reducer and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
borjamaza committed Dec 9, 2016
1 parent 560f665 commit 06a4897
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/common/actionEnums/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

export const adminActionEnums = {
GET_SUMMARY_TRAINING_REQUEST_COMPLETED: 'GET_SUMMARY_TRAINING_REQUEST_COMPLETED',
GET_SUMMARY_TRAINING_REQUEST_COMPLETED: 'GET_SUMMARY_TRAINING_REQUEST_COMPLETED',
GET_TRAINING_REQUEST_COMPLETED: 'GET_TRAINING_REQUEST_COMPLETED',
}
3 changes: 3 additions & 0 deletions src/common/actionEnums/spec/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ describe('adminActionEnums', () => {
expect(adminActionEnums.GET_SUMMARY_TRAINING_REQUEST_COMPLETED).not.to.be.undefined;
expect(adminActionEnums.GET_SUMMARY_TRAINING_REQUEST_COMPLETED).to.be.a('string');
expect(adminActionEnums.GET_SUMMARY_TRAINING_REQUEST_COMPLETED).equals('GET_SUMMARY_TRAINING_REQUEST_COMPLETED');
expect(adminActionEnums.GET_TRAINING_REQUEST_COMPLETED).not.to.be.undefined;
expect(adminActionEnums.GET_TRAINING_REQUEST_COMPLETED).to.be.a('string');
expect(adminActionEnums.GET_TRAINING_REQUEST_COMPLETED).equals('GET_TRAINING_REQUEST_COMPLETED');
})
})
27 changes: 27 additions & 0 deletions src/reducers/adminTraining.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Action } from 'redux';
import { TrainingEntity } from '../model/training';
import { adminActionEnums} from '../common/actionEnums/admin';


export class AdminTrainingState {
editTraining : TrainingEntity;

public constructor() {
this.editTraining = new TrainingEntity();
}
}

export const adminTrainingReducer = (state : AdminTrainingState = new AdminTrainingState(), action) => {
switch(action.type) {
case adminActionEnums.GET_TRAINING_REQUEST_COMPLETED:
return handleGetTrainingRequestCompleted(state, action.payload);
}

return state;
}

const handleGetTrainingRequestCompleted =
(state : AdminTrainingState, payload : TrainingEntity) => {
const newState = Object.assign({}, state, {editTraining: payload});
return newState;
}
62 changes: 62 additions & 0 deletions src/reducers/spec/adminTraining.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect } from 'chai';
import * as deepFreeze from 'deep-freeze';
import {} from 'mocha'
import {} from 'core-js'
import { adminTrainingReducer, AdminTrainingState } from '../adminTraining';
import { TrainingEntity } from '../../model/training'
import { Student } from '../../model/student'
import { Trainer } from '../../model/trainer'
import {adminActionEnums} from '../../common/actionEnums/admin'


describe('adminTrainingReducer', () => {
it("is defined", () => {
// Arrange
// Act
// Assert
expect(adminTrainingReducer).not.be.undefined;
});

it("should return same state when passing an action that is not expected", () => {
// Arrange
const originalState : AdminTrainingState = new AdminTrainingState();

const action = {
type: 'NOT_EXPECTED_ACTION'
}

// Act
const newState = adminTrainingReducer(originalState, action);

// Assert
expect(newState).to.be.equal(originalState);

});

it("should return a new state including edit training when passing a GET_TRAINING_REQUEST_COMPLETED", () => {
// Arrange
const originalState : AdminTrainingState = new AdminTrainingState();
const id: number = 32;
const expectedTraining : TrainingEntity = {
id: 32,
name: 'React/Redux',
isActive: true,
start: new Date(1,1,2017),
end: new Date(31,1,2017),
students: new Array<Student>(),
trainers: new Array<Trainer>()
};
const actionResult = {
type: adminActionEnums.GET_TRAINING_REQUEST_COMPLETED,
payload: expectedTraining
}

// Act
const newState = adminTrainingReducer(originalState, actionResult);

// Assert
expect(newState).not.to.be.equal(originalState);
expect(newState.editTraining).to.be.eql(expectedTraining);

});
});

0 comments on commit 06a4897

Please sign in to comment.