@aire-ux/aire-condensation
Version:
Client-side serialization library for Aire-UX
91 lines (76 loc) • 2.37 kB
text/typescript
import "reflect-metadata";
import { Condensation } from "@condensation/condensation";
import {
Configuration,
Property,
RootElement,
} from "@condensation/root-element";
import { PropertyDefinition } from "@condensation/type-registry";
test("root element should be able to register a type with the type registry", () => {
class TestElement {}
expect(Condensation.typeRegistry.contains(TestElement)).toBeTruthy();
expect(
Condensation.typeRegistry.resolveConfiguration(TestElement).alias
).toBe("TestElement");
});
test("root element should be registerable with an alias", () => {
class TestElement {}
const cfg = Condensation.typeRegistry.resolveConfiguration(TestElement);
expect(cfg.alias).toBe("root-element");
});
test("properties should be available on a type", () => {
class TestElement {
name: string | undefined;
}
const cfg = Condensation.typeRegistry.resolveConfiguration(TestElement);
expect(cfg?.properties?.size).toBe(1);
const prop: PropertyDefinition = cfg.properties?.get(
"name"
) as PropertyDefinition;
expect(prop?.realName).toBe("name");
expect(prop?.readAlias).toBe("name");
expect(prop?.writeAlias).toBe("name");
});
test("deserializer should be able to read element from json document with no aliases", () => {
class TestElement {
name: string | undefined;
sayHello(): string {
return `Hello, ${this.name}`;
}
}
const element = Condensation.deserializerFor(TestElement).read({
name: "josiah",
});
expect(element.name).toBe("josiah");
expect(element.sayHello()).toBe("Hello, josiah");
});
test("deserializer should be able to read element from json document constructor arguments", () => {
class Person {
name: string | undefined;
sayHello(): string {
return `Hello, ${this.name}`;
}
}
class TestElement {
person: Person | undefined;
}
const element = Condensation.deserializerFor(TestElement).read({
person: {
name: "josiah",
},
});
expect(element.person).toBeDefined();
expect(element?.person?.name).toBe("josiah");
expect(element?.person?.sayHello()).toBe("Hello, josiah");
});