refactor(presets): Reorder code and fix mocks (#9421)

This commit is contained in:
Sergei Zharinov 2021-04-07 11:50:55 +04:00 committed by GitHub
parent 36f8d1df0e
commit 612be5e449
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 87 deletions

View file

@ -11,7 +11,11 @@ Array [
] ]
`; `;
exports[`config/presets/local/index getPreset() forwards to custom bitbucket-server 2`] = `undefined`; exports[`config/presets/local/index getPreset() forwards to custom bitbucket-server 2`] = `
Object {
"resolved": "preset",
}
`;
exports[`config/presets/local/index getPreset() forwards to custom gitea 1`] = ` exports[`config/presets/local/index getPreset() forwards to custom gitea 1`] = `
Array [ Array [
@ -24,7 +28,11 @@ Array [
] ]
`; `;
exports[`config/presets/local/index getPreset() forwards to custom gitea 2`] = `undefined`; exports[`config/presets/local/index getPreset() forwards to custom gitea 2`] = `
Object {
"resolved": "preset",
}
`;
exports[`config/presets/local/index getPreset() forwards to custom github 1`] = ` exports[`config/presets/local/index getPreset() forwards to custom github 1`] = `
Array [ Array [
@ -71,7 +79,11 @@ Array [
] ]
`; `;
exports[`config/presets/local/index getPreset() forwards to gitea 2`] = `undefined`; exports[`config/presets/local/index getPreset() forwards to gitea 2`] = `
Object {
"resolved": "preset",
}
`;
exports[`config/presets/local/index getPreset() forwards to github 1`] = ` exports[`config/presets/local/index getPreset() forwards to github 1`] = `
Array [ Array [

View file

@ -5,21 +5,24 @@ import * as _github from '../github';
import * as _gitlab from '../gitlab'; import * as _gitlab from '../gitlab';
import * as local from '.'; import * as local from '.';
jest.mock('../gitlab');
jest.mock('../github');
jest.mock('../gitea');
jest.mock('../bitbucket-server'); jest.mock('../bitbucket-server');
jest.mock('../gitea');
jest.mock('../github');
jest.mock('../gitlab');
const gitlab = mocked(_gitlab);
const github = mocked(_github);
const gitea = mocked(_gitea);
const bitbucketServer = mocked(_bitbucketServer); const bitbucketServer = mocked(_bitbucketServer);
const gitea = mocked(_gitea);
const github = mocked(_github);
const gitlab = mocked(_gitlab);
describe(getName(__filename), () => { describe(getName(__filename), () => {
beforeEach(() => { beforeEach(() => {
jest.resetAllMocks(); jest.resetAllMocks();
gitlab.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' }); const preset = { resolved: 'preset' };
github.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' }); bitbucketServer.getPresetFromEndpoint.mockResolvedValueOnce(preset);
gitea.getPresetFromEndpoint.mockResolvedValueOnce(preset);
github.getPresetFromEndpoint.mockResolvedValueOnce(preset);
gitlab.getPresetFromEndpoint.mockResolvedValueOnce(preset);
}); });
describe('getPreset()', () => { describe('getPreset()', () => {
it('throws for unsupported platform', async () => { it('throws for unsupported platform', async () => {
@ -44,50 +47,19 @@ describe(getName(__filename), () => {
}); });
}).rejects.toThrow(); }).rejects.toThrow();
}); });
it('forwards to gitlab', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
presetName: 'default',
baseConfig: {
platform: 'GitLab',
},
});
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot();
});
it('forwards to custom gitlab', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
presetName: 'default',
baseConfig: {
platform: 'gitlab',
endpoint: 'https://gitlab.example.com/api/v4',
},
});
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot();
});
it('forwards to github', async () => { it('forwards to custom bitbucket-server', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
baseConfig: {
platform: 'github',
},
});
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot();
});
it('forwards to custom github', async () => {
const content = await local.getPreset({ const content = await local.getPreset({
packageName: 'some/repo', packageName: 'some/repo',
presetName: 'default', presetName: 'default',
baseConfig: { baseConfig: {
platform: 'github', platform: 'bitbucket-server',
endpoint: 'https://api.github.example.com', endpoint: 'https://git.example.com',
}, },
}); });
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot(); expect(
bitbucketServer.getPresetFromEndpoint.mock.calls
).toMatchSnapshot();
expect(content).toMatchSnapshot(); expect(content).toMatchSnapshot();
}); });
@ -114,18 +86,50 @@ describe(getName(__filename), () => {
expect(content).toMatchSnapshot(); expect(content).toMatchSnapshot();
}); });
it('forwards to custom bitbucket-server', async () => { it('forwards to github', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
baseConfig: {
platform: 'github',
},
});
expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot();
});
it('forwards to custom github', async () => {
const content = await local.getPreset({ const content = await local.getPreset({
packageName: 'some/repo', packageName: 'some/repo',
presetName: 'default', presetName: 'default',
baseConfig: { baseConfig: {
platform: 'bitbucket-server', platform: 'github',
endpoint: 'https://git.example.com', endpoint: 'https://api.github.example.com',
}, },
}); });
expect( expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
bitbucketServer.getPresetFromEndpoint.mock.calls expect(content).toMatchSnapshot();
).toMatchSnapshot(); });
it('forwards to gitlab', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
presetName: 'default',
baseConfig: {
platform: 'GitLab',
},
});
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot();
});
it('forwards to custom gitlab', async () => {
const content = await local.getPreset({
packageName: 'some/repo',
presetName: 'default',
baseConfig: {
platform: 'gitlab',
endpoint: 'https://gitlab.example.com/api/v4',
},
});
expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot();
expect(content).toMatchSnapshot(); expect(content).toMatchSnapshot();
}); });
}); });

View file

@ -10,6 +10,13 @@ import * as github from '../github';
import * as gitlab from '../gitlab'; import * as gitlab from '../gitlab';
import type { Preset, PresetConfig } from '../types'; import type { Preset, PresetConfig } from '../types';
const resolvers = {
[PLATFORM_TYPE_BITBUCKET_SERVER]: bitbucketServer,
[PLATFORM_TYPE_GITEA]: gitea,
[PLATFORM_TYPE_GITHUB]: github,
[PLATFORM_TYPE_GITLAB]: gitlab,
};
export function getPreset({ export function getPreset({
packageName: pkgName, packageName: pkgName,
presetName = 'default', presetName = 'default',
@ -20,38 +27,16 @@ export function getPreset({
if (!platform) { if (!platform) {
throw new Error(`Missing platform config for local preset.`); throw new Error(`Missing platform config for local preset.`);
} }
switch (platform.toLowerCase()) { const resolver = resolvers[platform.toLowerCase()];
case PLATFORM_TYPE_GITLAB: if (!resolver) {
return gitlab.getPresetFromEndpoint( throw new Error(
pkgName, `Unsupported platform '${baseConfig.platform}' for local preset.`
presetName, );
presetPath,
endpoint
);
case PLATFORM_TYPE_GITHUB:
return github.getPresetFromEndpoint(
pkgName,
presetName,
presetPath,
endpoint
);
case PLATFORM_TYPE_BITBUCKET_SERVER:
return bitbucketServer.getPresetFromEndpoint(
pkgName,
presetName,
presetPath,
endpoint
);
case PLATFORM_TYPE_GITEA:
return gitea.getPresetFromEndpoint(
pkgName,
presetName,
presetPath,
endpoint
);
default:
throw new Error(
`Unsupported platform '${baseConfig.platform}' for local preset.`
);
} }
return resolver.getPresetFromEndpoint(
pkgName,
presetName,
presetPath,
endpoint
);
} }