From 34bb7747a6f23f4b7e748c046945182084bb208b Mon Sep 17 00:00:00 2001 From: Kamil Zielinski Date: Thu, 14 Jun 2018 08:53:37 +0200 Subject: [PATCH 1/2] added unit tests for session creation service --- .../sessions-creation.service.spec.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/app/sessions/services/sessions-creation.service.spec.ts diff --git a/src/app/sessions/services/sessions-creation.service.spec.ts b/src/app/sessions/services/sessions-creation.service.spec.ts new file mode 100644 index 00000000..2627aa16 --- /dev/null +++ b/src/app/sessions/services/sessions-creation.service.spec.ts @@ -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); + }); + }); +}); From a6ded394ec70429bfc034939a9cc887019056f8c Mon Sep 17 00:00:00 2001 From: Kamil Zielinski Date: Fri, 15 Jun 2018 12:04:36 +0200 Subject: [PATCH 2/2] added unit tests for session-service --- .../services/sessions-service.spec.ts | 198 ++++++++++++++++++ .../test-data/sessions-service-test-data.ts | 197 +++++++++++++++++ 2 files changed, 395 insertions(+) create mode 100644 src/app/sessions/services/sessions-service.spec.ts create mode 100644 src/app/sessions/services/test-data/sessions-service-test-data.ts diff --git a/src/app/sessions/services/sessions-service.spec.ts b/src/app/sessions/services/sessions-service.spec.ts new file mode 100644 index 00000000..82592b86 --- /dev/null +++ b/src/app/sessions/services/sessions-service.spec.ts @@ -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); + }); + }); +}); diff --git a/src/app/sessions/services/test-data/sessions-service-test-data.ts b/src/app/sessions/services/test-data/sessions-service-test-data.ts new file mode 100644 index 00000000..34813ee4 --- /dev/null +++ b/src/app/sessions/services/test-data/sessions-service-test-data.ts @@ -0,0 +1,197 @@ +export const getSessionResponse = { + id: '2bfd6084-5240-4665-ae86-ba9d90e2215a', + person: { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: null + }, + start: '2018-06-14T09:12:40.003+02:00', + duration: 'PT30M', + caseType: 'SCLAIMS', + room: { id: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', name: 'Room A' } +}; + +export const normalizedGetSessionResponse = { + entities: { + persons: { + 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2': { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: null + } + }, + rooms: { + '30bcf571-45ca-4528-9d05-ce51b5e3fcde': { + id: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', + name: 'Room A' + } + }, + sessions: { + '2bfd6084-5240-4665-ae86-ba9d90e2215a': { + id: '2bfd6084-5240-4665-ae86-ba9d90e2215a', + person: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + start: '2018-06-14T09:12:40.003+02:00', + duration: 'PT30M', + caseType: 'SCLAIMS', + room: '30bcf571-45ca-4528-9d05-ce51b5e3fcde' + } + } + }, + result: '2bfd6084-5240-4665-ae86-ba9d90e2215a' +}; + +export const getSessionsResponse = [ + { + id: '2bfd6084-5240-4665-ae86-ba9d90e2215a', + start: '2018-06-14T09:12:40.003+02:00', + duration: 'PT30M', + person: { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: null + }, + room: { id: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', name: 'Room A' }, + caseType: 'SCLAIMS' + }, + { + id: '1231244-5240-4665-ae86-baasdasd15a', + start: '2018-06-16T09:12:40.003+02:00', + duration: 'PT30M', + person: { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: null + }, + room: { id: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', name: 'Room A' }, + caseType: 'SCLAIMS' + } +]; + +export const normalizedGetSessionsResponse = { + entities: { + persons: { + 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2': { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: null + } + }, + rooms: { + '30bcf571-45ca-4528-9d05-ce51b5e3fcde': { + id: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', + name: 'Room A' + } + }, + sessions: { + '2bfd6084-5240-4665-ae86-ba9d90e2215a': { + id: '2bfd6084-5240-4665-ae86-ba9d90e2215a', + start: '2018-06-14T09:12:40.003+02:00', + duration: 'PT30M', + person: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + room: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', + caseType: 'SCLAIMS' + }, + '1231244-5240-4665-ae86-baasdasd15a': { + id: '1231244-5240-4665-ae86-baasdasd15a', + start: '2018-06-16T09:12:40.003+02:00', + duration: 'PT30M', + person: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + room: '30bcf571-45ca-4528-9d05-ce51b5e3fcde', + caseType: 'SCLAIMS' + } + } + }, + result: [ + '2bfd6084-5240-4665-ae86-ba9d90e2215a', + '1231244-5240-4665-ae86-baasdasd15a' + ] +}; + +export const getSessionsWithHearingsResponse = { + sessions: [ + { + id: 'a4a353d0-4bc1-4837-b49c-6e946ab96d6d', + person: { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: 'judge1' + }, + start: '2018-06-15T11:00:57.353+02:00', + duration: 'PT30M', + caseType: 'SCLAIMS', + room: { id: '3e699f29-6ea9-46bf-a338-00622fe0ae1b', name: 'Room B' } + } + ], + hearingParts: [ + { + id: '860f4aae-421c-4ca3-97b4-ddbbc0bf7ecb', + caseNumber: '123', + caseTitle: '123', + caseType: 'SCLAIMS', + hearingType: 'Preliminary Hearing', + duration: 'PT10M', + scheduleStart: null, + scheduleEnd: null, + start: null, + createdAt: '2018-06-15T10:40:28.58+02:00', + session: 'a4a353d0-4bc1-4837-b49c-6e946ab96d6d' + } + ] +}; + +export const normalizedGetSessionsWithHearings = { + entities: { + persons: { + 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2': { + id: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + personType: 'JUDGE', + name: 'John Harris', + username: 'judge1' + } + }, + rooms: { + '3e699f29-6ea9-46bf-a338-00622fe0ae1b': { + id: '3e699f29-6ea9-46bf-a338-00622fe0ae1b', + name: 'Room B' + } + }, + sessions: { + 'a4a353d0-4bc1-4837-b49c-6e946ab96d6d': { + id: 'a4a353d0-4bc1-4837-b49c-6e946ab96d6d', + person: 'b5bc80ec-8306-4f0f-8c6e-af218bb116c2', + start: '2018-06-15T11:00:57.353+02:00', + duration: 'PT30M', + caseType: 'SCLAIMS', + room: '3e699f29-6ea9-46bf-a338-00622fe0ae1b' + } + }, + hearingParts: { + '860f4aae-421c-4ca3-97b4-ddbbc0bf7ecb': { + id: '860f4aae-421c-4ca3-97b4-ddbbc0bf7ecb', + caseNumber: '123', + caseTitle: '123', + caseType: 'SCLAIMS', + hearingType: 'Preliminary Hearing', + duration: 'PT10M', + scheduleStart: null, + scheduleEnd: null, + start: null, + createdAt: '2018-06-15T10:40:28.58+02:00', + session: 'a4a353d0-4bc1-4837-b49c-6e946ab96d6d' + } + }, + sessionsWithHearings: { + undefined: { + sessions: ['a4a353d0-4bc1-4837-b49c-6e946ab96d6d'], + hearingParts: ['860f4aae-421c-4ca3-97b4-ddbbc0bf7ecb'] + } + } + }, + result: undefined +};