Add tests to verify the searchParams are added as expected

Signed-off-by: ivgo <ivgo@spreadgroup.com>
This commit is contained in:
ivgo
2023-09-29 08:39:59 +02:00
parent 2da314370c
commit c11113dded
@@ -120,6 +120,14 @@ function setupFakeHasFileEndpoint(srv: SetupServer, apiBaseUrl: string) {
);
}
function setupFakeURLReturnEndpoint(srv: SetupServer, url: string) {
srv.use(
rest.get(url, (req, res, ctx) => {
return res(ctx.json([{ endpoint: req.url.toString() }]));
}),
);
}
describe('GitLabClient', () => {
describe('isSelfManaged', () => {
it('returns true if self managed instance', () => {
@@ -933,3 +941,84 @@ describe('hasFile', () => {
expect(hasFile).toBe(false);
});
});
describe('pagedRequest search params', () => {
beforeEach(() => {
// setup fake paginated endpoint with 4 pages each returning one item
setupFakeURLReturnEndpoint(server, FAKE_PAGED_URL);
});
it('no search params provided', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{ endpoint: 'https://example.com/api/v4/some-endpoint' },
]);
});
it('defined numeric search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ page: 1, per_page: 50 },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint: 'https://example.com/api/v4/some-endpoint?page=1&per_page=50',
},
]);
});
it('defined string search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ test: 'value', empty: '' },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint: 'https://example.com/api/v4/some-endpoint?test=value',
},
]);
});
it('defined boolean search params', async () => {
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const { items } = await client.pagedRequest<{ endpoint: string }>(
FAKE_PAGED_ENDPOINT,
{ active: true, archived: false },
);
// fake page contains exactly one item
expect(items).toHaveLength(1);
expect(items).toEqual([
{
endpoint:
'https://example.com/api/v4/some-endpoint?active=true&archived=false',
},
]);
});
});