1+ var should = require ( 'should' ) ;
2+
3+ var Store = require ( '../../lib/store' ) ;
4+
5+ describe ( 'JSON' , function ( ) {
6+ var store = new Store ( ) ;
7+
8+ store . Model ( 'User' , function ( ) {
9+ this . attribute ( 'login' ) ;
10+ this . hasMany ( 'posts' )
11+ } ) ;
12+
13+ store . Model ( 'Post' , function ( ) {
14+ this . attribute ( 'title' ) ;
15+ this . belongsTo ( 'user' ) ;
16+ } ) ;
17+
18+ var User = store . Model ( 'User' ) ;
19+
20+ var posts = [ { title :'foo' } , { title : 'bar' } ] ;
21+
22+ var phil = User . new ( { login : 'phil' , foo : 'bar' } ) ;
23+ var michl = User . new ( { login : 'michl' , foo : 'bar' , posts :posts } ) ;
24+
25+ Collection = User . chain ( ) . add ( phil ) . add ( michl ) ;
26+
27+ describe ( 'Record toJson()' , function ( ) {
28+
29+ it ( 'method exists' , function ( ) {
30+ phil . toJson . should . be . a . Function ;
31+ } )
32+
33+ it ( 'returns a new object' , function ( ) {
34+ var json = phil . toJson ( ) ;
35+ json . should . not . be . eql ( phil ) ;
36+ json . login . should . be . equal ( 'phil' ) ;
37+ should . not . exist ( json . foo ) ;
38+ json . posts . should . be . eql ( [ ] ) ;
39+ } ) ;
40+
41+ it ( 'returns a new object with relations' , function ( ) {
42+ var json = michl . toJson ( ) ;
43+ json . should . not . be . eql ( michl ) ;
44+ json . login . should . be . equal ( 'michl' ) ;
45+ should . not . exist ( json . foo ) ;
46+ json . posts . should . be . eql ( posts ) ;
47+ michl . posts . should . not . be . eql ( json . posts ) ;
48+ } ) ;
49+
50+ } ) ;
51+
52+
53+ describe ( 'Collection toJson()' , function ( ) {
54+
55+ it ( 'method exists' , function ( ) {
56+ Collection . toJson . should . be . a . Function ;
57+ } )
58+
59+ it ( 'returns a new object' , function ( ) {
60+ var json = Collection . toJson ( ) ;
61+ json . should . not . be . eql ( Collection ) ;
62+ json . should . be . instanceof ( Array ) ;
63+ json [ 0 ] . login . should . be . equal ( 'phil' ) ;
64+ } ) ;
65+
66+ it ( 'returns a new object with relations' , function ( ) {
67+ var json = Collection . toJson ( ) ;
68+ json [ 1 ] . login . should . be . equal ( 'michl' ) ;
69+ json [ 1 ] . posts . should . be . eql ( posts ) ;
70+ michl . posts . should . not . be . eql ( json [ 1 ] . posts ) ;
71+ } ) ;
72+
73+ } ) ;
74+
75+ } ) ;
0 commit comments