UNPKG

2.08 kBJavaScriptView Raw
1describe("#autorun() & #depend()", function() {
2 this.timeout(500);
3 var tpl, comp;
4
5 before(function() {
6 tpl = new Temple();
7 });
8
9 beforeEach(function() {
10 tpl.set("foo", "bar");
11 });
12
13 afterEach(function() {
14 if (comp != null) {
15 comp.stop();
16 comp = null;
17 }
18 });
19
20 it("autorun() context always runs once, immediately", function() {
21 var seen = false;
22 comp = tpl.autorun(function() {
23 expect(tpl.get("foo")).to.equal("bar");
24 seen = true;
25 });
26 expect(seen).to.be.ok;
27 });
28
29 it("`this` in autorun() contexts points to Temple instance", function() {
30 comp = tpl.autorun(function() {
31 expect(tpl.get("foo")).to.equal("bar");
32 expect(this).to.equal(tpl);
33 });
34 });
35
36 it("changing value at `key` after calling get(key) in a context causes context to run again", function(done) {
37 var run = 2;
38
39 comp = tpl.autorun(function() {
40 try { expect(tpl.get("foo")).to.be.ok; }
41 catch(e) { return done(e); }
42 if (!(--run)) done();
43 });
44
45 setTimeout(function() {
46 tpl.set("foo", { bar: "baz" });
47 }, 10);
48 });
49
50 it("autorun() context reruns for fallback changes", function(done) {
51 var fb = new Temple.Model({ baz: "buz" }),
52 run = 2;
53
54 function donedone(e) {
55 tpl.removeModel(fb);
56 done(e);
57 }
58
59 tpl.addModel(fb);
60
61 comp = tpl.autorun(function() {
62 try { expect(tpl.get("baz")).to.be.ok; }
63 catch(e) { return donedone(e); }
64 if (!(--run)) donedone();
65 });
66
67 setTimeout(function() {
68 fb.set("baz", { bar: "baz" });
69 }, 10);
70 });
71
72 it("autorun() context reruns for changes to value when previous get() returned a fallback scope's value", function(done) {
73 var fb = new Temple.Model({ baz: "buz" }),
74 run = 2;
75
76 function donedone(e) {
77 tpl.removeModel(fb);
78 done(e);
79 }
80
81 tpl.addModel(fb);
82
83 comp = tpl.autorun(function() {
84 try {
85 if (run == 2) expect(tpl.get("baz")).to.equal("buz");
86 if (run == 1) expect(tpl.get("baz")).to.deep.equal({ bar: "baz" });
87 }
88 catch(e) { return donedone(e); }
89 if (!(--run)) donedone();
90 });
91
92 setTimeout(function() {
93 tpl.set("baz", { bar: "baz" });
94 }, 10);
95 });
96});
\No newline at end of file