feat: support for webhooks to the split command (#550)

Co-authored-by: Anton_Kozachuk <anton.kozachuk@cloudmobilizd.com>
Co-authored-by: romanhotsiy <gotsijroman@gmail.com>
This commit is contained in:
Anton Kozachuk
2022-02-17 16:22:45 +02:00
committed by GitHub
parent 964c2ba797
commit 98d6122a9f
16 changed files with 629 additions and 91 deletions

View File

@@ -1,32 +1,76 @@
import { iteratePaths } from '../index';
import { iteratePathItems, handleSplit } from '../index';
import * as path from 'path';
import * as openapiCore from '@redocly/openapi-core';
import {
ComponentsFiles,
} from '../types';
import { blue, green } from 'colorette';
jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'),
writeYaml: jest.fn(),
}));
describe('#split', () => {
it('should have correct paths for mac', () => {
const pathsDir = 'test/paths';
const openapiDir = 'test';
jest.mock('@redocly/openapi-core', () => ({
...jest.requireActual('@redocly/openapi-core'),
isRef: jest.fn(),
}));
describe('#split', () => {
const openapiDir = 'test';
const componentsFiles: ComponentsFiles = {};
it('should split the file and show the success message', async () => {
const filePath = "./packages/cli/src/commands/split/__tests__/fixtures/spec.json";
jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
await handleSplit (
{
entrypoint: filePath,
outDir: openapiDir,
}
);
expect(process.stderr.write).toBeCalledTimes(2);
expect((process.stderr.write as jest.Mock).mock.calls[0][0]).toBe(
`🪓 Document: ${blue(filePath!)} ${green('is successfully split')}
and all related files are saved to the directory: ${blue(openapiDir)} \n`
);
expect((process.stderr.write as jest.Mock).mock.calls[1][0]).toContain(
`${filePath}: split processed in <test>ms`
);
});
it('should have correct path with paths', () => {
const openapi = require("./fixtures/spec.json");
jest.spyOn(openapiCore, 'slash').mockImplementation(() => 'paths/test.yaml');
jest.spyOn(path, 'relative').mockImplementation(() => 'paths/test.yaml');
iteratePaths(require("./fixtures/spec.json"), pathsDir, openapiDir);
iteratePathItems(openapi.paths, openapiDir, path.join(openapiDir, 'paths'), componentsFiles);
expect(openapiCore.slash).toHaveBeenCalledWith('paths/test.yaml');
expect(path.relative).toHaveBeenCalledWith('test', 'test/paths/test.yaml');
});
it('should have correct paths for windows', () => {
const pathsDir = 'test\\paths';
const openapiDir = 'test';
it('should have correct path with webhooks', () => {
const openapi = require("./fixtures/webhooks.json");
jest.spyOn(openapiCore, 'slash').mockImplementation(() => 'paths\\test.yaml');
jest.spyOn(path, 'relative').mockImplementation(() => 'paths\\test.yaml');
iteratePaths(require("./fixtures/spec.json"), pathsDir, openapiDir);
expect(openapiCore.slash).toHaveBeenCalledWith('paths\\test.yaml');
expect(path.relative).toHaveBeenCalledWith('test', 'test\\paths/test.yaml');
jest.spyOn(openapiCore, 'slash').mockImplementation(() => 'webhooks/test.yaml');
jest.spyOn(path, 'relative').mockImplementation(() => 'webhooks/test.yaml');
iteratePathItems(openapi.webhooks, openapiDir, path.join(openapiDir, 'webhooks'), componentsFiles, 'webhook_');
expect(openapiCore.slash).toHaveBeenCalledWith('webhooks/test.yaml');
expect(path.relative).toHaveBeenCalledWith('test', 'test/webhooks/test.yaml');
});
it('should have correct path with x-webhooks', () => {
const openapi = require("./fixtures/spec.json");
jest.spyOn(openapiCore, 'slash').mockImplementation(() => 'webhooks/test.yaml');
jest.spyOn(path, 'relative').mockImplementation(() => 'webhooks/test.yaml');
iteratePathItems(openapi['x-webhooks'], openapiDir, path.join(openapiDir, 'webhooks'), componentsFiles, 'webhook_');
expect(openapiCore.slash).toHaveBeenCalledWith('webhooks/test.yaml');
expect(path.relative).toHaveBeenCalledWith('test', 'test/webhooks/test.yaml');
});
});