Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 10, 2023
1 parent 853e6fd commit 9d282d6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
8 changes: 7 additions & 1 deletion __tests__/classes/issues-processor-mock.ts
Expand Up @@ -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(
Expand All @@ -18,7 +19,8 @@ export class IssuesProcessorMock extends IssuesProcessor {
issue: Issue,
label: string
) => Promise<string | undefined>,
getPullRequest?: (issue: Issue) => Promise<IPullRequest | undefined | void>
getPullRequest?: (issue: Issue) => Promise<IPullRequest | undefined | void>,
getIssueEvents?: (issue: Issue) => Promise<IIssueEvent[]>
) {
super(options, state);

Expand All @@ -37,5 +39,9 @@ export class IssuesProcessorMock extends IssuesProcessor {
if (getPullRequest) {
this.getPullRequest = getPullRequest;
}

if (getIssueEvents) {
this.getIssueEvents = getIssueEvents;
}
}
}
89 changes: 89 additions & 0 deletions __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<Issue[]> => {
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);
});
});
2 changes: 1 addition & 1 deletion src/classes/issues-processor.ts
Expand Up @@ -195,7 +195,7 @@ export class IssuesProcessor {

private _lastIssueEvents: IIssueEvent[] = [];
private _lastIssueEventsIssueId = -1;
async getIssueEvents(issue: Issue): Promise<IIssueEvent[]> {
private async getIssueEvents(issue: Issue): Promise<IIssueEvent[]> {
if (issue.number !== this._lastIssueEventsIssueId) {
const options = this.client.rest.issues.listEvents.endpoint.merge({
owner: context.repo.owner,
Expand Down

0 comments on commit 9d282d6

Please sign in to comment.