add tests for filterByClause
Signed-off-by: Paul Cowan <paul.cowan@cutting.scot>
This commit is contained in:
@@ -20,7 +20,8 @@ jest.mock('octokit', () => ({
|
||||
|
||||
import { ConfigApi, ErrorApi } from '@backstage/core-plugin-api';
|
||||
import { ForwardedError } from '@backstage/errors';
|
||||
import { gitHubIssuesApi } from './gitHubIssuesApi';
|
||||
import { createFilterByClause, gitHubIssuesApi } from './gitHubIssuesApi';
|
||||
import type { GithubIssuesFilters } from './gitHubIssuesApi';
|
||||
|
||||
function getFragment(
|
||||
filterBy = '',
|
||||
@@ -141,6 +142,30 @@ describe('gitHubIssuesApi', () => {
|
||||
getFragment(expectedFilterBy, expectedOrderBy),
|
||||
);
|
||||
});
|
||||
|
||||
describe('filterBy', () => {
|
||||
const cases: [GithubIssuesFilters | undefined, string][] = [
|
||||
[{}, ''],
|
||||
[undefined, ''],
|
||||
[{ states: ['OPEN'] }, 'states: OPEN'],
|
||||
[
|
||||
{ labels: ['bug', 'enhancement'], assignee: 'someone' },
|
||||
`labels: [ \"bug\", \"enhancement\"], assignee: "someone"`,
|
||||
],
|
||||
[
|
||||
{
|
||||
createdBy: 'someone',
|
||||
mentioned: 'someone else',
|
||||
milestone: 'milestone',
|
||||
},
|
||||
`createdBy: \"someone\", mentioned: \"someone else\", milestone: \"milestone\"`,
|
||||
],
|
||||
];
|
||||
|
||||
test.each(cases)('filterBy(%s) should be %s', (filterBy, expected) => {
|
||||
expect(createFilterByClause(filterBy)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should return data for repos with successfully retrieved issues when GitHub returns partial failure', async () => {
|
||||
|
||||
@@ -199,22 +199,20 @@ function formatFilterValue(
|
||||
return typeof value === 'string' ? `\"${value}\"` : `${value}`;
|
||||
}
|
||||
|
||||
function createFilterByClause(filterBy?: GithubIssuesFilters): string {
|
||||
/** @internal */
|
||||
export function createFilterByClause(filterBy?: GithubIssuesFilters): string {
|
||||
if (!filterBy) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Object.entries(filterBy)
|
||||
.flatMap(([field, value]) => {
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
.filter(value => value)
|
||||
.map(([field, value]) => {
|
||||
if (field === 'states') {
|
||||
return [`${field}: ${value.join(', ')}`];
|
||||
return `${field}: ${value.join(', ')}`;
|
||||
}
|
||||
|
||||
return [`${field}: ${formatFilterValue(value)}`];
|
||||
return `${field}: ${formatFilterValue(value)}`;
|
||||
})
|
||||
.join(', ');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user