48e24fe3f565733cc826f5c5ecbd79d44907aefa
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 describe("app.data.Model", function() {
17
18         var Model = window.app.data.Model;
19
20         it("test setting model does a shallow copy", function() {
21                 var test = {};
22                 var array = [ 1, 2, 3 ];
23                 var m = new Model({
24                         data: {
25                                 "foo": "bar",
26                                 "test": test,
27                                 "array": array
28                         }
29                 });
30                 expect( m.get("foo") ).toBe("bar");
31                 expect( m.get("array").length ).toBe(  3  );
32                 expect( m.get("array")[1] ).toBe( 2 );
33                 expect( m.get("array") ).toBe( array );
34                 expect( m.get("test") ).toBe( test );
35         });
36
37         it("should replace model with shallow copy when put with no path", function() {
38                 var m = new Model({ foo: "bar" });
39                 m.set({ bar: "blat" });
40                 expect( m.get("foo")).toBe( undefined );
41                 expect( m.get("bar")).toBe("blat");
42         });
43
44         it("test getting values from deep in a model", function() {
45                 var m = new Model({
46                         data: {
47                                 "foo": {
48                                         "bar": {
49                                                 "baz": {
50                                                         "quix": "abcdefg"
51                                                 }
52                                         }
53                                 }
54                         }
55                 });
56
57                 expect( m.get("foo.bar.baz.quix") ).toBe( "abcdefg" );
58         });
59
60         it("test setting non-existant values creates new values", function() {
61                 var m = new Model({
62                         data: {
63                                 "foo": {
64                                         "bar": "abc"
65                                 }
66                         }
67                 });
68                 m.set("foo.bar", "123" );
69                 m.set("foo.baz", "456" );
70                 expect( m.get("foo.bar") ).toBe( "123" );
71                 expect( m.get("foo.baz") ).toBe( "456" );
72         });
73
74         it("test setting values deep in a model", function() {
75                 var m = new Model({
76                         data: {
77                                 "foo": {
78                                         "bar": "abc"
79                                 }
80                         }
81                 });
82                 m.set("foo.bar", "123" );
83                 m.set("foo.baz", "456" );
84                 m.set("foo.something.else.is.here", "xyz" );
85                 expect( m.get("foo.something.else.is").here ).toBe( "xyz" );
86                 expect( m.get("foo.something.else.is.here") ).toBe( "xyz" );
87         });
88
89 });