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

Commit

Permalink
fix(astronaut): ...and fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir authored and joshuawilson committed Mar 3, 2017
1 parent 45124cc commit 35009af
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 98 deletions.
147 changes: 71 additions & 76 deletions src/app/iteration/iterations.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MockBackend } from '@angular/http/testing';
import { cloneDeep } from 'lodash';
import {
AuthenticationService,
Broadcaster,
Logger
} from 'ngx-login-client';
import { SpaceService } from 'ngx-fabric8-wit';
Expand All @@ -22,65 +23,58 @@ import { IterationModel } from '../models/iteration.model';
import { IterationService } from './iteration.service';
import { GlobalSettings } from '../shared/globals';

describe ('Iteration service - ', () => {
describe('Iteration service - ', () => {
let apiService: IterationService;
let mockService: MockBackend;

let fakeAuthService: any;
let fakeSpaceService: any;
let fakeSpace: any;

beforeEach(() => {
fakeAuthService = {
getToken: function () {
return '';
},
isLoggedIn: function() {
isLoggedIn: function () {
return true;
}
};

fakeSpaceService = {
getCurrentSpace: function() {
return {
fakeSpace = {
'links': {
'self': 'http://localhost:8080/api/'
},
'relationships': {
'areas': {
'links': {
'self': 'http://localhost:8080/api/'
},
'relationships': {
'areas': {
'links': {
'related': 'http://localhost:8080/api/spaces/1f669678-ca2c-4cbb-b46d-5b70a98dde3c/areas'
}
},
'iterations': {
'links': {
'related': 'http://localhost:8080/api/spaces/1f669678-ca2c-4cbb-b46d-5b70a98dde3c/iterations'
}
}
},
name: 'Project 1',
};
}
'related': 'http://localhost:8080/api/spaces/1f669678-ca2c-4cbb-b46d-5b70a98dde3c/areas'
}
},
'iterations': {
'links': {
'related': 'http://localhost:8080/api/spaces/1f669678-ca2c-4cbb-b46d-5b70a98dde3c/iterations'
}
}
},
name: 'Project 1',
};

TestBed.configureTestingModule({
providers: [
Broadcaster,
Logger,
BaseRequestOptions,
MockBackend,
{
provide: Http,
useFactory: (backend: MockBackend,
options: BaseRequestOptions) => new Http(backend, options),
options: BaseRequestOptions) => new Http(backend, options),
deps: [MockBackend, BaseRequestOptions]
},
{
provide: AuthenticationService,
useValue: fakeAuthService
},
{
provide: SpaceService,
useValue: fakeSpaceService
},
IterationService,
GlobalSettings
]
Expand All @@ -92,43 +86,44 @@ describe ('Iteration service - ', () => {
(service: IterationService, mock: MockBackend) => {
apiService = service;
mockService = mock;
(apiService as any)._currentSpace = fakeSpace;
}
));

let resp: IterationModel[] = [{
attributes: {
endAt: '2016-11-29T23:18:14Z',
name: 'Sprint #24',
startAt: '2016-11-29T23:18:14Z',
state: 'start'
},
id: 'd8535583-dfbd-4b62-be8d-44a7d4fa7048',
links: {
self: 'http://localhost:8080/api/iterations/d8535583-dfbd-4b62-be8d-44a7d4fa7048'
},
relationships: {
space: {
data: {
id: 'd7d98b45-415a-4cfc-add2-ec7b7aee7dd5',
type: 'spaces'
},
links: {
self: 'http://localhost:8080/api/spaces/d7d98b45-415a-4cfc-add2-ec7b7aee7dd5'
}
attributes: {
endAt: '2016-11-29T23:18:14Z',
name: 'Sprint #24',
startAt: '2016-11-29T23:18:14Z',
state: 'start'
},
id: 'd8535583-dfbd-4b62-be8d-44a7d4fa7048',
links: {
self: 'http://localhost:8080/api/iterations/d8535583-dfbd-4b62-be8d-44a7d4fa7048'
},
relationships: {
space: {
data: {
id: 'd7d98b45-415a-4cfc-add2-ec7b7aee7dd5',
type: 'spaces'
},
workitems: {
links: {
related: 'http://localhost:8080/api/spaces/d7d98b45-415a-4cfc-add2-ec7b7aee7dd5/workitems'
},
meta: {
closed: 0,
total: 0
}
links: {
self: 'http://localhost:8080/api/spaces/d7d98b45-415a-4cfc-add2-ec7b7aee7dd5'
}
},
type: 'iterations'
workitems: {
links: {
related: 'http://localhost:8080/api/spaces/d7d98b45-415a-4cfc-add2-ec7b7aee7dd5/workitems'
},
meta: {
closed: 0,
total: 0
}
}
},
type: 'iterations'
}];
let response = {data: resp, links: {}};
let response = { data: resp, links: {} };
let checkResp = cloneDeep(resp);

it('Get iterations', async(() => {
Expand All @@ -141,24 +136,24 @@ describe ('Iteration service - ', () => {
));
});
apiService.getIterations()
.then (data => {
.then(data => {
expect(data).toEqual(checkResp);
});
});

// For an invalid URL
apiService.getIterations()
.catch ((data) => {
.catch((data) => {
expect(data).toEqual([]);
});

// Check if data from response is assigned to the private variable
apiService.getIterations()
.then (data => {
.then(data => {
expect(apiService.iterations).toEqual(checkResp);
});
});

apiService.getIterations()
.catch ((data) => {
.catch((data) => {
expect(apiService.iterations).toEqual([]);
});
}));
Expand All @@ -178,10 +173,10 @@ describe ('Iteration service - ', () => {

// Error response
apiService.getIterations()
.catch (data => {
.catch(data => {
expect(data).toEqual([]);
})
.then (() => {
.then(() => {
expect(apiService.iterations.length).toEqual(0);
});
}));
Expand All @@ -190,7 +185,7 @@ describe ('Iteration service - ', () => {
it('Create iteration', async(() => {
let requestParams = resp[0];
let responseData = cloneDeep(requestParams);
let response = {data: responseData};
let response = { data: responseData };

mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
Expand All @@ -202,10 +197,10 @@ describe ('Iteration service - ', () => {
});

apiService.createIteration(requestParams)
.then (data => {
.then(data => {
expect(data).toEqual(responseData);
})
.then (() => {
.then(() => {
expect(apiService.iterations.length).toEqual(1);
});
}));
Expand All @@ -226,10 +221,10 @@ describe ('Iteration service - ', () => {

// Error response
apiService.createIteration(requestParams)
.catch (data => {
.catch(data => {
expect(data).toEqual({});
})
.then (() => {
.then(() => {
expect(apiService.iterations.length).toEqual(0);
});
}));
Expand All @@ -244,7 +239,7 @@ describe ('Iteration service - ', () => {
});

// Patch service test
it ('Update iteration', async(() => {
it('Update iteration', async(() => {
// Assign the existing iteration value
apiService.iterations = cloneDeep(resp);

Expand All @@ -256,21 +251,21 @@ describe ('Iteration service - ', () => {
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: {data: requestParams},
body: { data: requestParams },
status: 200
})
));
});

apiService.updateIteration(requestParams)
.then (data => {
.then(data => {
console.log(apiService.iterations);
expect(apiService.iterations[0].attributes.name).toEqual('New Name');
});
}));

// Patch service test with API error
it ('Update iteration with API error', async(() => {
it('Update iteration with API error', async(() => {
// Assign the existing iteration value
apiService.iterations = cloneDeep(resp);

Expand All @@ -289,10 +284,10 @@ describe ('Iteration service - ', () => {
});

apiService.updateIteration(requestParams)
.catch (data => {
.catch(data => {
expect(data).toEqual({});
})
.then (() => {
.then(() => {
expect(apiService.iterations[0].attributes.name).toEqual('Sprint #24');
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { DebugElement } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { Logger } from 'ngx-login-client';
import { Logger, Broadcaster } from 'ngx-login-client';

import { WorkItem } from '../../models/work-item';
import { WorkItemService } from '../work-item.service';
import { AstronautService } from './../../shared/astronaut.service';

import { WorkItemQuickAddComponent } from './work-item-quick-add.component';

Expand Down Expand Up @@ -141,14 +140,11 @@ describe('Quick add work item component - ', () => {
WorkItemQuickAddComponent
],
providers: [
Broadcaster,
Logger,
{
provide: WorkItemService,
useValue: fakeService
},
{
provide: AstronautService,
useValue: fakeSpcaeService
}
]
})
Expand Down
18 changes: 2 additions & 16 deletions src/app/work-item/work-item.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Work Item Service - ', () => {
let iterationService: IterationService;

let fakeAuthService: any;
let fakeSpcaeService: any;
let fakeSpace: any;

let spaces = [{
'name': 'Project 1',
Expand Down Expand Up @@ -107,17 +107,6 @@ describe('Work Item Service - ', () => {
}
};

fakeSpcaeService = {
getCurrentSpaceBus: function() {
let currentSpaceSubjectSource = new BehaviorSubject<any>(spaces[0]);
return currentSpaceSubjectSource.asObservable();
},

getCurrentSpace: function() {
return spaces[0];
}
};

TestBed.configureTestingModule({
providers: [
Logger,
Expand All @@ -139,10 +128,6 @@ describe('Work Item Service - ', () => {
},
// MockDataService should be removed at some point
MockDataService,
{
provide: SpaceService,
useValue: fakeSpcaeService
},
WorkItemService,
UserService,
IterationService,
Expand All @@ -163,6 +148,7 @@ describe('Work Item Service - ', () => {
apiService = service;
mockService = mock;
iterationService = iService;
(apiService as any)._currentSpace = spaces[0];
}
));
let resp: WorkItem[] = [
Expand Down

0 comments on commit 35009af

Please sign in to comment.