From 98de908f82fe7f454a1ab9eca1b66527a1fc5ab3 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Fri, 7 Jul 2023 11:59:39 +0200 Subject: [PATCH] add unit tests --- __tests__/classes/issues-processor-mock.ts | 8 +- __tests__/pinned.spec.ts | 89 ++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 __tests__/pinned.spec.ts diff --git a/__tests__/classes/issues-processor-mock.ts b/__tests__/classes/issues-processor-mock.ts index 3b2d488fe..bd4c06e11 100644 --- a/__tests__/classes/issues-processor-mock.ts +++ b/__tests__/classes/issues-processor-mock.ts @@ -4,6 +4,7 @@ import {IComment} from '../../src/interfaces/comment'; import {IIssuesProcessorOptions} from '../../src/interfaces/issues-processor-options'; import {IPullRequest} from '../../src/interfaces/pull-request'; import {IState} from '../../src/interfaces/state/state'; +import {IIssueEvent} from '../../src/interfaces/issue-event'; export class IssuesProcessorMock extends IssuesProcessor { constructor( @@ -18,7 +19,8 @@ export class IssuesProcessorMock extends IssuesProcessor { issue: Issue, label: string ) => Promise, - getPullRequest?: (issue: Issue) => Promise + getPullRequest?: (issue: Issue) => Promise, + getIssueEvents?: (issue: Issue) => Promise ) { super(options, state); @@ -37,5 +39,9 @@ export class IssuesProcessorMock extends IssuesProcessor { if (getPullRequest) { this.getPullRequest = getPullRequest; } + + if (getIssueEvents) { + this.getIssueEvents = getIssueEvents; + } } } diff --git a/__tests__/pinned.spec.ts b/__tests__/pinned.spec.ts new file mode 100644 index 000000000..fa2a1a2b1 --- /dev/null +++ b/__tests__/pinned.spec.ts @@ -0,0 +1,89 @@ +import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options'; +import {DefaultProcessorOptions} from './constants/default-processor-options'; +import {Issue} from '../src/classes/issue'; +import {generateIssue} from './functions/generate-issue'; +import {IssuesProcessorMock} from './classes/issues-processor-mock'; +import {alwaysFalseStateMock} from './classes/state-mock'; +import {IIssueEvent} from '../src/interfaces/issue-event'; + +const opts: IIssuesProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0 +}; +const testIssueList = async (page: number): Promise => { + return page == 1 + ? [ + generateIssue(opts, 1, 'Issue 1', '2020-01-01T17:00:00Z'), + generateIssue(opts, 2, 'Issue 2', '2020-01-01T17:00:00Z') + ] + : []; +}; + +const pinnedEvent: IIssueEvent = { + created_at: '2020-01-01T17:00:00Z', + event: 'pinned', + label: {} +}; +const unpinnedEvent: IIssueEvent = { + created_at: '2020-01-01T17:00:00Z', + event: 'unpinned', + label: {} +}; +describe('exempt-pinned-issues options', (): void => { + it('pinned issues should be skipped if exemptPinnedIssues true', async () => { + const processor = new IssuesProcessorMock( + {...opts, exemptPinnedIssues: true}, + alwaysFalseStateMock, + testIssueList, + async () => [], + async () => new Date().toDateString(), + async (issue: Issue) => undefined, + async (issue: Issue) => (issue.number === 1 ? [pinnedEvent] : []) + ); + await processor.processIssues(1); + expect(processor.staleIssues).toHaveLength(1); + }); + + it('pinned issues should not be skipped if exemptPinnedIssues false', async () => { + const processor = new IssuesProcessorMock( + {...opts, exemptPinnedIssues: false}, + alwaysFalseStateMock, + testIssueList, + async () => [], + async () => new Date().toDateString(), + async (issue: Issue) => undefined, + async (issue: Issue) => (issue.number === 1 ? [pinnedEvent] : []) + ); + await processor.processIssues(1); + expect(processor.staleIssues).toHaveLength(2); + }); + + it('pinned issues should not be skipped if exemptPinnedIssues true but it was unpinned', async () => { + const processor = new IssuesProcessorMock( + {...opts, exemptPinnedIssues: true}, + alwaysFalseStateMock, + testIssueList, + async () => [], + async () => new Date().toDateString(), + async (issue: Issue) => undefined, + async (issue: Issue) => + issue.number === 1 ? [unpinnedEvent, pinnedEvent] : [] + ); + await processor.processIssues(1); + expect(processor.staleIssues).toHaveLength(2); + }); + it('pinned issues should not be skipped if exemptPinnedIssues true and it was unpinned and pinned', async () => { + const processor = new IssuesProcessorMock( + {...opts, exemptPinnedIssues: true}, + alwaysFalseStateMock, + testIssueList, + async () => [], + async () => new Date().toDateString(), + async (issue: Issue) => undefined, + async (issue: Issue) => + issue.number === 1 ? [pinnedEvent, unpinnedEvent, pinnedEvent] : [] + ); + await processor.processIssues(1); + expect(processor.staleIssues).toHaveLength(1); + }); +});