Add test suites for JSONFunctions and SerializableObject

This commit is contained in:
Drantaz 2023-10-27 20:24:33 +00:00
parent 3f7d186db0
commit 4ffe215665
2 changed files with 96 additions and 0 deletions

View 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);
});
});
});

View 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);
});
});
});