@@ -20,10 +20,10 @@ describe("RailsModel", function() {
20
20
expect ( book . attributes ) . toBeDefined ( ) ;
21
21
} ) ;
22
22
23
- describe ( "hasMany associations " , function ( ) {
23
+ describe ( "Associations Using Nested Attributes " , function ( ) {
24
24
25
25
beforeEach ( function ( ) {
26
- Book = Backbone . RailsModel . extend ( hasManyNested ) ;
26
+ Book = Backbone . RailsModel . extend ( nestedAssociations ) ;
27
27
} ) ;
28
28
29
29
describe ( "Empty model" , function ( ) {
@@ -35,34 +35,29 @@ describe("RailsModel", function() {
35
35
expect ( book . pages instanceof Collections . Pages ) . toBeTruthy ( ) ;
36
36
expect ( book . customerReviews instanceof Collections . CustomerReviews ) . toBeTruthy ( ) ;
37
37
} ) ;
38
+
39
+ it ( "should have models set" , function ( ) {
40
+ expect ( book . author instanceof Models . Author ) . toBeTruthy ( ) ;
41
+ expect ( book . publishingHouse instanceof Models . PublishingHouse ) . toBeTruthy ( ) ;
42
+ } ) ;
38
43
39
44
describe ( "serialization" , function ( ) {
40
- it ( "should not contain the books and reviews collections" , function ( ) {
45
+ it ( "should not contain the collections" , function ( ) {
41
46
expect ( book . toJSON ( ) . pages_attributes ) . toBeUndefined ( ) ;
42
47
expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeUndefined ( ) ;
43
48
} ) ;
44
49
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
- } ) ;
50
+ it ( "should not contain the models" , function ( ) {
51
+ expect ( book . toJSON ( ) . author_attributes ) . toBeUndefined ( ) ;
52
+ expect ( book . toJSON ( ) . publishing_house_attributes ) . toBeUndefined ( ) ;
55
53
} ) ;
56
54
} ) ;
57
55
58
56
} ) ;
59
57
60
58
describe ( "Populated model" , function ( ) {
61
59
beforeEach ( function ( ) {
62
- book = new Book ( {
63
- pages : [ { one :1 } , { two :2 } ] ,
64
- customer_reviews : [ { one :'great' } , { two :'mediocre' } , { three :'soso' } ]
65
- } ) ;
60
+ book = new Book ( sampleBook ) ;
66
61
} ) ;
67
62
68
63
it ( "should have a populated each collection" , function ( ) {
@@ -75,11 +70,21 @@ describe("RailsModel", function() {
75
70
expect ( book . customerReviews . at ( 1 ) . get ( 'two' ) ) . toEqual ( 'mediocre' ) ;
76
71
expect ( book . customerReviews . at ( 2 ) . get ( 'three' ) ) . toEqual ( 'soso' ) ;
77
72
} ) ;
73
+
74
+ it ( "should have populated each model" , function ( ) {
75
+ expect ( book . author . get ( 'name' ) ) . toEqual ( 'John Doe' ) ;
76
+ expect ( book . publishingHouse . get ( 'name' ) ) . toEqual ( 'Books co.' ) ;
77
+ } ) ;
78
78
79
- it ( "should not have the collection set as attributes" , function ( ) {
79
+ it ( "should not have the passed in collection names set as attributes" , function ( ) {
80
80
expect ( book . get ( 'pages' ) ) . toBeUndefined ( ) ;
81
81
expect ( book . get ( 'customer_reviews' ) ) . toBeUndefined ( ) ;
82
82
} ) ;
83
+
84
+ it ( "should not have the passed in model names set as attributes" , function ( ) {
85
+ expect ( book . get ( 'author' ) ) . toBeUndefined ( ) ;
86
+ expect ( book . get ( 'publishing_house' ) ) . toBeUndefined ( ) ;
87
+ } ) ;
83
88
84
89
describe ( "serialization" , function ( ) {
85
90
it ( "should have both collection's attributes" , function ( ) {
@@ -89,34 +94,81 @@ describe("RailsModel", function() {
89
94
expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeDefined ( ) ;
90
95
expect ( book . toJSON ( ) . customer_reviews_attributes [ 0 ] . one ) . toEqual ( 'great' ) ;
91
96
} ) ;
97
+
98
+ it ( "should have model attributes set" , function ( ) {
99
+ expect ( book . toJSON ( ) . author_attributes ) . toBeDefined ( ) ;
100
+ expect ( book . toJSON ( ) . author_attributes . name ) . toEqual ( 'John Doe' ) ;
101
+
102
+ expect ( book . toJSON ( ) . publishing_house_attributes ) . toBeDefined ( ) ;
103
+ expect ( book . toJSON ( ) . publishing_house_attributes . name ) . toEqual ( 'Books co.' ) ;
104
+ } ) ;
92
105
93
106
it ( "should not have pages or customer_reviews" , function ( ) {
94
107
expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
95
108
expect ( book . toJSON ( ) . customer_reviews ) . toBeUndefined ( ) ;
96
109
} ) ;
110
+
111
+ it ( "should not have author or publishing_house" , function ( ) {
112
+ expect ( book . toJSON ( ) . author ) . toBeUndefined ( ) ;
113
+ expect ( book . toJSON ( ) . publishing_house ) . toBeUndefined ( ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
117
+ } ) ;
118
+
119
+ describe ( "Associations Without Using Nested Attributes" , function ( ) {
120
+
121
+ beforeEach ( function ( ) {
122
+ Book = Backbone . RailsModel . extend ( nonNestedAssociations ) ;
123
+ } ) ;
97
124
98
- describe ( "asNestedAttributes: false" , function ( ) {
99
- beforeEach ( function ( ) {
100
- Book = Backbone . RailsModel . extend ( hasManyNotNested ) ;
101
-
102
- book = new Book ( {
103
- pages : [ { one :1 } , { two :2 } ] ,
104
- customer_reviews : [ { one :'great' } , { two :'mediocre' } , { three :'soso' } ]
105
- } ) ;
106
- } ) ;
107
-
108
- it ( "should have pages" , function ( ) {
109
- expect ( book . toJSON ( ) . pages ) . toBeDefined ( ) ;
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' ) ;
114
- } ) ;
115
-
116
- it ( "should not have pages_attributes" , function ( ) {
117
- expect ( book . toJSON ( ) . pages_attributes ) . toBeUndefined ( ) ;
118
- expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeUndefined ( ) ;
119
- } ) ;
125
+ describe ( "serialization" , function ( ) {
126
+
127
+ describe ( "empty model" , function ( ) {
128
+ beforeEach ( function ( ) {
129
+ book = new Book ( ) ;
130
+ } ) ;
131
+
132
+ it ( "should not include collections" , function ( ) {
133
+ expect ( book . toJSON ( ) . pages ) . toBeUndefined ( ) ;
134
+ expect ( book . toJSON ( ) . customer_reviews ) . toBeUndefined ( ) ;
135
+ } ) ;
136
+
137
+ it ( "should not include models" , function ( ) {
138
+ expect ( book . toJSON ( ) . author ) . toBeUndefined ( ) ;
139
+ expect ( book . toJSON ( ) . publishing_house ) . toBeUndefined ( ) ;
140
+ } ) ;
141
+ } ) ;
142
+
143
+ describe ( "populated model" , function ( ) {
144
+ beforeEach ( function ( ) {
145
+ book = new Book ( sampleBook ) ;
146
+ } ) ;
147
+
148
+ it ( "should include collections" , function ( ) {
149
+ expect ( book . toJSON ( ) . pages ) . toBeDefined ( ) ;
150
+ expect ( book . toJSON ( ) . pages [ 0 ] . one ) . toEqual ( 1 ) ;
151
+
152
+ expect ( book . toJSON ( ) . customer_reviews ) . toBeDefined ( ) ;
153
+ expect ( book . toJSON ( ) . customer_reviews [ 0 ] . one ) . toEqual ( 'great' ) ;
154
+ } ) ;
155
+
156
+ it ( "should include models" , function ( ) {
157
+ expect ( book . toJSON ( ) . author ) . toBeDefined ( ) ;
158
+ expect ( book . toJSON ( ) . author . name ) . toEqual ( 'John Doe' ) ;
159
+
160
+ expect ( book . toJSON ( ) . publishing_house ) . toBeDefined ( ) ;
161
+ expect ( book . toJSON ( ) . publishing_house . name ) . toEqual ( 'Books co.' ) ;
162
+ } ) ;
163
+
164
+ it ( "should not have _attributes collections" , function ( ) {
165
+ expect ( book . toJSON ( ) . pages_attributes ) . toBeUndefined ( ) ;
166
+ expect ( book . toJSON ( ) . customer_reviews_attributes ) . toBeUndefined ( ) ;
167
+ } ) ;
168
+
169
+ it ( "should not have _attributes models" , function ( ) {
170
+ expect ( book . toJSON ( ) . author_attributes ) . toBeUndefined ( ) ;
171
+ expect ( book . toJSON ( ) . publishing_house_attributes ) . toBeUndefined ( ) ;
120
172
} ) ;
121
173
} ) ;
122
174
} ) ;
0 commit comments