You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
6 years ago
|
"use strict";
|
||
|
|
||
|
const expect = require("chai").expect;
|
||
|
|
||
|
const dm = require("../src");
|
||
|
const generateTypeHash = require("../src/type-hash");
|
||
|
|
||
|
function customSerializer(value) {
|
||
|
if (value instanceof Date) {
|
||
|
return {
|
||
|
_d_sT: "date",
|
||
|
date: Math.round(value.getTime() / 1000)
|
||
|
};
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function customDeserializer(value) {
|
||
|
if (value != null && value._d_sT === "date") {
|
||
|
let date = new Date();
|
||
|
date.setTime(value.date);
|
||
|
return date;
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let registry = dm.createRegistry();
|
||
|
|
||
|
let User = registry.createType("User", {
|
||
|
name: dm.string(),
|
||
|
messages: dm.setOf(registry.trait("Addressable"))
|
||
|
});
|
||
|
|
||
|
let Addressable = registry.createTrait("Addressable", {
|
||
|
to: registry.type("User"),
|
||
|
from: registry.type("User"),
|
||
|
});
|
||
|
|
||
|
let Message = registry.createType("Message", {
|
||
|
body: dm.string(),
|
||
|
date: dm.instanceOf(Date)
|
||
|
}).implements(Addressable, {
|
||
|
to: dm.slot(),
|
||
|
from: dm.slot()
|
||
|
});
|
||
|
|
||
|
describe("serialization", () => {
|
||
|
it("should serialize correctly", () => {
|
||
|
let joe = User({
|
||
|
name: "Joe",
|
||
|
messages: new Set()
|
||
|
});
|
||
|
|
||
|
let jane = User({
|
||
|
name: "Jane",
|
||
|
messages: new Set()
|
||
|
});
|
||
|
|
||
|
let messageOne = Message({
|
||
|
to: joe,
|
||
|
from: jane,
|
||
|
body: "Hello world!",
|
||
|
date: new Date()
|
||
|
});
|
||
|
|
||
|
joe.messages.add(messageOne);
|
||
|
|
||
|
let messageTwo = Message({
|
||
|
to: jane,
|
||
|
from: joe,
|
||
|
body: "Hello you!",
|
||
|
date: new Date()
|
||
|
});
|
||
|
|
||
|
jane.messages.add(messageTwo);
|
||
|
|
||
|
let serializedJoe = joe.serialize(customSerializer);
|
||
|
let deserializedJoe = User.deserialize(serializedJoe, customDeserializer);
|
||
|
|
||
|
expect(joe.name).to.equal(deserializedJoe.name);
|
||
|
expect(Array.from(joe.messages)[0].body).to.equal(Array.from(deserializedJoe.messages)[0].body);
|
||
|
expect(Array.from(joe.messages)[0].from.name).to.equal(Array.from(deserializedJoe.messages)[0].from.name);
|
||
|
|
||
|
let serializedMessageTwo = messageTwo.serialize(customSerializer);
|
||
|
let deserializedMessageTwo = Message.deserialize(serializedMessageTwo, customDeserializer);
|
||
|
|
||
|
expect(messageTwo.body).to.equal(deserializedMessageTwo.body);
|
||
|
expect(messageTwo.to.name).to.equal(deserializedMessageTwo.to.name);
|
||
|
expect(messageTwo.from.name).to.equal(deserializedMessageTwo.from.name);
|
||
|
expect(Array.from(messageTwo.from.messages)[0].body).to.equal(Array.from(deserializedMessageTwo.from.messages)[0].body);
|
||
|
});
|
||
|
});
|