relu-core
Version:
133 lines (129 loc) • 3.22 kB
JavaScript
var should = require("should");
var rp = require("../");
var checkRefs = require("./checkRefs");
describe("element", function() {
it("should operate with elements of an array", checkRefs(function() {
var x = rp.variable(["one", "two", "three", "four", "five"]);
var three = x(2);
var four = x(3);
var five = x(4);
x.isWritable().should.be.eql(true);
four.isWritable().should.be.eql(true);
three().should.be.eql("three");
four().should.be.eql("four");
five().should.be.eql("five");
x.unshift("zero");
var zero = x(0);
zero().should.be.eql("zero");
three().should.be.eql("three");
four().should.be.eql("four");
five().should.be.eql("five");
zero.should.be.equal(x(0));
x.splice(x.indexOf(four)(), 1);
three().should.be.eql("three");
should.not.exist(four());
five().should.be.eql("five");
x.shift();
three().should.be.eql("three");
five().should.be.eql("five");
}));
it("should operation on nested objects", checkRefs(function() {
var x = rp.variable([
{a: 1},
[1, 2, 3],
{b: 2}
]);
var json = x.stringified();
var a = x(0, "a");
var b = x(2);
var c = x(1, 0);
x.isWritable().should.be.eql(true);
a.isWritable().should.be.eql(true);
b.isWritable().should.be.eql(true);
c.isWritable().should.be.eql(true);
x.unshift(1);
a.should.be.equal(x(1, "a"));
b.should.be.equal(x(3));
c.should.be.equal(x(2, 0));
json().should.be.eql(JSON.stringify([
1,
{a: 1},
[1, 2, 3],
{b: 2}
]));
a.set(123);
json().should.be.eql(JSON.stringify([
1,
{a: 123},
[1, 2, 3],
{b: 2}
]));
b.set(123);
json().should.be.eql(JSON.stringify([
1,
{a: 123},
[1, 2, 3],
123
]));
c.set(123);
json().should.be.eql(JSON.stringify([
1,
{a: 123},
[123, 2, 3],
123
]));
x.shift();
x.shift();
json().should.be.eql(JSON.stringify([
[123, 2, 3],
123
]));
should.not.exist(a());
x.shift();
x.shift();
json().should.be.eql("[]");
should.not.exist(b());
should.not.exist(c());
}));
it("should fire correct events", checkRefs(function() {
var x = rp.variable([[1,2,3]])(0);
var events = [];
x.onChanged(function() {
events.push("changed");
});
x.onUpdated(function(n, o) {
events.push(["updated", n, o]);
});
x.onAdded(function(idx, i) {
events.push(["added", idx, i]);
});
x.onRemoved(function(idx, i) {
events.push(["removed", idx, i]);
});
x.push(4);
events.shift().should.be.eql(["added", 3, 4]);
events.shift().should.be.eql("changed");
x.pop();
events.shift().should.be.eql(["removed", 3, 4]);
events.shift().should.be.eql("changed");
rp.atomic(function() {
x.set([0, 1, 2]);
x.shift();
x.push(4);
});
events.shift().should.be.eql(["updated", [1, 2, 4], [1, 2, 3]]);
events.shift().should.be.eql("changed");
}));
it("should remove atomically multiple elements", checkRefs(function() {
var x = rp.variable([0,1,2,3,4]);
var a = x(1);
var b = x(2);
var c = x(3);
rp.atomic(function() {
b.remove();
a.remove();
c.remove();
});
x().should.be.eql([0,4]);
}));
});