Skip to content

Commit

Permalink
fix: use mocking pull request fn also for issues
Browse files Browse the repository at this point in the history
  • Loading branch information
adelkahomolova committed Jan 7, 2020
1 parent 5d196f5 commit cfea49b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import moment from 'moment';
import nock from 'nock';
import { CollaborationInspector } from '../../inspectors';
import { CollaborationInspector, Paginated } from '../../inspectors';
import { createTestContainer, TestContainerContext } from '../../inversify.config';
import { PracticeEvaluationResult } from '../../model';
import { BitbucketPullRequestState, BitbucketService } from '../../services';
import { BitbucketNock } from '../../test/helpers/bitbucketNock';
import { TimeToSolvePullRequestsPractice } from './TimeToSolvePullRequestsPractice';
import { Types } from '../../types';
import { PullRequest } from '../../services/git/model';

describe('TimeToSolvePullRequestsPractice', () => {
let practice: TimeToSolvePullRequestsPractice;
Expand All @@ -22,9 +23,9 @@ describe('TimeToSolvePullRequestsPractice', () => {

beforeAll(() => {
containerCtx = createTestContainer();
containerCtx.container.bind('TimeToSolvePullRequestsPractice').to(TimeToSolvePullRequestsPractice);
containerCtx.container.bind('TimeToSolvePractice').to(TimeToSolvePullRequestsPractice);
containerCtx.container.rebind(Types.IContentRepositoryBrowser).to(BitbucketService);
practice = containerCtx.container.get('TimeToSolvePullRequestsPractice');
practice = containerCtx.container.get('TimeToSolvePractice');
mockCollaborationInspector = new MockedCollaborationInspector();
});

Expand All @@ -38,7 +39,7 @@ describe('TimeToSolvePullRequestsPractice', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests', state: BitbucketPullRequestState.open });
const args = { states: BitbucketPullRequestState.open, updatedAt: Date.now() - moment.duration(10, 'days').asMilliseconds() };
mockCollaborationInspector.getPullRequests = async () => {
return bitbucketNock.mockBitbucketPullRequestsResponse(args);
return <Paginated<PullRequest>>bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse(args);
};

const evaluated = await practice.evaluate({
Expand All @@ -54,7 +55,7 @@ describe('TimeToSolvePullRequestsPractice', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests', state: BitbucketPullRequestState.open });
const args = { states: BitbucketPullRequestState.open, updatedAt: Date.now() - moment.duration(100, 'days').asMilliseconds() };
mockCollaborationInspector.getPullRequests = async () => {
return bitbucketNock.mockBitbucketPullRequestsResponse(args);
return <Paginated<PullRequest>>bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse(args);
};

const evaluated = await practice.evaluate({
Expand Down
10 changes: 5 additions & 5 deletions src/services/bitbucket/BitbucketService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Bitbucket Service', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests' });

const response = await service.getPullRequests('pypy', 'pypy');
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketPullRequestsResponse({ states: BitbucketPullRequestState.open });
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse({ states: BitbucketPullRequestState.open });
expect(response).toMatchObject(getOpenPullRequestsResponse);
});

Expand All @@ -38,7 +38,7 @@ describe('Bitbucket Service', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests', state: BitbucketPullRequestState.open, pagination: { page: 1, perPage: 1 } });

const response = await service.getPullRequests('pypy', 'pypy', { pagination: { page: 1, perPage: 1 } });
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketPullRequestsResponse({ states: BitbucketPullRequestState.open });
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse({ states: BitbucketPullRequestState.open });
expect(response).toMatchObject(getOpenPullRequestsResponse);
});

Expand All @@ -48,7 +48,7 @@ describe('Bitbucket Service', () => {
bitbucketNock.getAdditionsAndDeletions('1');

const response = await service.getPullRequests('pypy', 'pypy', { withDiffStat: true });
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketPullRequestsResponse({
const getOpenPullRequestsResponse = bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse({
states: BitbucketPullRequestState.open,
withDiffStat: true,
});
Expand All @@ -62,7 +62,7 @@ describe('Bitbucket Service', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests', state: state });

const response = await service.getPullRequests('pypy', 'pypy', { filter: { state: PullRequestState.all } });
const allPullrequestsResponse = bitbucketNock.mockBitbucketPullRequestsResponse({ states: state });
const allPullrequestsResponse = bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse({ states: state });

expect(response).toMatchObject(allPullrequestsResponse);
});
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Bitbucket Service', () => {
bitbucketNock.getApiResponse({ resource: 'pullrequests', state: BitbucketPullRequestState.closed });

const response = await service.getPullRequests('pypy', 'pypy', state);
const getMergedPullRequestsResponse = bitbucketNock.mockBitbucketPullRequestsResponse({ states: BitbucketPullRequestState.closed });
const getMergedPullRequestsResponse = bitbucketNock.mockBitbucketIssuesOrPullRequestsResponse({ states: BitbucketPullRequestState.closed });

expect(response).toMatchObject(getMergedPullRequestsResponse);
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/helpers/bitbucketNock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BitbucketCommit } from '../../services/bitbucket/BitbucketService';
import { BitbucketPullRequestState } from '../../services/git/IVCSService';
import qs from 'qs';
import { Paginated, PaginationParams } from '../../inspectors';
import { PullRequest } from '../../services/git/model';
import { PullRequest, Issue } from '../../services/git/model';
import { getPullRequestResponse } from '../../services/git/__MOCKS__/bitbucketServiceMockFolder';
import _ from 'lodash';
import moment from 'moment';
Expand Down Expand Up @@ -142,11 +142,11 @@ export class BitbucketNock {
return interceptor;
}

mockBitbucketPullRequestsResponse(args: {
mockBitbucketIssuesOrPullRequestsResponse(args: {
states?: BitbucketPullRequestState | BitbucketPullRequestState[];
updatedAt?: number;
withDiffStat?: boolean;
}): Paginated<PullRequest> {
}): Paginated<PullRequest | Issue> {
const pullRequests: PullRequest[] = [];
if (!args.states) {
return {
Expand Down

0 comments on commit cfea49b

Please sign in to comment.