mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-22 15:24:55 +00:00
Add test suites for JSONFunctions and SerializableObject
This commit is contained in:
parent
3f7d186db0
commit
4ffe215665
52
Common/Tests/Types/JSONFunctions.test.ts
Normal file
52
Common/Tests/Types/JSONFunctions.test.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import BaseModel from '../../Models/BaseModel';
|
||||
import { JSONObject } from '../../Types/JSON';
|
||||
import JSONFunctions from '../../Types/JSONFunctions';
|
||||
|
||||
describe('JSONFunctions Class', () => {
|
||||
let baseModel: BaseModel;
|
||||
|
||||
beforeEach(() => {
|
||||
baseModel = new BaseModel();
|
||||
});
|
||||
|
||||
describe('isEmptyObject Method', () => {
|
||||
test('Returns true for an empty object', () => {
|
||||
const emptyObj: JSONObject = {};
|
||||
expect(JSONFunctions.isEmptyObject(emptyObj)).toBe(true);
|
||||
});
|
||||
|
||||
test('Returns false for a non-empty object', () => {
|
||||
const nonEmptyObj: JSONObject = { key: 'value' };
|
||||
expect(JSONFunctions.isEmptyObject(nonEmptyObj)).toBe(false);
|
||||
});
|
||||
|
||||
test('Returns true for null or undefined', () => {
|
||||
expect(JSONFunctions.isEmptyObject(null)).toBe(true);
|
||||
expect(JSONFunctions.isEmptyObject(undefined)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON and fromJSON Methods', () => {
|
||||
test('toJSON returns a valid JSON object', () => {
|
||||
const json: JSONObject = JSONFunctions.toJSON(baseModel, BaseModel);
|
||||
expect(json).toEqual(expect.objectContaining({}));
|
||||
});
|
||||
|
||||
test('toJSONObject returns a valid JSON object', () => {
|
||||
const json: JSONObject = JSONFunctions.toJSONObject(
|
||||
baseModel,
|
||||
BaseModel
|
||||
);
|
||||
expect(json).toEqual(expect.objectContaining({}));
|
||||
});
|
||||
|
||||
test('fromJSON returns a BaseModel instance', () => {
|
||||
const json: JSONObject = { name: 'oneuptime' };
|
||||
const result: BaseModel | BaseModel[] = JSONFunctions.fromJSON(
|
||||
json,
|
||||
BaseModel
|
||||
);
|
||||
expect(result).toBeInstanceOf(BaseModel);
|
||||
});
|
||||
});
|
||||
});
|
44
Common/Tests/Types/SerializableObject.test.ts
Normal file
44
Common/Tests/Types/SerializableObject.test.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import NotImplementedException from '../../Types/Exception/NotImplementedException';
|
||||
import { JSONObject } from '../../Types/JSON';
|
||||
import SerializableObject from '../../Types/SerializableObject';
|
||||
|
||||
describe('SerializableObject Class', () => {
|
||||
let serializableObject: SerializableObject;
|
||||
|
||||
beforeEach(() => {
|
||||
serializableObject = new SerializableObject();
|
||||
});
|
||||
|
||||
test('Constructor initializes an instance of SerializableObject', () => {
|
||||
expect(serializableObject).toBeInstanceOf(SerializableObject);
|
||||
});
|
||||
|
||||
describe('toJSON Method', () => {
|
||||
test('Throws NotImplementedException when called', () => {
|
||||
expect(() => {
|
||||
return serializableObject.toJSON();
|
||||
}).toThrow(NotImplementedException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromJSON Method', () => {
|
||||
test('Throws NotImplementedException when called', () => {
|
||||
expect(() => {
|
||||
return SerializableObject.fromJSON({});
|
||||
}).toThrow(NotImplementedException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromJSON Instance Method', () => {
|
||||
test('Returns the result from the static fromJSON method', () => {
|
||||
const json: JSONObject = { key: 'value' };
|
||||
const expectedResult: SerializableObject = new SerializableObject();
|
||||
jest.spyOn(SerializableObject, 'fromJSON').mockReturnValue(
|
||||
expectedResult
|
||||
);
|
||||
const result: SerializableObject =
|
||||
serializableObject.fromJSON(json);
|
||||
expect(result).toBe(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user