@@ -115,4 +115,125 @@ describe('DS.defineResource(definition)', function () {
115115 assert . equal ( lifecycle . beforeInject . callCount , 1 , 'beforeInject should have been called' ) ;
116116 assert . equal ( lifecycle . afterInject . callCount , 1 , 'afterInject should have been called' ) ;
117117 } ) ;
118+ it ( 'should allow definition of computed properties' , function ( ) {
119+ var callCount = 0 ;
120+
121+ DS . defineResource ( {
122+ name : 'person' ,
123+ computed : {
124+ fullName : function ( first , last ) {
125+ callCount ++ ;
126+ return first + ' ' + last ;
127+ }
128+ }
129+ } ) ;
130+
131+ DS . inject ( 'person' , {
132+ first : 'John' ,
133+ last : 'Anderson' ,
134+ email : 'john.anderson@test.com' ,
135+ id : 1
136+ } ) ;
137+
138+ var person = DS . get ( 'person' , 1 ) ;
139+
140+ assert . deepEqual ( JSON . stringify ( person ) , JSON . stringify ( {
141+ first : 'John' ,
142+ last : 'Anderson' ,
143+ email : 'john.anderson@test.com' ,
144+ id : 1 ,
145+ fullName : 'John Anderson'
146+ } ) ) ;
147+ assert . equal ( person . fullName , 'John Anderson' ) ;
148+ assert . equal ( lifecycle . beforeInject . callCount , 1 , 'beforeInject should have been called' ) ;
149+ assert . equal ( lifecycle . afterInject . callCount , 1 , 'afterInject should have been called' ) ;
150+
151+ person . first = 'Johnny' ;
152+
153+ // digest loop hasn't happened yet
154+ assert . equal ( DS . get ( 'person' , 1 ) . first , 'Johnny' ) ;
155+ assert . equal ( DS . get ( 'person' , 1 ) . fullName , 'John Anderson' ) ;
156+
157+ DS . digest ( ) ;
158+
159+ assert . deepEqual ( person , {
160+ first : 'Johnny' ,
161+ last : 'Anderson' ,
162+ email : 'john.anderson@test.com' ,
163+ id : 1 ,
164+ fullName : 'Johnny Anderson'
165+ } ) ;
166+ assert . equal ( person . fullName , 'Johnny Anderson' ) ;
167+
168+ // should work with $timeout
169+ $timeout ( function ( ) {
170+ person . first = 'Jack' ;
171+
172+ DS . digest ( ) ;
173+
174+ assert . deepEqual ( person , {
175+ first : 'Jack' ,
176+ last : 'Anderson' ,
177+ email : 'john.anderson@test.com' ,
178+ id : 1 ,
179+ fullName : 'Jack Anderson'
180+ } ) ;
181+ assert . equal ( person . fullName , 'Jack Anderson' ) ;
182+ } ) ;
183+
184+ $timeout . flush ( ) ;
185+
186+ // computed property function should not be called
187+ // when a property changes that isn't a dependency
188+ // of the computed property
189+ person . email = 'ja@test.com' ;
190+
191+ DS . digest ( ) ;
192+
193+ assert . equal ( callCount , 3 , 'fullName() should have been called 3 times' ) ;
194+ } ) ;
195+ it ( 'should work if idAttribute is a computed property computed property' , function ( ) {
196+ DS . defineResource ( {
197+ name : 'person' ,
198+ computed : {
199+ id : function ( first , last ) {
200+ return first + '_' + last ;
201+ }
202+ }
203+ } ) ;
204+
205+ DS . inject ( 'person' , {
206+ first : 'John' ,
207+ last : 'Anderson' ,
208+ email : 'john.anderson@test.com'
209+ } ) ;
210+
211+ var person = DS . get ( 'person' , 'John_Anderson' ) ;
212+
213+ assert . deepEqual ( JSON . stringify ( person ) , JSON . stringify ( {
214+ first : 'John' ,
215+ last : 'Anderson' ,
216+ email : 'john.anderson@test.com' ,
217+ id : 'John_Anderson'
218+ } ) ) ;
219+ assert . equal ( person . id , 'John_Anderson' ) ;
220+ assert . equal ( lifecycle . beforeInject . callCount , 1 , 'beforeInject should have been called' ) ;
221+ assert . equal ( lifecycle . afterInject . callCount , 1 , 'afterInject should have been called' ) ;
222+
223+ person . first = 'Johnny' ;
224+
225+ // digest loop hasn't happened yet
226+ assert . equal ( DS . get ( 'person' , 'John_Anderson' ) . first , 'Johnny' ) ;
227+ assert . equal ( DS . get ( 'person' , 'John_Anderson' ) . id , 'John_Anderson' ) ;
228+
229+ DS . digest ( ) ;
230+
231+ assert . deepEqual ( person , {
232+ first : 'Johnny' ,
233+ last : 'Anderson' ,
234+ email : 'john.anderson@test.com' ,
235+ id : 'Johnny_Anderson'
236+ } ) ;
237+ assert . equal ( person . id , 'Johnny_Anderson' ) ;
238+ } ) ;
118239} ) ;
0 commit comments