Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #58 from zielinskikamil/add-unit-tests
Browse files Browse the repository at this point in the history
Added unit tests part I
  • Loading branch information
kpakur authored Jun 20, 2018
2 parents 08bb48e + a6ded39 commit 337a942
Show file tree
Hide file tree
Showing 3 changed files with 424 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/app/sessions/services/sessions-creation.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SessionCreate } from './../models/session-create.model';
import { SessionsCreationService } from './sessions-creation.service';

let storeSpy;
let session: any | SessionCreate;

describe('SessionsCreationService', () => {
beforeEach(() => {
storeSpy = jasmine.createSpyObj('Store', ['dispatch']);
session = { userTransactionId: null };

new SessionsCreationService(storeSpy).create(session);
});

describe('create', () => {
it('should set userTransactionId for given session', () => {
expect(session.userTransactionId).toBeDefined();
});

it('should dispatch two actions', () => {
expect(storeSpy.dispatch).toHaveBeenCalledTimes(2);
});

it('should set transaction.id to be the same as session.userTransactionId', () => {
const transactionId = storeSpy.dispatch.calls.first().args[0].payload.id;
expect(transactionId).toEqual(session.userTransactionId);
});
});
});
198 changes: 198 additions & 0 deletions src/app/sessions/services/sessions-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { TestBed } from '@angular/core/testing';
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import { AppConfig } from '../../app.config';
import * as moment from 'moment';
import { SessionsService } from './sessions-service';
import { SessionCreate } from '../models/session-create.model';
import {
getSessionResponse,
normalizedGetSessionResponse,
getSessionsResponse,
normalizedGetSessionsResponse,
getSessionsWithHearingsResponse,
normalizedGetSessionsWithHearings
} from './test-data/sessions-service-test-data';

const sessionID = 'some-session-id';
const mockedAppConfig = { getApiUrl: () => 'https://google.co.uk' };
const format = 'DD-MM-YYYY';
const startDayString = '01-01-2019';
const endDayString = '02-02-2019';
const judgeUsername = 'Andrew Smith';
const expectedGetSessionsByJudgeAndDateURL =
`${mockedAppConfig.getApiUrl()}/sessions/judge-diary?` +
`judge=${judgeUsername}` +
`&startDate=${startDayString}` +
`&endDate=${endDayString}`;

let httpMock: HttpTestingController;
let sessionsService: SessionsService;

const verifyIfSessionsAreNormalized = (data: any) => {
expect(data).toEqual(normalizedGetSessionsResponse);
};

describe('SessionsService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
SessionsService,
{ provide: AppConfig, useValue: mockedAppConfig }
]
});
sessionsService = TestBed.get(SessionsService);
httpMock = TestBed.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});

describe('getSession', () => {
const expectedGetSessionURL = `${mockedAppConfig.getApiUrl()}/sessions/${sessionID}`;

it('should build valid URL', () => {
sessionsService.getSession(sessionID).subscribe();
httpMock.expectOne(expectedGetSessionURL).flush(getSessionResponse);
});

it('should return normalized data', () => {
sessionsService
.getSession(sessionID)
.subscribe(data => expect(data).toEqual(normalizedGetSessionResponse));

httpMock.expectOne(expectedGetSessionURL).flush(getSessionResponse);
});
});

describe('searchSessions', () => {
const date = new Date();
const expectedGetSessionURL = `${mockedAppConfig.getApiUrl()}/sessions?date=${date}`;
const sessionQuery = { date };

it('should build valid URL', () => {
sessionsService.searchSessions(sessionQuery).subscribe();
httpMock.expectOne(expectedGetSessionURL).flush(getSessionResponse);
});

it('should return normalized data', () => {
sessionsService
.searchSessions(sessionQuery)
.subscribe(verifyIfSessionsAreNormalized);
httpMock.expectOne(expectedGetSessionURL).flush(getSessionsResponse);
});
});

describe('searchSessionsForDates', () => {
const expectedGetSessionsByDateURL =
`${mockedAppConfig.getApiUrl()}` +
`/sessions?startDate=${startDayString}` +
`&endDate=${endDayString}`;
const sessionQueryForDates = {
startDate: moment(startDayString, format).toDate(),
endDate: moment(endDayString, format).toDate()
};

it('should build valid URL', () => {
sessionsService.searchSessionsForDates(sessionQueryForDates).subscribe();
httpMock
.expectOne(expectedGetSessionsByDateURL)
.flush(getSessionResponse);
});

it('should return normalized data', () => {
sessionsService
.searchSessionsForDates(sessionQueryForDates)
.subscribe(verifyIfSessionsAreNormalized);

httpMock
.expectOne(expectedGetSessionsByDateURL)
.flush(getSessionsResponse);
});
});

describe('searchSessionsForJudge', () => {
const diaryLoadParameters = {
startDate: moment(startDayString, format).toDate(),
endDate: moment(endDayString, format).toDate(),
judgeUsername
};

it('should build valid URL', () => {
sessionsService.searchSessionsForJudge(diaryLoadParameters).subscribe();
httpMock
.expectOne(expectedGetSessionsByJudgeAndDateURL)
.flush(getSessionResponse);
});

it('should return normalized data', () => {
sessionsService
.searchSessionsForJudge(diaryLoadParameters)
.subscribe(verifyIfSessionsAreNormalized);

httpMock
.expectOne(expectedGetSessionsByJudgeAndDateURL)
.flush(getSessionsResponse);
});
});

describe('searchSessionsForJudgeWithHearings', () => {
const diaryLoadParameters = {
startDate: moment(startDayString, format).toDate(),
endDate: moment(endDayString, format).toDate(),
judgeUsername
};

it('should build valid URL', () => {
sessionsService
.searchSessionsForJudgeWithHearings(diaryLoadParameters)
.subscribe();

httpMock
.expectOne(expectedGetSessionsByJudgeAndDateURL)
.flush(getSessionResponse);
});

it('should return normalized data', () => {
sessionsService
.searchSessionsForJudgeWithHearings(diaryLoadParameters)
.subscribe(data =>
expect(data).toEqual(normalizedGetSessionsWithHearings)
);

httpMock
.expectOne(expectedGetSessionsByJudgeAndDateURL)
.flush(getSessionsWithHearingsResponse);
});
});

describe('createSession', () => {
const expectedCreateSessionsURL = `${mockedAppConfig.getApiUrl()}/sessions`;
const dummySession: SessionCreate = {
id: 'some-id',
userTransactionId: 'some-user-transaction-id',
personId: 'some-person-id',
roomId: 'some-room-id',
duration: 30,
start: new Date(),
caseType: 'some-case-type'
};

it('should build valid URL with body', () => {
sessionsService.createSession(dummySession).subscribe();
const req = httpMock.expectOne(expectedCreateSessionsURL);
expect(req.request.body).toEqual(dummySession);
req.flush(null);
});

it('should make PUT request', () => {
sessionsService.createSession(dummySession).subscribe();
const req = httpMock.expectOne(expectedCreateSessionsURL);
expect(req.request.method).toBe('PUT');
req.flush(null);
});
});
});
Loading

0 comments on commit 337a942

Please sign in to comment.