@apistudio/apim-cli
Version:
CLI for API Management Products
47 lines (40 loc) • 1.8 kB
text/typescript
import { isJSON, parseJSON } from '../../src/functions/json.helper.js';
describe('JSON Helper Function tests', () => {
describe('isJSON tests', () => {
it('should return true for valid JSON data', () => {
expect(isJSON({ key: 'value' })).toBe(true);
expect(isJSON({ 'key': 'value' })).toBe(true);
});
it('should return false for invalid JSON data', () => {
expect(isJSON('invalid-json')).toBe(false);
});
it('should return false when an error occurs', () => {
jest.spyOn(JSON, 'stringify').mockImplementationOnce(() => { throw new Error('Invalid JSON'); });
expect(isJSON({ key: 'value' })).toBe(false);
});
});
describe('parseJSON tests', () => {
it('should parse valid JSON string correctly', () => {
const jsonContent = '{"key": "value"}';
const expectedOutput = { key: 'value' };
const result = parseJSON(jsonContent);
expect(result).toEqual(expectedOutput);
});
it('should throw an error for invalid JSON string', () => {
const invalidJsonContent = '{key: value}';
expect(() => parseJSON(invalidJsonContent)).toThrow(SyntaxError);
});
it('should parse an empty JSON object correctly', () => {
const jsonContent = '{}';
const expectedOutput = {};
const result = parseJSON(jsonContent);
expect(result).toEqual(expectedOutput);
});
it('should handle null as JSON input correctly', () => {
const jsonContent = 'null';
const expectedOutput = null;
const result = parseJSON(jsonContent);
expect(result).toBe(expectedOutput);
});
});
});