@apistudio/apim-cli
Version:
CLI for API Management Products
67 lines (60 loc) • 3.2 kB
text/typescript
import { isOpenAPI } from '../../src/functions/open-api.helper.js';
import { isSwagger } from '../../src/functions/swagger-api.helper.js';
import { getDocumentBasedOnLanguage } from '../../src/functions/data.parser.js';
jest.mock('../../src/functions/data.parser', () => ({
getDocumentBasedOnLanguage: jest.fn()
}));
describe('Rest API Helper Function tests', () => {
describe('isOpenAPI tests', () => {
it('should return true if the parsed YAML has an openapi property', () => {
const fileContent = 'openapi: 3.1.1';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue({ openapi: '3.1.1' });
const result = isOpenAPI(fileContent, language);
expect(result).toBe(true);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
it('should return false if the parsed YAML does not have an openapi property', () => {
const fileContent = 'key: value';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue({ key: 'value' });
const result = isOpenAPI(fileContent, language);
expect(result).toBe(false);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
it('should return false if the parsed YAML is null', () => {
const fileContent = '';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue(null);
const result = isOpenAPI(fileContent, language);
expect(result).toBe(false);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
});
describe('isSwagger tests', () => {
it('should return true if the parsed YAML has a swagger property', () => {
const fileContent = 'swagger: 2.0';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue({ swagger: '2.0' });
const result = isSwagger(fileContent, language);
expect(result).toBe(true);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
it('should return false if the parsed YAML does not have a swagger property', () => {
const fileContent = 'key: value';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue({ key: 'value' });
const result = isSwagger(fileContent, language);
expect(result).toBe(false);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
it('should return false if the parsed YAML is null', () => {
const fileContent = '';
const language = 'yaml';
(getDocumentBasedOnLanguage as jest.Mock).mockReturnValue(null);
const result = isSwagger(fileContent, language);
expect(result).toBe(false);
expect(getDocumentBasedOnLanguage).toHaveBeenCalledWith(fileContent, language);
});
});
});