Skip to content

Commit d7030f6

Browse files
committed
fix: use search api, fix tests
1 parent 1515f41 commit d7030f6

11 files changed

Lines changed: 191 additions & 80 deletions

File tree

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import { HttpHeaders, HttpResponse } from '@angular/common/http';
2+
import { PullRequest } from '../repository/services/repository.interfaces';
13
import {
24
ISSUE_STATE,
35
PullRequestAPIResponse,
4-
PullRequestItemAPIResponse,
56
IssueLabel,
6-
RepoPullRequests,
77
} from '../state/repository';
88

99
export const generatePullRequestAPIResponseFixture = (
1010
state: ISSUE_STATE = 'open',
11-
): PullRequestAPIResponse => {
11+
): HttpResponse<PullRequestAPIResponse> => {
1212
const closedDate = new Date(2022, 2, 1).toISOString();
13-
return {
13+
const body = {
1414
incomplete_results: false,
1515
total_count: 1,
1616
items: [
@@ -37,30 +37,18 @@ export const generatePullRequestAPIResponseFixture = (
3737
} as IssueLabel,
3838
],
3939
comments: 305,
40-
} as PullRequestItemAPIResponse,
40+
} as unknown as PullRequest,
4141
],
4242
};
43-
};
44-
45-
const prObject = generatePullRequestAPIResponseFixture().items[0];
4643

47-
export const pullRequestFixture: RepoPullRequests = {
48-
totalCount: 1,
49-
pullRequests: [
50-
{
51-
id: prObject.id,
52-
login: prObject.user.login,
53-
title: prObject.title,
54-
number: prObject.number,
55-
state: prObject.state,
56-
closedAt: prObject.closed_at ? new Date(prObject.closed_at) : null,
57-
mergedAt: prObject.pull_request.merged_at
58-
? new Date(prObject.pull_request.merged_at)
59-
: null,
60-
createdAt: new Date(prObject.created_at),
61-
labels: prObject.labels,
62-
commentCount: prObject.comments,
63-
labelCount: prObject.labels.length,
64-
},
65-
],
44+
return {
45+
headers: new HttpHeaders(),
46+
status: 200,
47+
statusText: 'OK',
48+
ok: true,
49+
type: 4,
50+
url: 'https://api.github.com/search/issues?q=repo:thisdot/open-source/issues+type:pr+state:open',
51+
clone: jasmine.createSpy('clone'),
52+
body,
53+
};
6654
};

angular-ngrx-scss/src/app/pull-requests/components/pull-request-card/pull-request-card.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { PullRequestCardComponent } from './pull-request-card.component';
44
import { ChangeDetectionStrategy } from '@angular/core';
5-
import { RepoPullRequest } from '../../../state/repository';
65
import { By } from '@angular/platform-browser';
76
import { SharedModule } from '../../../shared/shared.module';
7+
import { PullRequest } from 'src/app/repository/services/repository.interfaces';
88

99
describe('PullRequestCardComponent', () => {
1010
let component: PullRequestCardComponent;
@@ -26,11 +26,11 @@ describe('PullRequestCardComponent', () => {
2626
component = fixture.componentInstance;
2727
component.pullRequest = {
2828
id: 1,
29-
login: 'thisdot',
29+
user: { login: 'thisdot' },
3030
title: 'Get PRs information',
3131
number: 45,
3232
state: 'open',
33-
createdAt: new Date('01/01/2022'),
33+
created_at: new Date('01/01/2022'),
3434
labels: [
3535
{
3636
id: 2,
@@ -40,8 +40,8 @@ describe('PullRequestCardComponent', () => {
4040
color: 'D4C5F9',
4141
},
4242
],
43-
commentCount: 3,
44-
} as RepoPullRequest;
43+
comments: 3,
44+
} as unknown as PullRequest;
4545
fixture.detectChanges();
4646
});
4747

angular-ngrx-scss/src/app/pull-requests/components/pull-requests-header/pull-requests-header.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
name="Label"
2323
description="Select label"
2424
[isRepo]="true"
25+
[toggle]="true"
2526
[items]="(labels$ | async) ?? []"
27+
(setFilter)="setLabel($event)"
28+
[current]="filterParams.labels"
2629
></app-filter-dropdown>
2730
<app-filter-dropdown
2831
name="Sort"
2932
description="Select sort"
3033
[isRepo]="true"
34+
[items]="sortOptions"
35+
(setFilter)="setSort($event)"
36+
[current]="filterParams.sort"
3137
></app-filter-dropdown>
3238
</div>
3339
</div>

angular-ngrx-scss/src/app/pull-requests/components/pull-requests-header/pull-requests-header.component.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import {
88
import {
99
ISSUE_STATE,
1010
RepoPullRequests,
11+
fetchPullRequests,
1112
selectLabels,
1213
} from '../../../state/repository';
1314
import { Store } from '@ngrx/store';
1415
import { map } from 'rxjs';
16+
import { FilterOption } from 'src/app/shared/components/filter-dropdown/filter-dropdown.component';
17+
import { SORTING_OPTIONS } from 'src/app/shared/constants';
18+
import { Sort } from 'src/app/repository/services/repository.interfaces';
1519

1620
@Component({
1721
selector: 'app-pull-requests-header',
@@ -20,11 +24,19 @@ import { map } from 'rxjs';
2024
changeDetection: ChangeDetectionStrategy.OnPush,
2125
})
2226
export class PullRequestsHeaderComponent {
27+
@Input() owner!: string;
28+
@Input() repoName!: string;
2329
@Input() openPullRequests!: RepoPullRequests | null;
2430
@Input() closedPullRequests!: RepoPullRequests | null;
2531
@Input() viewState: ISSUE_STATE = 'open';
2632
@Output() viewStateChange = new EventEmitter<ISSUE_STATE>();
2733

34+
filterParams: { labels?: string; sort: Sort } = {
35+
sort: 'created',
36+
};
37+
38+
sortOptions: FilterOption[] = SORTING_OPTIONS;
39+
2840
labels$ = this.store
2941
.select(selectLabels)
3042
.pipe(
@@ -39,4 +51,31 @@ export class PullRequestsHeaderComponent {
3951
this.viewState = state;
4052
this.viewStateChange.emit(this.viewState);
4153
}
54+
55+
setLabel(label: string) {
56+
this.filterParams.labels = label;
57+
this.refetchPulls();
58+
}
59+
60+
setSort(sort: string) {
61+
this.filterParams.sort = sort as Sort;
62+
this.refetchPulls();
63+
}
64+
65+
private refetchPulls() {
66+
this.store.dispatch(
67+
fetchPullRequests({
68+
owner: this.owner,
69+
repoName: this.repoName,
70+
params: { state: 'open', ...this.filterParams },
71+
}),
72+
);
73+
this.store.dispatch(
74+
fetchPullRequests({
75+
owner: this.owner,
76+
repoName: this.repoName,
77+
params: { state: 'closed', ...this.filterParams },
78+
}),
79+
);
80+
}
4281
}

angular-ngrx-scss/src/app/pull-requests/components/pull-requests-list/pull-requests-list.component.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { PullRequestsListComponent } from './pull-requests-list.component';
44
import { ChangeDetectionStrategy, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
5-
import { RepoPullRequest } from '../../../state/repository';
65
import { By } from '@angular/platform-browser';
6+
import { PullRequest } from 'src/app/repository/services/repository.interfaces';
77

88
describe('PullRequestsListComponent', () => {
99
let component: PullRequestsListComponent;
@@ -42,15 +42,20 @@ describe('PullRequestsListComponent', () => {
4242

4343
it('should render pull request cards', () => {
4444
component.pullRequests = {
45-
totalCount: 1,
45+
total: 1,
46+
paginationParams: {
47+
page: 1,
48+
canNext: false,
49+
canPrev: false,
50+
},
4651
pullRequests: [
4752
{
4853
id: 1,
49-
login: 'thisdot',
54+
user: { login: 'thisdot' },
5055
title: 'Get PRs information',
5156
number: 45,
5257
state: 'open',
53-
createdAt: new Date('01/01/2021'),
58+
created_at: new Date('01/01/2021'),
5459
labels: [
5560
{
5661
id: 2,
@@ -61,7 +66,7 @@ describe('PullRequestsListComponent', () => {
6166
},
6267
],
6368
commentCount: 3,
64-
} as RepoPullRequest,
69+
} as unknown as PullRequest,
6570
],
6671
};
6772
fixture.detectChanges();

angular-ngrx-scss/src/app/pull-requests/pull-requests.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[closedPullRequests]="closedPullRequests$ | async"
55
[viewState]="viewState"
66
(viewStateChange)="viewStateChange($event)"
7+
[owner]="owner"
8+
[repoName]="repoName"
79
></app-pull-requests-header>
810
<app-pull-requests-list
911
[pullRequests]="

angular-ngrx-scss/src/app/pull-requests/pull-requests.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ describe('PullRequestsComponent', () => {
6868
store.scannedActions$.subscribe((action) => {
6969
expect(action).toEqual(
7070
fetchPullRequests({
71-
prState: 'closed',
7271
owner: 'thisdot',
7372
repoName: 'starter.dev-github-showcases',
73+
params: {
74+
state: 'closed',
75+
},
7476
}),
7577
);
7678
done();

angular-ngrx-scss/src/app/repository/services/repository.interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export interface RepositoryIssuesApiParams {
5656

5757
export interface RepositoryPullsApiParams {
5858
state: 'open' | 'closed' | 'all';
59+
labels?: string;
60+
sort?: Sort;
5961
page?: number;
6062
}
6163

angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import {
1111
RepoApiResponse,
1212
RepoContentsApiResponse,
1313
RepoIssues,
14+
RepoPullRequests,
1415
} from 'src/app/state/repository';
15-
import {
16-
IssueComments,
17-
PullRequest,
18-
PullRequests,
19-
} from './repository.interfaces';
16+
import { IssueComments, PullRequest } from './repository.interfaces';
2017

2118
import { generatePullRequestAPIResponseFixture } from '../../fixtures/repository.fixtures';
2219
import { RepositoryService } from './repository.service';
@@ -192,7 +189,7 @@ const MOCK_PULL_REQUEST_COMMENTS: IssueComments = [
192189
},
193190
];
194191

195-
const MOCK_PULL_REQUEST: PullRequest = {
192+
const MOCK_PULL_REQUEST = {
196193
title: 'Et quis culpa ex sapiente dolores qui quo qui.',
197194
number: MOCK_PULL_REQUEST_NUMBER,
198195
user: {
@@ -204,9 +201,9 @@ const MOCK_PULL_REQUEST: PullRequest = {
204201
},
205202
closed_at: '2022-07-01T23:46:12Z',
206203
created_at: '2022-07-01T23:46:12Z',
207-
};
204+
} as PullRequest;
208205

209-
const MOCK_PULL_REQUESTS: PullRequests = [
206+
const MOCK_PULL_REQUESTS = [
210207
{
211208
title: 'Et quis culpa ex sapiente dolores qui quo qui.',
212209
number: MOCK_PULL_REQUEST_NUMBER,
@@ -233,7 +230,32 @@ const MOCK_PULL_REQUESTS: PullRequests = [
233230
closed_at: '2022-07-02T23:46:12Z',
234231
created_at: '2022-07-02T23:46:12Z',
235232
},
236-
];
233+
] as PullRequest[];
234+
235+
const EXPECTED_PULL_REQUESTS: RepoPullRequests = {
236+
total: 3,
237+
paginationParams: {
238+
page: 1,
239+
canNext: false,
240+
canPrev: false,
241+
},
242+
pullRequests: MOCK_PULL_REQUESTS,
243+
};
244+
245+
const MOCK_PULL_REQUESTS_RESPONSE: HttpResponse<IssueAPIResponse> = {
246+
headers: new HttpHeaders(),
247+
clone: jasmine.createSpy('clone'),
248+
type: HttpEventType.Response,
249+
status: 200,
250+
statusText: 'OK',
251+
ok: true,
252+
url: 'http://localhost',
253+
body: {
254+
total_count: 3,
255+
incomplete_results: false,
256+
items: MOCK_PULL_REQUESTS,
257+
},
258+
};
237259

238260
describe('RepositoryService', () => {
239261
let repoService: RepositoryService;
@@ -381,23 +403,25 @@ describe('RepositoryService', () => {
381403
});
382404

383405
it('should return multiple pull requests for a given repository', (done) => {
384-
httpClientSpy.get.and.returnValue(of(MOCK_PULL_REQUESTS));
406+
httpClientSpy.get.and.returnValue(of(MOCK_PULL_REQUESTS_RESPONSE));
385407

386-
repoService.getRepositoryPullRequests('FakeCo', 'fake-repo').subscribe({
387-
next: (pullRequests) => {
388-
expect(pullRequests).toBe(MOCK_PULL_REQUESTS);
408+
repoService
409+
.getRepositoryPullRequests('FakeCo', 'fake-repo', { state: 'all' })
410+
.subscribe({
411+
next: (pullRequests) => {
412+
expect(pullRequests).toEqual(EXPECTED_PULL_REQUESTS);
389413

390-
expect(httpClientSpy.get).toHaveBeenCalledWith(
391-
`https://api.github.com/repos/FakeCo/fake-repo/pulls`,
392-
jasmine.objectContaining({
393-
headers: {
394-
Accept: 'application/vnd.github.v3+json',
395-
},
396-
}),
397-
);
398-
},
399-
complete: done,
400-
});
414+
expect(httpClientSpy.get).toHaveBeenCalledWith(
415+
`https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:pr+state:all`,
416+
jasmine.objectContaining({
417+
headers: {
418+
Accept: 'application/vnd.github.v3+json',
419+
},
420+
}),
421+
);
422+
},
423+
complete: done,
424+
});
401425
});
402426

403427
it('should return pull request comments when given a pull request number', (done) => {
@@ -468,15 +492,15 @@ describe('RepositoryService', () => {
468492
});
469493
});
470494

471-
describe('getPullRequests', () => {
495+
describe('getRepositoryPullRequests', () => {
472496
it('should return pull request for given repository', (done) => {
473-
const apiResponse: PullRequestAPIResponse =
497+
const apiResponse: HttpResponse<PullRequestAPIResponse> =
474498
generatePullRequestAPIResponseFixture();
475499

476500
httpClientSpy.get.and.returnValue(of(apiResponse).pipe(delay(0)));
477501
repoService
478-
.getPullRequests('thisdot', 'starter.dev-github-showcases', 'open')
479-
.subscribe((res) => {
502+
.getRepositoryPullRequest('thisdot', 'starter.dev-github-showcases', 1)
503+
.subscribe((res: unknown) => {
480504
expect(res).toEqual(apiResponse);
481505
done();
482506
});
@@ -485,7 +509,7 @@ describe('RepositoryService', () => {
485509
.withContext('called once')
486510
.toBe(1);
487511
expect(httpClientSpy.get).toHaveBeenCalledOnceWith(
488-
'https://api.github.com/search/issues?q=repo:thisdot/starter.dev-github-showcases+type:pr+state:open',
512+
'https://api.github.com/repos/thisdot/starter.dev-github-showcases/pulls/1',
489513
jasmine.objectContaining({
490514
headers: {
491515
Accept: 'application/vnd.github.v3+json',

0 commit comments

Comments
 (0)