diff options
author | crupest <crupest@outlook.com> | 2019-02-21 23:36:41 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-02-21 23:36:41 +0800 |
commit | b694d7a3e1b04bb942e5ba8224762d706f98a371 (patch) | |
tree | ce0cd6249f2c09797bca65cfb9dbc81f6c334d2e /Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts | |
parent | 0ed96f534f75ebc2a7bdbec031a4674368d0420a (diff) | |
download | timeline-b694d7a3e1b04bb942e5ba8224762d706f98a371.tar.gz timeline-b694d7a3e1b04bb942e5ba8224762d706f98a371.tar.bz2 timeline-b694d7a3e1b04bb942e5ba8224762d706f98a371.zip |
Add unit test.
Diffstat (limited to 'Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts')
-rw-r--r-- | Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts b/Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts index 529ba8cc..7e88ca52 100644 --- a/Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts +++ b/Timeline/ClientApp/src/app/todo-list-page/todo-list.service.spec.ts @@ -1,12 +1,79 @@ import { TestBed } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; + +import { TodoListService, WorkItem, AzureDevOpsAccessInfo, WiqlResult, WiqlWorkItemResult, WorkItemResult } from './todo-list.service'; -import { TodoListService } from './todo-list.service'; describe('TodoListServiceService', () => { - beforeEach(() => TestBed.configureTestingModule({})); + beforeEach(() => TestBed.configureTestingModule({ + imports: [HttpClientTestingModule] + })); it('should be created', () => { + + const service: TodoListService = TestBed.get(TodoListService); + expect(service).toBeTruthy(); + }); + + it('should work well', () => { const service: TodoListService = TestBed.get(TodoListService); expect(service).toBeTruthy(); + + const mockWorkItems: WorkItem[] = [{ + id: 0, + title: 'Test work item 1', + closed: true + }, { + id: 1, + title: 'Test work item 2', + closed: false + }]; + + service.getWorkItemList().subscribe(data => { + expect(data).toEqual(mockWorkItems); + }); + + const httpController: HttpTestingController = TestBed.get(HttpTestingController); + + const mockAccessInfo: AzureDevOpsAccessInfo = { + username: 'testusername', + personalAccessToken: 'testtoken', + organization: 'testorganization', + project: 'testproject' + }; + + httpController.expectOne('/api/TodoPage/AzureDevOpsAccessInfo').flush(mockAccessInfo); + + const mockWiqlWorkItems: WiqlWorkItemResult[] = [{ + id: 0, + url: `https://dev.azure.com/${mockAccessInfo.organization}/${mockAccessInfo.project}/_apis/wit/workItems/0` + }, { + id: 1, + url: `https://dev.azure.com/${mockAccessInfo.organization}/${mockAccessInfo.project}/_apis/wit/workItems/1` + }]; + + const authorizationHeader = 'Basic ' + btoa(mockAccessInfo.username + ':' + mockAccessInfo.personalAccessToken); + + httpController.expectOne(req => + req.url === `https://dev.azure.com/${mockAccessInfo.organization}/${mockAccessInfo.project}/_apis/wit/wiql?api-version=5.0` && + req.headers.get('Authorization') === authorizationHeader + ).flush(<WiqlResult>{ workItems: mockWiqlWorkItems }); + + function mapWorkItemToResult(workItem: WorkItem): WorkItemResult { + return { + id: workItem.id, + fields: { + [TodoListService.titleFieldName]: workItem.title, + [TodoListService.stateFieldName]: (workItem.closed ? 'Closed' : 'Active') + } + }; + } + + for (let i = 0; i < mockWorkItems.length; i++) { + httpController.expectOne(req => + req.url === mockWiqlWorkItems[i].url && + req.headers.get('Authorization') === authorizationHeader + ).flush(mapWorkItemToResult(mockWorkItems[i])); + } }); }); |