relu-core
Version:
62 lines (61 loc) • 1.54 kB
JavaScript
var should = require("should");
var rp = require("../");
var checkRefs = require("./checkRefs");
describe("doubleComputed", function() {
it("should compute in both directions", checkRefs(function() {
var x = rp.variable(6);
var y = x.computed(function(x) {
return x * 2;
}, function(y) {
return y / 2;
});
y().should.be.eql(12);
x().should.be.eql(6);
x.set(4);
y().should.be.eql(8);
x().should.be.eql(4);
y.set(10);
y().should.be.eql(10);
x().should.be.eql(5);
rp.atomic(function() {
x.set(100);
x.set(102);
});
x().should.be.eql(102);
y().should.be.eql(204);
}));
it("should not computed more often than required", checkRefs(function() {
var x = rp.variable(6);
var counter = 0;
var counter2 = 0;
var y = x.computed(function(x) {
counter++;
return x + 1;
}, function(y) {
counter2++;
return y - 1;
});
counter.should.be.eql(1);
counter2.should.be.eql(1);
x.set(10);
counter.should.be.eql(2);
counter2.should.be.eql(2);
y.set(11);
counter.should.be.eql(2);
counter2.should.be.eql(2);
y.set(10);
counter.should.be.eql(3);
counter2.should.be.eql(3);
}));
it("should have helpers", checkRefs(function() {
var x = rp.variable(123), y = rp.variable(123.456);
var xx = x.asIntString();
var yy = y.asFloatString();
xx().should.be.eql("123");
yy().should.be.eql("123.456");
xx.set("789");
yy.set("789.012");
x().should.be.eql(789);
y().should.be.eql(789.012);
}));
});