Skip to content

Instantly share code, notes, and snippets.

@endel
Created April 1, 2018 21:22
Show Gist options
  • Save endel/ae4dee520babea72106313412af1eb3d to your computer and use it in GitHub Desktop.
Save endel/ae4dee520babea72106313412af1eb3d to your computer and use it in GitHub Desktop.
///<reference types="mocha" />
import { assert, expect } from "chai";
import { StateContainer, DataChange } from "../src";
function clone (data: any) {
return JSON.parse(JSON.stringify(data));
}
describe("StateContainer", () => {
let container: StateContainer<any>;
let data: any;
beforeEach(() => {
data = {
npc_entities: {
npc: {
one: { x: 10, y: 0 },
two: { x: 0, y: 0 },
}
}
};
container = new StateContainer<any>(clone(data));
});
it("should trigger container callbacks before its properties", () => {
let container = new StateContainer({});
let npcs: any = {};
container.listen("npc_entities/npc/:id", (change: DataChange) => {
npcs[change.path.id] = change.value;
});
container.listen("npc_entities/npc/:id/:attribute", (change: DataChange) => {
const npcId = change.path.id;
const attribute = change.path.attribute;
assert.ok(npcs[npcId], "npc should already exist");
assert.equal(npcs[npcId][attribute], change.value);
});
container.set(clone(data));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment