diff --git a/src/practices/LanguageIndependent/TimeToSolvePractice.spec.ts b/src/practices/LanguageIndependent/TimeToSolvePractice.spec.ts index 343f639ca..b78076384 100644 --- a/src/practices/LanguageIndependent/TimeToSolvePractice.spec.ts +++ b/src/practices/LanguageIndependent/TimeToSolvePractice.spec.ts @@ -1,22 +1,29 @@ +import moment from 'moment'; import nock from 'nock'; -import { PullRequestState } from '../../inspectors/ICollaborationInspector'; +import { CollaborationInspector } from '../../inspectors'; import { createTestContainer, TestContainerContext } from '../../inversify.config'; import { PracticeEvaluationResult } from '../../model'; -import { GitHubNock } from '../../test/helpers/gitHubNock'; +import { BitbucketPullRequestState } from '../../services'; +import { BitbucketNock } from '../../test/helpers/bitbucketNock'; import { TimeToSolvePractice } from './TimeToSolvePractice'; describe('TimeToSolvePractice', () => { let practice: TimeToSolvePractice; let containerCtx: TestContainerContext; + let bitbucketNock: BitbucketNock; + const MockedCollaborationInspector = >(CollaborationInspector); + let mockCollaborationInspector: CollaborationInspector; beforeEach(async () => { nock.cleanAll(); + bitbucketNock = new BitbucketNock('pypy', 'pypy'); }); beforeAll(() => { containerCtx = createTestContainer(); containerCtx.container.bind('TimeToSolvePractice').to(TimeToSolvePractice); practice = containerCtx.container.get('TimeToSolvePractice'); + mockCollaborationInspector = new MockedCollaborationInspector(); }); afterEach(async () => { @@ -24,14 +31,63 @@ describe('TimeToSolvePractice', () => { containerCtx.practiceContext.fileInspector!.purgeCache(); }); - it('', async () => { - containerCtx.practiceContext.projectComponent.repositoryPath = 'https://github.com/octocat/Hello-World'; - new GitHubNock('1', 'octocat', 1296269, 'Hello-World').getPulls( - [{ number: 1347, state: 'open', title: 'new-feature', body: 'Please pull these awesome changes', head: 'new-topic', base: 'master' }], - PullRequestState.open, - ); + it('returns practicing if there are open pullrequests updated or created less than 30 days from now', async () => { + containerCtx.practiceContext.projectComponent.repositoryPath = 'https://bitbucket.org/pypy/pypy'; + nock(bitbucketNock.url) + .get('/users/pypy') + .reply(200); + bitbucketNock.getApiResponse('pullrequests', undefined, undefined, BitbucketPullRequestState.open); + const args = { states: BitbucketPullRequestState.open, updatedAt: Date.now() - moment.duration(10, 'days').asMilliseconds() }; + mockCollaborationInspector.getPullRequests = async () => { + return bitbucketNock.mockBitbucketPullRequestsResponse(args); + }; + + const evaluated = await practice.evaluate({ + ...containerCtx.practiceContext, + collaborationInspector: mockCollaborationInspector, + }); - const evaluated = await practice.evaluate(containerCtx.practiceContext); expect(evaluated).toEqual(PracticeEvaluationResult.practicing); }); + + it('returns practicing if there are open pullrequests updated or created more than 30 days from now', async () => { + containerCtx.practiceContext.projectComponent.repositoryPath = 'https://bitbucket.org/pypy/pypy'; + nock(bitbucketNock.url) + .get('/users/pypy') + .reply(200); + bitbucketNock.getApiResponse('pullrequests', undefined, undefined, BitbucketPullRequestState.open); + const args = { states: BitbucketPullRequestState.open, updatedAt: Date.now() - moment.duration(100, 'days').asMilliseconds() }; + mockCollaborationInspector.getPullRequests = async () => { + return bitbucketNock.mockBitbucketPullRequestsResponse(args); + }; + + const evaluated = await practice.evaluate({ + ...containerCtx.practiceContext, + collaborationInspector: mockCollaborationInspector, + }); + + expect(evaluated).toEqual(PracticeEvaluationResult.notPracticing); + }); + + it('returns practicing if there are no open pullrequest', async () => { + mockCollaborationInspector.getPullRequests = async () => { + return bitbucketNock.mockBitbucketPullRequestsResponse({}); + }; + + const evaluated = await practice.evaluate({ + ...containerCtx.practiceContext, + collaborationInspector: mockCollaborationInspector, + }); + expect(evaluated).toEqual(PracticeEvaluationResult.practicing); + }); + + it('returns always true, as it is always applicable', async () => { + const response = await practice.isApplicable(); + expect(response).toBe(true); + }); + + it('returns unknown if there is no collaborationInspector', async () => { + const evaluated = await practice.evaluate({ ...containerCtx.practiceContext, collaborationInspector: undefined }); + expect(evaluated).toEqual(PracticeEvaluationResult.unknown); + }); });