diff options
| author | 杨宇千 <crupest@outlook.com> | 2019-03-06 23:29:12 +0800 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-06 23:29:12 +0800 | 
| commit | 4b37c96de2c7d3fe046a6f342d2da8ef03d3c807 (patch) | |
| tree | e5618cddfa6f637d7033ae5b52e6da825eb53c63 /Timeline/ClientApp/src/app/todo/todo-service | |
| parent | aca753fba19a221f1aec65030ba4aec4bc34f576 (diff) | |
| parent | b5e01c4571061cbaf5915aa4c0f1b7126ef1ed18 (diff) | |
| download | timeline-4b37c96de2c7d3fe046a6f342d2da8ef03d3c807.tar.gz timeline-4b37c96de2c7d3fe046a6f342d2da8ef03d3c807.tar.bz2 timeline-4b37c96de2c7d3fe046a6f342d2da8ef03d3c807.zip  | |
Merge pull request #3 from crupest/user
Develop user dialog.
Diffstat (limited to 'Timeline/ClientApp/src/app/todo/todo-service')
| -rw-r--r-- | Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts | 54 | ||||
| -rw-r--r-- | Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts | 43 | 
2 files changed, 97 insertions, 0 deletions
diff --git a/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts new file mode 100644 index 00000000..d8283b54 --- /dev/null +++ b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts @@ -0,0 +1,54 @@ +import { TestBed } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { toArray } from 'rxjs/operators'; + +import { TodoItem } from '../todo-item'; +import { TodoListService, IssueResponse } from './todo-list.service'; + + +describe('TodoListServiceService', () => { +  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); + +    const baseUrl = service.baseUrl; + +    const mockIssueList: IssueResponse = [{ +      number: 1, +      title: 'Issue title 1', +      state: 'open', +      html_url: 'test_url1' +    }, { +      number: 2, +      title: 'Issue title 2', +      state: 'closed', +      html_url: 'test_url2', +      pull_request: {} +    }]; + +    const mockTodoItemList: TodoItem[] = [{ +      number: 1, +      title: 'Issue title 1', +      isClosed: false, +      detailUrl: 'test_url1' +    }]; + +    service.getWorkItemList().pipe(toArray()).subscribe(data => { +      expect(data).toEqual(mockTodoItemList); +    }); + +    const httpController: HttpTestingController = TestBed.get(HttpTestingController); + +    httpController.expectOne(request => request.url === baseUrl + '/issues' && request.params.get('state') === 'all').flush(mockIssueList); + +    httpController.verify(); +  }); +}); diff --git a/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts new file mode 100644 index 00000000..83bf47ec --- /dev/null +++ b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable, from } from 'rxjs'; +import { switchMap, map, filter } from 'rxjs/operators'; + +import {TodoItem} from '../todo-item'; + +export interface IssueResponseItem { +  number: number; +  title: string; +  state: string; +  html_url: string; +  pull_request?: any; +} + +export type IssueResponse = IssueResponseItem[]; + +@Injectable({ +  providedIn: 'root' +}) +export class TodoListService { + +  readonly baseUrl = 'https://api.github.com/repos/crupest/Timeline'; + +  constructor(private client: HttpClient) { } + +  getWorkItemList(): Observable<TodoItem> { +    return this.client.get<IssueResponse>(`${this.baseUrl}/issues`, { +      params: { +        state: 'all' +      } +    }).pipe( +      switchMap(result => from(result)), +      filter(result => result.pull_request === undefined), // filter out pull requests. +      map(result => <TodoItem>{ +        number: result.number, +        title: result.title, +        isClosed: result.state === 'closed', +        detailUrl: result.html_url +      }) +    ); +  } +}  | 
