stale/__tests__/any-of-labels.spec.ts
Geoffrey Testelin 419a53bc05
feat(statistics): display some stats in the logs (#337)
* test: add more coverage

* docs: reorder and enhance typo

* docs(contributing): add more information about the npm scripts

* feat(statistics): add simple statistics

* feat(statistics): add more stats

* refactor(issues-processor): remove some options from the constructor

it should have been only useful for the tests

* feat(statistics): add stats for new stale or undo stale issues

* chore(rebase): handle rebase conflicts
2021-03-01 15:34:35 -05:00

108 lines
3.1 KiB
TypeScript

import {Issue} from '../src/classes/issue';
import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options';
import {IssuesProcessorMock} from './classes/issues-processor-mock';
import {DefaultProcessorOptions} from './constants/default-processor-options';
import {generateIssue} from './functions/generate-issue';
describe('any-of-labels option', () => {
test('should do nothing when not set', async () => {
const sut = new IssuesProcessorBuilder()
.emptyAnyOfLabels()
.issues([{labels: [{name: 'some-label'}]}])
.build();
await sut.processIssues();
expect(sut.staleIssues).toHaveLength(1);
});
test('should skip it when none of the issue labels match', async () => {
const sut = new IssuesProcessorBuilder()
.anyOfLabels('skip-this-issue,and-this-one')
.issues([{labels: [{name: 'some-label'}, {name: 'some-other-label'}]}])
.build();
await sut.processIssues();
expect(sut.staleIssues).toHaveLength(0);
});
test('should skip it when the issue has no labels', async () => {
const sut = new IssuesProcessorBuilder()
.anyOfLabels('skip-this-issue,and-this-one')
.issues([{labels: []}])
.build();
await sut.processIssues();
expect(sut.staleIssues).toHaveLength(0);
});
test('should process it when one of the issue labels match', async () => {
const sut = new IssuesProcessorBuilder()
.anyOfLabels('skip-this-issue,and-this-one')
.issues([{labels: [{name: 'some-label'}, {name: 'skip-this-issue'}]}])
.build();
await sut.processIssues();
expect(sut.staleIssues).toHaveLength(1);
});
test('should process it when all the issue labels match', async () => {
const sut = new IssuesProcessorBuilder()
.anyOfLabels('skip-this-issue,and-this-one')
.issues([{labels: [{name: 'and-this-one'}, {name: 'skip-this-issue'}]}])
.build();
await sut.processIssues();
expect(sut.staleIssues).toHaveLength(1);
});
});
class IssuesProcessorBuilder {
private _options: IIssuesProcessorOptions;
private _issues: Issue[];
constructor() {
this._options = {...DefaultProcessorOptions};
this._issues = [];
}
anyOfLabels(labels: string): IssuesProcessorBuilder {
this._options.anyOfLabels = labels;
return this;
}
emptyAnyOfLabels(): IssuesProcessorBuilder {
return this.anyOfLabels('');
}
issues(issues: Partial<Issue>[]): IssuesProcessorBuilder {
this._issues = issues.map(
(issue, index): Issue =>
generateIssue(
this._options,
index,
issue.title || 'Issue title',
issue.updated_at || '2000-01-01T00:00:00Z', // we only care about stale/expired issues here
issue.created_at || '2000-01-01T00:00:00Z',
issue.isPullRequest || false,
issue.labels ? issue.labels.map(label => label.name) : []
)
);
return this;
}
build(): IssuesProcessorMock {
return new IssuesProcessorMock(
this._options,
async () => 'abot',
async p => (p === 1 ? this._issues : []),
async () => [],
async () => new Date().toDateString()
);
}
}