@aire-ux/aire-condensation
Version:
Client-side serialization library for Aire-UX
461 lines (376 loc) • 8.68 kB
text/typescript
import {Receive, Remotable, Remote} from "@condensation/remotable";
import {Property, RootElement} from "@condensation/root-element";
import {Condensation} from "@condensation/condensation";
import {Dynamic} from "@condensation/types";
import {customElement, LitElement} from "lit-element";
test("remotable should work with constructor arguments", () => {
class TestDTO {
}
class TestReceiver {
constructor( dto: TestDTO) {
}
}
const defs = Condensation.remoteRegistry.resolve(TestReceiver).definitions;
expect(defs.length).toBe(1);
expect(defs[0].index).toBe(0);
});
test('remotable should work with subclasses', () => {
class TestDTO {
}
class Parent {
id: string | undefined;
}
class TestReceiver extends Parent {
constructor( dto: TestDTO) {
super();
}
}
const defs = Condensation.remoteRegistry.resolve(TestReceiver).definitions;
expect(defs.length).toBe(1);
expect(defs[0].index).toBe(0);
});
test("remotable should allow a value to be constructed", () => {
class Person {
name: string | undefined;
}
class Pet {
name: string | undefined;
momma: Person | undefined;
sayHenlo(): string {
return "Mommymommymommy!";
}
}
class TestReceiver extends LitElement {
name: string | undefined;
constructor(
public readonly pet: Pet,
public readonly dto: Person
) {
super();
this.name = dto.name;
}
set( dto: Person): void {
console.log(dto);
}
}
const ctx = Condensation.newContext();
const receiver = ctx.create<TestReceiver>(
TestReceiver,
`
{
"name": "Flances",
"momma": {
"name": "Wab"
}
}
`,
`{
"name": "Josiah"
}`
);
expect(receiver.name).toBe("Josiah");
expect(receiver.dto.name).toBe("Josiah");
expect(receiver.pet.sayHenlo()).toBe("Mommymommymommy!");
expect(receiver.pet?.momma?.name).toBe("Wab");
});
test("ensure base use-case works", () => {
class GraphConfiguration {
loadResources: string | undefined;
forceIncludes: string | undefined;
loadStylesheets: boolean | undefined;
resourceExtension: boolean | undefined;
productionMode: boolean | undefined;
basePath: string | undefined;
}
class MxGraphManager {
constructor(
readonly configuration: GraphConfiguration
) {
}
}
const mgr = Condensation.newContext().create<MxGraphManager>(
MxGraphManager,
`{
"load-resources": "loading them resources"
}`
);
expect(mgr.configuration?.loadResources).toEqual("loading them resources");
});
test("ensure array is deserializable", () => {
class Person {
name: string | undefined;
}
class Group {
members: Person[] | undefined;
}
const group = Condensation.deserializerFor<Group>(Group).read(
{
members: [
{
name: "Josiah",
},
{
name: "Lisa",
},
{
name: "Alejandro",
},
{
name: "Tiff",
},
]
}
);
expect(group.members?.length).toBe(4);
expect(group.members?.map((m) => m.name)).toEqual([
"Josiah",
"Lisa",
"Alejandro",
"Tiff",
]);
});
test("pointers should be invocable", () => {
class Person {
name: string | undefined;
}
class MxGraphManager {
person: Person | undefined;
public init( person: Person): void {
this.person = person;
}
}
const ctx = Condensation.newContext(),
mgr = ctx.create<MxGraphManager>(MxGraphManager);
ctx.invoke(
mgr,
"init",
`
{
"name": "Josiah"
}
`
);
expect(mgr.person?.name).toBe("Josiah");
});
test("values should be invocable", () => {
class Person {
name: string | undefined;
}
class MxGraphManager {
person: Person | undefined;
public init( person: Person): void {
this.person = person;
}
}
const mgr = new MxGraphManager();
mgr.init(`{
"name": "Josiah"
}
` as any);
expect(mgr?.person?.name).toBe("Josiah");
});
test('canvas scenario should work', () => {
class Vertex {
private x: number | undefined;
private y: number | undefined;
private width: number | undefined;
private height: number | undefined;
label: string | undefined;
}
class Canvas extends LitElement {
readonly vertices: Vertex[];
constructor() {
super();
this.vertices = [];
}
public addVertex( vertex: Vertex) {
this.vertices.push(vertex);
// this.graph?.addNode(vertex as any);
}
}
const canvas = new Canvas();
expect(canvas.vertices).toBeTruthy();
// @ts-ignore
canvas.addVertex(`
{
"x": null,
"y": null,
"label": "hello",
"width": null,
"height": null
}
`);
expect(canvas.vertices.length).toBe(1);
const vertex = canvas.vertices[0];
expect(vertex.label).toBe("hello");
});
test('parsing json to a type should work', () => {
type Whatever = {
hello: string
};
class Test {
readonly whatevers: Whatever[] = [];
add( whatever: Whatever): void {
this.whatevers.push(whatever)
}
}
let a = new Test();
// @ts-ignore
a.add(`
{"hello": "world"}
`);
expect(a.whatevers.length).toBe(1);
expect(a.whatevers[0].hello).toBe("world");
});
test('parsing json to a list type should work', () => {
type Whatever = {
hello: string
};
class Test {
readonly whatevers: Whatever[] = [];
add( whatever: Whatever[]): void {
this.whatevers.push(...whatever)
}
}
let a = new Test();
// @ts-ignore
a.add(`[
{"hello": "world"},
{"hello": "jorld"}
]
`);
expect(a.whatevers.length).toBe(2);
expect(a.whatevers[0].hello).toBe("world");
expect(a.whatevers[1].hello).toBe("jorld");
});
test('add all should work', () => {
class Vertex {
private x: number | undefined;
private y: number | undefined;
private width: number | undefined;
private height: number | undefined;
label: string | undefined;
}
class Canvas extends LitElement {
readonly vertices: Vertex[];
constructor() {
super();
this.vertices = [];
}
public addVertex( vertex: Vertex) {
this.vertices.push(vertex);
// this.graph?.addNode(vertex as any);
}
public addVertices( vertex: Vertex[]) {
this.vertices.push(...vertex);
// this.graph?.addNode(vertex as any);
}
}
const canvas = new Canvas();
expect(canvas.vertices).toBeTruthy();
// @ts-ignore
canvas.addVertices(`
[{
"x": null,
"y": null,
"label": "hello",
"width": null,
"height": null
},
{
"x": null,
"y": null,
"label": "jello",
"width": null,
"height": null
}]
`);
expect(canvas.vertices.length).toBe(2);
let vertex = canvas.vertices[0];
expect(vertex.label).toBe("hello");
vertex = canvas.vertices[1];
expect(vertex.label).toBe("jello");
});