import {Issue} from '../src/classes/issue'; import {IIssue} from '../src/interfaces/issue'; import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options'; import {ILabel} from '../src/interfaces/label'; import {IssuesProcessorMock} from './classes/issues-processor-mock'; import {DefaultProcessorOptions} from './constants/default-processor-options'; import {generateIssue} from './functions/generate-issue'; let issuesProcessorBuilder: IssuesProcessorBuilder; let issuesProcessor: IssuesProcessorMock; /** * @description * Assuming there is a comment on the issue */ describe('remove-issue-stale-when-updated option', (): void => { beforeEach((): void => { issuesProcessorBuilder = new IssuesProcessorBuilder(); }); describe('when the option "remove-issue-stale-when-updated" is disabled', (): void => { beforeEach((): void => { issuesProcessorBuilder.keepIssueStaleWhenUpdated(); }); test('should not remove the stale label on the issue', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.staleIssues([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(0); }); test('should remove the stale label on the pull request', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.stalePrs([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); }); describe('when the option "remove-issue-stale-when-updated" is enabled', (): void => { beforeEach((): void => { issuesProcessorBuilder.removeIssueStaleWhenUpdated(); }); test('should remove the stale label on the issue', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.staleIssues([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); test('should remove the stale label on the pull request', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.stalePrs([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); }); }); describe('remove-pr-stale-when-updated option', (): void => { beforeEach((): void => { issuesProcessorBuilder = new IssuesProcessorBuilder(); }); describe('when the option "remove-pr-stale-when-updated" is disabled', (): void => { beforeEach((): void => { issuesProcessorBuilder.keepPrStaleWhenUpdated(); }); test('should remove the stale label on the issue', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.staleIssues([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); test('should not remove the stale label on the pull request', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.stalePrs([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(0); }); }); describe('when the option "remove-pr-stale-when-updated" is enabled', (): void => { beforeEach((): void => { issuesProcessorBuilder.removePrStaleWhenUpdated(); }); test('should remove the stale label on the issue', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.staleIssues([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); test('should remove the stale label on the pull request', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder.stalePrs([{}]).build(); await issuesProcessor.processIssues(); expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); }); }); class IssuesProcessorBuilder { private _options: IIssuesProcessorOptions = { ...DefaultProcessorOptions, removeIssueStaleWhenUpdated: true, removePrStaleWhenUpdated: true }; private _issues: Issue[] = []; keepIssueStaleWhenUpdated(): IssuesProcessorBuilder { this._options.removeIssueStaleWhenUpdated = false; return this; } removeIssueStaleWhenUpdated(): IssuesProcessorBuilder { this._options.removeIssueStaleWhenUpdated = true; return this; } keepPrStaleWhenUpdated(): IssuesProcessorBuilder { this._options.removePrStaleWhenUpdated = false; return this; } removePrStaleWhenUpdated(): IssuesProcessorBuilder { this._options.removePrStaleWhenUpdated = true; return this; } issuesOrPrs(issues: Partial[]): IssuesProcessorBuilder { this._issues = issues.map( (issue: Readonly>, index: Readonly): Issue => generateIssue( this._options, index, issue.title ?? 'dummy-title', issue.updated_at ?? new Date().toDateString(), issue.created_at ?? new Date().toDateString(), !!issue.pull_request, issue.labels ? issue.labels.map(label => label.name) : [] ) ); return this; } issues(issues: Partial[]): IssuesProcessorBuilder { this.issuesOrPrs( issues.map((issue: Readonly>): Partial => { return { ...issue, pull_request: null }; }) ); return this; } staleIssues(issues: Partial[]): IssuesProcessorBuilder { this.issues( issues.map((issue: Readonly>): Partial => { return { ...issue, updated_at: '2020-01-01T17:00:00Z', created_at: '2020-01-01T17:00:00Z', labels: issue.labels?.map((label: Readonly): ILabel => { return { ...label, name: 'Stale' }; }) ?? [ { name: 'Stale' } ] }; }) ); return this; } prs(issues: Partial[]): IssuesProcessorBuilder { this.issuesOrPrs( issues.map((issue: Readonly>): Partial => { return { ...issue, pull_request: {key: 'value'} }; }) ); return this; } stalePrs(issues: Partial[]): IssuesProcessorBuilder { this.prs( issues.map((issue: Readonly>): Partial => { return { ...issue, updated_at: '2020-01-01T17:00:00Z', created_at: '2020-01-01T17:00:00Z', labels: issue.labels?.map((label: Readonly): ILabel => { return { ...label, name: 'Stale' }; }) ?? [ { name: 'Stale' } ] }; }) ); return this; } build(): IssuesProcessorMock { return new IssuesProcessorMock( this._options, async p => (p === 1 ? this._issues : []), async () => [ { user: { login: 'notme', type: 'User' }, body: 'body' } ], async () => new Date().toDateString() ); } }