UNPKG

9.35 kBJavaScriptView Raw
1var observeReader = require("can-stache-key");
2var QUnit = require('steal-qunit');
3var Observation = require('can-observation');
4var eventQueue = require('can-event-queue/map/map');
5var SimpleObservable = require("can-simple-observable");
6var testHelpers = require('can-test-helpers');
7var ObservationRecorder = require("can-observation-recorder");
8
9var SimpleMap = require("can-simple-map");
10var canReflect = require("can-reflect");
11
12QUnit.module('can-stache-key',{
13
14});
15
16QUnit.test("can read a promise (#179)", function(assert) {
17 var done = assert.async();
18 var data = {
19 promise: new Promise(function(resolve){
20 setTimeout(function(){
21 resolve("Something");
22 },2);
23 })
24 };
25 var calls = 0;
26 var c = new Observation(function(){
27 return observeReader.read(data,observeReader.reads("promise.value")).value;
28 });
29 canReflect.onValue(c, function(newVal, oldVal){
30 calls++;
31 assert.equal(calls, 1, "only one call");
32 assert.equal(newVal, "Something", "new value");
33 assert.equal(oldVal, undefined, "oldVal");
34 done();
35 });
36
37
38
39});
40
41QUnit.test("can.Compute.read can read a promise-like (#82)", function(assert) {
42 var done = assert.async();
43 var data = {
44 promiseLike: {
45 then: function(resolve) {
46 setTimeout(function(){
47 resolve("Something");
48 }, 2);
49 }
50 }
51 };
52 var calls = 0;
53 var c = new Observation(function(){
54 return observeReader.read(data,observeReader.reads("promiseLike.value")).value;
55 });
56 canReflect.onValue(c, function(newVal, oldVal){
57 calls++;
58 assert.equal(calls, 1, "only one call");
59 assert.equal(newVal, "Something", "new value");
60 assert.equal(oldVal, undefined, "oldVal");
61 done();
62 });
63
64
65});
66
67QUnit.test('can.compute.reads', function(assert) {
68 assert.deepEqual( observeReader.reads("@foo"),
69 [{key: "foo", at: true}]);
70
71 assert.deepEqual( observeReader.reads("@foo.bar"),
72 [{key: "foo", at: true}, {key: "bar", at: false}]);
73
74 assert.deepEqual( observeReader.reads("@foo\\.bar"),
75 [{key: "foo.bar", at: true}]);
76
77 assert.deepEqual( observeReader.reads("foo.bar@zed"),
78 [{key: "foo", at: false},{key: "bar", at: false},{key: "zed", at: true}]);
79
80});
81
82QUnit.test('able to read things like can-define', function(assert) {
83 assert.expect(3);
84 var obj = eventQueue({});
85 var prop = "PROP";
86 Object.defineProperty(obj, "prop",{
87 get: function(){
88 ObservationRecorder.add(obj,"prop");
89 return prop;
90 },
91 set: function(val){
92 var old = prop;
93 prop = val;
94 this.dispatch("prop", prop, old);
95 }
96 });
97 var data = {
98 obj: obj
99 };
100
101 var c = new Observation(function(){
102 var value = observeReader.read(data,observeReader.reads("obj.prop"),{
103 foundObservable: function(obs, index){
104 assert.equal(obs, obj, "got an observable");
105 assert.equal(index,1, "got the right index");
106 }
107 }).value;
108 assert.equal(value, "PROP");
109 });
110 canReflect.onValue(c, function(){});
111});
112
113QUnit.test("foundObservable called with observable object (#7)", function(assert) {
114 var map = new SimpleMap({
115 isSaving: function(){
116 ObservationRecorder.add(this, "_saving");
117 },
118 addEventListener: function(){}
119 });
120
121 // must use an observation to make sure things are listening.
122 var c = new Observation(function(){
123 observeReader.read(map,observeReader.reads("isSaving"),{
124 foundObservable: function(obs){
125 assert.equal(obs, map);
126 },
127 callMethodsOnObservables: true
128 });
129 });
130 canReflect.onValue(c, function(){});
131});
132
133QUnit.test("can read from strings", function(assert) {
134 var context = " hi there ";
135 var result = observeReader.read(context,observeReader.reads("trim"),{});
136 assert.equal(
137 result.value(context),
138 context.trim(context),
139 'trim method works'
140 );
141});
142
143QUnit.test("read / write to SimpleMap", function(assert) {
144 var map = new SimpleMap();
145 var c = new Observation(function(){
146 var data = observeReader.read(map,observeReader.reads("value"),{
147 foundObservable: function(obs){
148 assert.equal(obs, map, "got map");
149 }
150 });
151 return data.value;
152 });
153 canReflect.onValue(c, function(newVal){
154 assert.equal(newVal, 1, "got updated");
155 });
156 observeReader.write(map,"value",1);
157});
158
159QUnit.test("write deep in SimpleMap", function(assert) {
160 var map = new SimpleMap();
161 observeReader.write(map,"foo", new SimpleMap());
162 observeReader.write(map,"foo.bar", 1);
163
164 assert.equal(map.get("foo").get("bar"), 1, "value set");
165});
166
167QUnit.test("write to compute in object", function(assert) {
168 var value = 2;
169 var computeObject = {};
170 canReflect.assignSymbols(computeObject, {
171 "can.getValue": function(){
172 return value;
173 },
174 "can.setValue": function(newVal){
175 value = newVal;
176 }
177 });
178
179 var obj = {compute: computeObject};
180
181 observeReader.write(obj,"compute", 3);
182
183 assert.equal(value, 3, "value set");
184});
185
186QUnit.test("write to a map in a compute", function(assert) {
187
188 var map = new SimpleMap({complete: true});
189 var computeObject = {};
190
191 canReflect.assignSymbols(computeObject, {
192 "can.getValue": function(){
193 return map;
194 },
195 "can.setValue": function(newVal){
196 map = newVal;
197 }
198 });
199
200 observeReader.write(computeObject, "complete", false);
201
202 assert.equal(map.attr("complete"), false, "value set");
203});
204
205QUnit.test("reads can be passed a number (can-stache#207)", function(assert) {
206 var reads = observeReader.reads(0);
207 assert.deepEqual(reads, [{key: "0", at: false}], "number converted to string");
208
209});
210
211QUnit.test("can read primitive numbers (#88)", function(assert) {
212 var reads = observeReader.reads("num@toFixed");
213 var toFixed = observeReader.read({
214 num: 5
215 }, reads, {}).value;
216
217 assert.equal(typeof toFixed, "function", "got to fixed");
218
219});
220
221QUnit.test("it returns null when promise getter is null #2", function(assert) {
222 var nullPromise = observeReader.read(null, observeReader.reads('value'));
223 assert.equal(typeof nullPromise,"object");
224});
225
226QUnit.test("set onto observable objects and values", function(assert) {
227 var map = new SimpleMap();
228 observeReader.write({map: map},"map", {a: "b"});
229
230 assert.equal(map.get("a"), "b", "merged");
231
232 var simple = new SimpleObservable();
233 observeReader.write({simple: simple},"simple", 1);
234 assert.equal(simple.get(), 1);
235});
236
237testHelpers.dev.devOnlyTest("functions are not called by read()", function (assert) {
238 var func = function() {
239 assert.ok(false, "method called");
240 };
241 var data = { func: func };
242 var reads = observeReader.reads("func");
243
244 observeReader.read(data, reads);
245
246 assert.ok(true);
247});
248
249testHelpers.dev.devOnlyTest("a warning is given for `callMethodsOnObservables: true`", function (assert) {
250 var teardown = testHelpers.dev.willWarn("can-stache-key: read() called with `callMethodsOnObservables: true`.");
251 var func = function() {
252 assert.ok(true, "method called");
253 };
254 var data = new SimpleMap({ func: func });
255 var reads = observeReader.reads("func");
256
257 observeReader.read(data, reads, {
258 callMethodsOnObservables: true
259 });
260
261 assert.equal(teardown(), 1, "warning displayed");
262});
263
264QUnit.test("writing to a null observable is ignored", function(assert) {
265 observeReader.write({},"foo.bar", "value");
266 observeReader.write(null,"bar", "value");
267 observeReader.write(null,"foo.bar", "value");
268 assert.ok(true, "all passed without error");
269});
270
271QUnit.test("parentHasKey and foundLastParent (#31)", function(assert) {
272 var hasKeys = function(obj, keys) {
273 canReflect.assignSymbols(obj, {
274 "can.hasKey": function(key) {
275 return keys.indexOf(key) > -1;
276 }
277 });
278 };
279
280 var def = { ghi: undefined };
281 hasKeys(def, [ "ghi" ]);
282
283 var abc = { def: def };
284 hasKeys(abc, [ "def" ]);
285
286 var parent = { abc: abc };
287 hasKeys(parent, [ "abc" ]);
288
289 var testCases = {
290 "abc.def.ghi": { parent: def, value: undefined, parentHasKey: true, foundLastParent: true },
291 "abc.def.jkl": { parent: def, value: undefined, parentHasKey: false, foundLastParent: true },
292 "abc.ghi.ghi": { parent: abc, value: undefined, parentHasKey: false, foundLastParent: false },
293 "def.ghi.jkl": { parent: parent, value: undefined, parentHasKey: false, foundLastParent: false }
294 };
295
296 var reads, actual, expected;
297 for (var key in testCases) {
298 reads = observeReader.reads(key);
299 actual = observeReader.read(parent, reads);
300 expected = testCases[key];
301
302 assert.equal(actual.value, expected.value, key + ".value");
303 assert.equal(actual.parent, expected.parent, key + ".parent");
304 assert.equal(actual.parentHasKey, expected.parentHasKey, key + ".parentHasKey");
305 assert.equal(actual.foundLastParent, expected.foundLastParent, key + ".foundLastParent");
306 }
307});
308
309QUnit.test("objHasKeyAtIndex doesn't handle non-object types correctly (#33)", function(assert) {
310 var result = observeReader.read(47, observeReader.reads("toFixed"));
311 assert.equal(typeof result.value, 'function');
312 assert.equal(result.parent, 47);
313 assert.equal(result.parentHasKey, true);
314});
315
316QUnit.test("write to an object", function(assert) {
317 var obj = {};
318 observeReader.write(obj,"value",1);
319 assert.deepEqual(obj,{value: 1});
320 obj = {value: null};
321 observeReader.write(obj,"value",1);
322 assert.deepEqual(obj,{value: 1});
323});
324
325QUnit.test(".then won't call bindings #49", function(assert){
326 var promiseIsh = {};
327 Object.defineProperty(promiseIsh,"then",{
328 get: function(){
329 ObservationRecorder.add(this, "then");
330 }
331 });
332 ObservationRecorder.start();
333 observeReader.read(promiseIsh, observeReader.reads("prop"));
334 var recordings = ObservationRecorder.stop();
335
336 assert.equal( recordings.keyDependencies.size, 0, "no key recordings");
337});