1+ var capitalize = function ( string ) {
2+ return string . charAt ( 0 ) . toUpperCase ( ) + string . substring ( 1 ) . toLowerCase ( ) ;
3+ } ;
4+
5+ Tinytest . add ( 'Collection Hooks' , function ( test ) {
6+
7+ //XXX on client
8+ // - insert/update/remove invokes callbacks
9+ // - callbacks not invoked if inserted directly into the collection
10+ //
11+ //XXX on server
12+ // - insert/update/remove invokes callbacks
13+ // - callbacks ALSO invoked if you insert directly into the collection
14+
15+
16+ if ( Meteor . isClient ) {
17+ ClientItems = new Mongo . Collection ( 'client_items' , { connection : null } ) ;
18+
19+ var hookCalls = {
20+ total : 0
21+ } ;
22+
23+ _ . each ( [ 'insert' , 'update' , 'remove' ] , function ( method ) {
24+ var beforeHook = 'before' + capitalize ( method ) ;
25+ var afterHook = 'after' + capitalize ( method ) ;
26+
27+ hookCalls [ beforeHook ] = 0 ;
28+ hookCalls [ afterHook ] = 0 ;
29+
30+ ClientItems [ beforeHook ] ( function ( ) {
31+ hookCalls [ beforeHook ] ++ ;
32+ hookCalls . total ++ ;
33+ } ) ;
34+
35+ ClientItems [ afterHook ] ( function ( ) {
36+ hookCalls [ afterHook ] ++ ;
37+ hookCalls . total ++ ;
38+ } ) ;
39+ } ) ;
40+
41+ var id = ClientItems . _collection . insert ( { title : '1' } ) ;
42+ test . equal ( hookCalls . total , 0 , 'Hooks should not be called for LocalCollection.prototype.insert' ) ;
43+
44+ ClientItems . _collection . update ( { _id : id } , { $set : { title : 'updated' } } ) ;
45+ test . equal ( hookCalls . total , 0 , 'Hooks should not be called for LocalCollection.prototype.update' ) ;
46+
47+ ClientItems . _collection . remove ( { _id : id } ) ;
48+ test . equal ( hookCalls . total , 0 , 'Hooks should not be called for LocalCollection.prototype.remove' ) ;
49+
50+ var id = ClientItems . insert ( { title : '2' } ) ;
51+ test . equal ( hookCalls . total , 2 , 'Hooks should be called for Collection.prototype.insert' ) ;
52+ test . equal ( hookCalls . beforeInsert , 1 ) ;
53+ test . equal ( hookCalls . afterInsert , 1 ) ;
54+
55+ ClientItems . update ( { _id : id } , { $set : { title : 'updated' } } ) ;
56+ test . equal ( hookCalls . total , 4 , 'Hooks should be called for Collection.prototype.update' ) ;
57+ test . equal ( hookCalls . beforeUpdate , 1 ) ;
58+ test . equal ( hookCalls . afterUpdate , 1 ) ;
59+
60+ ClientItems . remove ( { _id : id } ) ;
61+ test . equal ( hookCalls . total , 6 , 'Hooks should be called for Collection.prototype.remove' ) ;
62+ test . equal ( hookCalls . beforeRemove , 1 ) ;
63+ test . equal ( hookCalls . afterRemove , 1 ) ;
64+ }
65+
66+ if ( Meteor . isServer ) {
67+ ServerItems = new Mongo . Collection ( 'server_items' ) ;
68+
69+ ServerItems . allow ( {
70+ insert : function ( ) { return true ; } ,
71+ update : function ( ) { return true ; } ,
72+ remove : function ( ) { return true ; }
73+ } ) ;
74+
75+ var hookCalls = {
76+ total : 0
77+ } ;
78+
79+ _ . each ( [ 'insert' , 'update' , 'remove' ] , function ( method ) {
80+ var beforeHook = 'before' + capitalize ( method ) ;
81+ var afterHook = 'after' + capitalize ( method ) ;
82+
83+ hookCalls [ beforeHook ] = 0 ;
84+ hookCalls [ afterHook ] = 0 ;
85+
86+ ServerItems [ beforeHook ] ( function ( ) {
87+ hookCalls [ beforeHook ] ++ ;
88+ hookCalls . total ++ ;
89+ } ) ;
90+
91+ ServerItems [ afterHook ] ( function ( ) {
92+ hookCalls [ afterHook ] ++ ;
93+ hookCalls . total ++ ;
94+ } ) ;
95+ } ) ;
96+
97+ var id = ServerItems . _collection . insert ( { title : '1' } ) ;
98+ test . equal ( hookCalls . total , 2 , 'Hooks should be called for Mongo insert' ) ;
99+ test . equal ( hookCalls . beforeInsert , 1 ) ;
100+ test . equal ( hookCalls . afterInsert , 1 ) ;
101+
102+ ServerItems . _collection . update ( { _id : id } , { $set : { title : 'updated' } } ) ;
103+ test . equal ( hookCalls . total , 4 , 'Hooks should be called for Mongo update' ) ;
104+ test . equal ( hookCalls . beforeUpdate , 1 ) ;
105+ test . equal ( hookCalls . afterUpdate , 1 ) ;
106+
107+ ServerItems . _collection . remove ( { _id : id } ) ;
108+ test . equal ( hookCalls . total , 6 , 'Hooks should be called for Mongo remove' ) ;
109+ test . equal ( hookCalls . beforeRemove , 1 ) ;
110+ test . equal ( hookCalls . afterRemove , 1 ) ;
111+
112+ }
113+ } ) ;
0 commit comments