From 289f574a51e381c143811a7cde7cb3029e57c798 Mon Sep 17 00:00:00 2001 From: Borja Maza Date: Thu, 8 Dec 2016 09:11:48 +0100 Subject: [PATCH] #39 Initial Commit + Rename Training model to TrainingEntity + function GetTrainingById in ApiRest and spec --- src/model/spec/training.spec.ts | 6 +++--- src/model/training.ts | 2 +- src/rest-api/spec/training.spec.ts | 25 ++++++++++++++++++++++--- src/rest-api/training.ts | 16 ++++++++++------ src/rest-api/trainingMockData.ts | 4 ++-- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/model/spec/training.spec.ts b/src/model/spec/training.spec.ts index db8863d..18d5d3d 100644 --- a/src/model/spec/training.spec.ts +++ b/src/model/spec/training.spec.ts @@ -1,15 +1,15 @@ import { expect } from 'chai'; import {} from 'mocha' import {} from 'core-js' -import {Training} from '../training'; +import {TrainingEntity} from '../training'; import {Student} from '../student'; import {Trainer} from '../trainer'; describe('Training', () => { - let training : Training = null; + let training : TrainingEntity = null; beforeEach(() => { - training = new Training(); + training = new TrainingEntity(); }); it('Is instantiated and exists', () => { diff --git a/src/model/training.ts b/src/model/training.ts index 57235de..20a74f2 100644 --- a/src/model/training.ts +++ b/src/model/training.ts @@ -1,7 +1,7 @@ import {Student} from "./student"; import {Trainer} from "./trainer"; -export class Training { +export class TrainingEntity { id : number; name : string; isActive : boolean; diff --git a/src/rest-api/spec/training.spec.ts b/src/rest-api/spec/training.spec.ts index 1943662..6d62c69 100644 --- a/src/rest-api/spec/training.spec.ts +++ b/src/rest-api/spec/training.spec.ts @@ -1,13 +1,13 @@ import { expect } from 'chai'; import {} from 'mocha' import {} from 'core-js' -import { Training } from '../../model/training'; +import { TrainingEntity } from '../../model/training'; import { Student } from '../../model/student'; import { Trainer } from '../../model/trainer'; import {TrainingSummary} from '../../model/trainingSummary'; import { trainingApi } from '../training' -let trainingList : Training[]; +let trainingList : TrainingEntity[]; beforeEach(() => { trainingList = [ @@ -80,6 +80,25 @@ describe('TrainingApi', () => { expect(summaryList).to.eql(expectedSummaryList); done(); }); - }).bind(this)) + }).bind(this)); + }); + describe('getTrainingById', () => { + it('Get the expected training', sinon.test((done) => { + // Arrange + const id: number = 32; + let expectedTraining : TrainingEntity; + + // Act + expectedTraining = trainingList.find(training => training.id === id); + + + const fetchTraining = trainingApi.getTrainingById(id); + + fetchTraining.then((training) => { + // Assert + expect(training).to.eql(expectedTraining); + done(); + }); + }).bind(this)); }); }); diff --git a/src/rest-api/training.ts b/src/rest-api/training.ts index a9126ee..09ab1ae 100644 --- a/src/rest-api/training.ts +++ b/src/rest-api/training.ts @@ -1,15 +1,14 @@ -import { Training } from '../model/training' -import { TrainingSummary } from '../model/trainingSummary' -import { trainingMockData } from './trainingMockData' - +import { TrainingSummary } from '../model/trainingSummary'; +import { trainingMockData } from './trainingMockData'; +import { TrainingEntity } from '../model/training'; class TrainingApi { - trainingList : Training[]; + trainingList : TrainingEntity[]; constructor() { this.trainingList = trainingMockData; } - setMockDataSeed(trainingList : Training[]) { + setMockDataSeed(trainingList : TrainingEntity[]) { this.trainingList = trainingList; } @@ -24,6 +23,11 @@ class TrainingApi { return Promise.resolve(trainingSummaryList); } + + getTrainingById(id: number): Promise { + const training = this.trainingList.find(tr => tr.id === id); + return Promise.resolve(training); + } } export const trainingApi = new TrainingApi(); diff --git a/src/rest-api/trainingMockData.ts b/src/rest-api/trainingMockData.ts index 3fcb4ca..c10bc08 100644 --- a/src/rest-api/trainingMockData.ts +++ b/src/rest-api/trainingMockData.ts @@ -1,8 +1,8 @@ -import { Training } from '../model/training' +import { TrainingEntity } from '../model/training' import { Student } from '../model/student' import { Trainer } from '../model/trainer' -export const trainingMockData : Training[] = +export const trainingMockData : TrainingEntity[] = [ { id: 32,