2
2
*
3
3
*
4
4
* Testing the RailsModel
5
+ * See spec_helper.js for model and colleciton defn's
5
6
*
6
7
*/
7
8
describe ( "RailsModel" , function ( ) {
8
9
var book ,
9
- Book ,
10
- Pages = Backbone . RailsNestedAttributesCollection . extend ( ) ;
11
-
12
- beforeEach ( function ( ) {
13
- Book = Backbone . RailsModel . extend ( {
14
- hasMany : {
15
- pages : {
16
- collection : Pages
17
- }
18
- }
19
- } ) ;
20
-
10
+ Book ;
11
+
12
+ beforeEach ( function ( ) {
13
+ Book = Backbone . RailsModel . extend ( ) ;
21
14
} ) ;
22
15
23
16
// since we override the constructor
24
17
it ( "should behave like a Backbone Model" , function ( ) {
25
18
book = new Book ( ) ;
19
+ expect ( book instanceof Backbone . Model ) . toBeTruthy ( ) ;
26
20
expect ( book . attributes ) . toBeDefined ( ) ;
27
21
} ) ;
28
22
29
23
describe ( "hasMany associations" , function ( ) {
24
+
25
+ beforeEach ( function ( ) {
26
+ Book = Backbone . RailsModel . extend ( hasManyNested ) ;
27
+ } ) ;
30
28
31
- describe ( "empty model" , function ( ) {
29
+ describe ( "Empty model" , function ( ) {
32
30
beforeEach ( function ( ) {
33
31
book = new Book ( ) ;
34
32
} ) ;
35
33
36
- it ( "should have a pages collection" , function ( ) {
37
- expect ( book . pages instanceof Pages ) . toBeTruthy ( ) ;
34
+ it ( "should have collections set" , function ( ) {
35
+ expect ( book . pages instanceof Collections . Pages ) . toBeTruthy ( ) ;
36
+ expect ( book . customerReviews instanceof Collections . CustomerReviews ) . toBeTruthy ( ) ;
38
37
} ) ;
39
38
40
39
describe ( "serialization" , function ( ) {
41
- it ( "should not contain the books collection" , function ( ) {
42
- // testing both nested attrs and not nested cases
43
- expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
40
+ it ( "should not contain the books and reviews collections" , function ( ) {
44
41
expect ( book . toJSON ( ) . pages_attributes ) . toBeUndefined ( ) ;
42
+ expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeUndefined ( ) ;
43
+ } ) ;
44
+
45
+ describe ( "asNestedAttributes: false" , function ( ) {
46
+ beforeEach ( function ( ) {
47
+ Book = Backbone . RailsModel . extend ( hasManyNotNested ) ;
48
+ book = new Book ( ) ;
49
+ } ) ;
50
+
51
+ it ( "should not contain the books and reviews collection" , function ( ) {
52
+ expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
53
+ expect ( book . toJSON ( ) . customer_reviews ) . toBeUndefined ( ) ;
54
+ } ) ;
45
55
} ) ;
46
56
} ) ;
47
57
@@ -50,55 +60,62 @@ describe("RailsModel", function() {
50
60
describe ( "Populated model" , function ( ) {
51
61
beforeEach ( function ( ) {
52
62
book = new Book ( {
53
- pages : [ { one :1 } , { two :2 } ]
63
+ pages : [ { one :1 } , { two :2 } ] ,
64
+ customer_reviews : [ { one :'great' } , { two :'mediocre' } , { three :'soso' } ]
54
65
} ) ;
55
66
} ) ;
56
67
57
- it ( "should have a populated collection" , function ( ) {
68
+ it ( "should have a populated each collection" , function ( ) {
58
69
expect ( book . pages . length ) . toEqual ( 2 ) ;
59
70
expect ( book . pages . at ( 0 ) . get ( 'one' ) ) . toEqual ( 1 ) ;
60
71
expect ( book . pages . at ( 1 ) . get ( 'two' ) ) . toEqual ( 2 ) ;
72
+
73
+ expect ( book . customerReviews . length ) . toEqual ( 3 ) ;
74
+ expect ( book . customerReviews . at ( 0 ) . get ( 'one' ) ) . toEqual ( 'great' ) ;
75
+ expect ( book . customerReviews . at ( 1 ) . get ( 'two' ) ) . toEqual ( 'mediocre' ) ;
76
+ expect ( book . customerReviews . at ( 2 ) . get ( 'three' ) ) . toEqual ( 'soso' ) ;
61
77
} ) ;
62
78
63
79
it ( "should not have the collection set as attributes" , function ( ) {
64
80
expect ( book . get ( 'pages' ) ) . toBeUndefined ( ) ;
81
+ expect ( book . get ( 'customer_reviews' ) ) . toBeUndefined ( ) ;
65
82
} ) ;
66
83
67
84
describe ( "serialization" , function ( ) {
68
- describe ( "using asNestedAttributes" , function ( ) {
69
- it ( "should have pages_attributes" , function ( ) {
70
- expect ( book . toJSON ( ) . pages_attributes ) . toBeDefined ( ) ;
71
- expect ( book . toJSON ( ) . pages_attributes [ 0 ] . one ) . toEqual ( 1 ) ;
72
- } ) ;
85
+ it ( "should have both collection's attributes" , function ( ) {
86
+ expect ( book . toJSON ( ) . pages_attributes ) . toBeDefined ( ) ;
87
+ expect ( book . toJSON ( ) . pages_attributes [ 0 ] . one ) . toEqual ( 1 ) ;
88
+
89
+ expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeDefined ( ) ;
90
+ expect ( book . toJSON ( ) . customer_reviews_attributes [ 0 ] . one ) . toEqual ( 'great' ) ;
91
+ } ) ;
73
92
74
- it ( "should not have pages" , function ( ) {
75
- expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
76
- } ) ;
93
+ it ( "should not have pages or customer_reviews " , function ( ) {
94
+ expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
95
+ expect ( book . toJSON ( ) . customer_reviews ) . toBeUndefined ( ) ;
77
96
} ) ;
78
97
79
- describe ( "not using asNestedAttributes" , function ( ) {
98
+ describe ( "asNestedAttributes: false " , function ( ) {
80
99
beforeEach ( function ( ) {
81
- Book = Backbone . RailsModel . extend ( {
82
- hasMany : {
83
- pages : {
84
- collection : Pages ,
85
- asNestedAttributes : false
86
- }
87
- }
88
- } ) ;
100
+ Book = Backbone . RailsModel . extend ( hasManyNotNested ) ;
89
101
90
102
book = new Book ( {
91
- pages : [ { one :1 } , { two :2 } ]
103
+ pages : [ { one :1 } , { two :2 } ] ,
104
+ customer_reviews : [ { one :'great' } , { two :'mediocre' } , { three :'soso' } ]
92
105
} ) ;
93
106
} ) ;
94
107
95
108
it ( "should have pages" , function ( ) {
96
109
expect ( book . toJSON ( ) . pages ) . toBeDefined ( ) ;
97
110
expect ( book . toJSON ( ) . pages [ 0 ] . one ) . toEqual ( 1 ) ;
111
+
112
+ expect ( book . toJSON ( ) . customer_reviews ) . toBeDefined ( ) ;
113
+ expect ( book . toJSON ( ) . customer_reviews [ 0 ] . one ) . toEqual ( 'great' ) ;
98
114
} ) ;
99
115
100
116
it ( "should not have pages_attributes" , function ( ) {
101
117
expect ( book . toJSON ( ) . pages_attributes ) . toBeUndefined ( ) ;
118
+ expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeUndefined ( ) ;
102
119
} ) ;
103
120
} ) ;
104
121
} ) ;
0 commit comments