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

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corinnekrych authored and joshuawilson committed Apr 26, 2017
1 parent 6abba76 commit 971a677
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 20 deletions.
58 changes: 52 additions & 6 deletions src/app/areas/area.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ describe('Service: AreaService', () => {
let areaService: AreaService;
let mockService: MockBackend;
let fakeAuthService: any;
let mockLog: any;

beforeEach(() => {
mockLog = jasmine.createSpyObj('Logger', ['error']);
fakeAuthService = {
getToken: function () {
return '';
Expand All @@ -28,7 +30,9 @@ describe('Service: AreaService', () => {
};
TestBed.configureTestingModule({
providers: [
Logger,
{
provide: Logger, useValue: mockLog
},
BaseRequestOptions,
MockBackend,
{
Expand Down Expand Up @@ -83,8 +87,8 @@ describe('Service: AreaService', () => {
let response = { data: responseData, links: {} };
let expectedResponse = cloneDeep(responseData);


it('Get areas', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
Expand All @@ -93,14 +97,28 @@ describe('Service: AreaService', () => {
})
));
});

// when
areaService.getAllBySpaceId('1').subscribe((data: Area[]) => {
// then
expect(data[0].id).toEqual(expectedResponse[0].id);
expect(data[0].attributes.name).toEqual(expectedResponse[0].attributes.name);
});
}));

it('Get areas in error', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
// when
areaService.getAllBySpaceId('1').subscribe((data: Area[]) => {
fail('Get areas should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));

it('Add new area', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
Expand All @@ -109,15 +127,30 @@ describe('Service: AreaService', () => {
})
));
});

// when
areaService.create('1', responseData[0])
.subscribe((data: Area) => {
// then
expect(data.id).toEqual(expectedResponse[0].id);
expect(data.attributes.name).toEqual(expectedResponse[0].attributes.name);
});
}));

it('Add new area in error', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
// when
areaService.create('1', responseData[0])
.subscribe((data: Area) => {
fail('Add new area should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));

it('Get a single area by Id', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
Expand All @@ -126,14 +159,27 @@ describe('Service: AreaService', () => {
})
));
});

let areaId = '1';

// when
areaService.getById(areaId)
.subscribe((data: Area) => {
// then
expect(data.id).toEqual(expectedResponse[0].id);
expect(data.attributes.name).toEqual(expectedResponse[0].attributes.name);
});
}));

it('Get a single area by Id in error', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
let areaId = '1';
// when
areaService.getById(areaId)
.subscribe((data: Area) => {
fail('Get a single area by Id should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));
});
75 changes: 70 additions & 5 deletions src/app/collaborators/collaborator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { async, inject, TestBed } from '@angular/core/testing';
import { BaseRequestOptions, Http, Response, ResponseOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { BaseRequestOptions, Http, Response, ResponseType, ResponseOptions } from '@angular/http';
import { MockBackend, MockConnection} from '@angular/http/testing';

import { cloneDeep } from 'lodash';

Expand All @@ -15,6 +15,7 @@ describe('Service: CollaboratorService', () => {
let collaboratorService: CollaboratorService;
let mockService: MockBackend;
let fakeAuthService: any;
let mockLog: any;

beforeEach(() => {
fakeAuthService = {
Expand All @@ -25,9 +26,12 @@ describe('Service: CollaboratorService', () => {
return true;
}
};
mockLog = jasmine.createSpyObj('Logger', ['error']);
TestBed.configureTestingModule({
providers: [
Logger,
{
provide: Logger, useValue: mockLog
},
BaseRequestOptions,
MockBackend,
{
Expand Down Expand Up @@ -83,8 +87,8 @@ describe('Service: CollaboratorService', () => {
let response = { data: responseData, links: {} };
let expectedResponse = cloneDeep(responseData);


it('Get collaborators', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
Expand All @@ -93,14 +97,28 @@ describe('Service: CollaboratorService', () => {
})
));
});

// when
collaboratorService.getAllBySpaceId('1').subscribe((data: User[]) => {
// then
expect(data[0].id).toEqual(expectedResponse[0].id);
expect(data[0].attributes.username).toEqual(expectedResponse[0].attributes.username);
});
}));

it('Get collaborators in error', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
// when
collaboratorService.getAllBySpaceId('1').subscribe((data: User[]) => {
fail('get collaboration should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));

it('Add new collaborators', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
Expand All @@ -109,10 +127,57 @@ describe('Service: CollaboratorService', () => {
})
));
});
// when
collaboratorService.addCollaborators('1', responseData)
.subscribe(() => {
// then
expect('1').toEqual('1');
});
}));

it('Add new collaborators in error', async(() => {
// given
mockLog.error.and.returnValue();
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
// when
collaboratorService.addCollaborators('1', responseData)
.subscribe(() => {
fail('add collaboration should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));

it('Remove collaborator', async(() => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: JSON.stringify({data: ['id1']}),
status: 201
})
));
});
// when
collaboratorService.removeCollaborator('1', '6abd2970-9407-469d-a8ad-9e18706d732c')
.subscribe(() => {
// then
expect('1').toEqual('1');
});
}));

it('Remove collaborator in error', async(() => {
// given
mockLog.error.and.returnValue();
mockService.connections.subscribe((connection: any) => {
connection.mockError(new Error('some error'));
});
// when
collaboratorService.removeCollaborator('1', '6abd2970-9407-469d-a8ad-9e18706d732c')
.subscribe(() => {
fail('remove collaboration should be in error');
}, // then
error => expect(error).toEqual('some error'));
}));
});
30 changes: 30 additions & 0 deletions src/app/spaces/space-name.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { async, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { SpaceNamePipe } from './space-name.pipe'

describe('Pipe used for Name Space', () => {

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [SpaceNamePipe]
});
});

it('Replace first underscore with space', async(() => {
// given
let fixture = new SpaceNamePipe();
// when
let spaceNameTansformed = fixture.transform('a_test_with_space');
// then
expect(spaceNameTansformed).toEqual('a test with space');
}));

it('Do not fail with undefined/nil value', async(() => {
// given
let fixture = new SpaceNamePipe();
// when
let spaceNameTansformed = fixture.transform(undefined);
// then
expect(spaceNameTansformed).toEqual(undefined);
}));
});
Loading

0 comments on commit 971a677

Please sign in to comment.