@@ -134,6 +134,91 @@ describe('Document methods', function () {
134134    } ) ; 
135135  } ) ; 
136136
137+   describe ( '#exists' ,  function  ( )  { 
138+     beforeEach ( function  ( )  { 
139+       result  =  {  result : true  } ; 
140+       expectedQuery  =  { 
141+         index : 'bar' , 
142+         collection : 'foo' , 
143+         action : 'exists' , 
144+         controller : 'document' 
145+       } ; 
146+     } ) ; 
147+ 
148+     it ( 'should send the right query to Kuzzle' ,  function  ( )  { 
149+       var 
150+         options  =  {  queuable : false  } , 
151+         document  =  new  Document ( collection ) ; 
152+ 
153+       document . id  =  'foo' ; 
154+       document . exists ( options ) ; 
155+       should ( kuzzle . query ) . be . calledOnce ( ) ; 
156+       should ( kuzzle . query ) . calledWith ( expectedQuery ,  { _id : 'foo' ,  body : { } ,  meta : { } } ,  options ) ; 
157+     } ) ; 
158+ 
159+     it ( 'should handle arguments correctly' ,  function  ( )  { 
160+       var 
161+         document  =  new  Document ( collection ) , 
162+         cb1  =  sinon . stub ( ) , 
163+         cb2  =  sinon . stub ( ) ; 
164+ 
165+       document . id  =  'foo' ; 
166+ 
167+       document . exists ( cb1 ) ; 
168+       document . exists ( { } ,  cb2 ) ; 
169+ 
170+       should ( kuzzle . query ) . be . calledTwice ( ) ; 
171+ 
172+       kuzzle . query . yield ( null ,  result ) ; 
173+       should ( cb1 ) . be . calledOnce ( ) ; 
174+       should ( cb2 ) . be . calledOnce ( ) ; 
175+ 
176+       kuzzle . query . reset ( ) ; 
177+       document . exists ( ) ; 
178+       document . exists ( { } ) ; 
179+ 
180+       should ( kuzzle . query ) . be . calledTwice ( ) ; 
181+     } ) ; 
182+ 
183+     it ( 'should throw an error if no ID has been set' ,  function  ( )  { 
184+       should ( function  ( )  {  document . exists ( ) ;  } ) . throw ( Error ) ; 
185+       should ( function  ( )  {  document . exists ( { } ) ;  } ) . throw ( Error ) ; 
186+       should ( function  ( )  {  document . exists ( sinon . stub ( ) ) ;  } ) . throw ( Error ) ; 
187+       should ( function  ( )  {  document . exists ( { } ,  sinon . stub ( ) ) ;  } ) . throw ( Error ) ; 
188+       should ( kuzzle . query ) . not . be . called ( ) ; 
189+     } ) ; 
190+ 
191+     it ( 'should resolve the callback with true as the result' ,  function  ( done )  { 
192+       var  document  =  new  Document ( collection ) ; 
193+ 
194+       this . timeout ( 50 ) ; 
195+       document . id  =  'foo' ; 
196+ 
197+       document . exists ( function  ( err ,  res )  { 
198+         should ( err ) . be . null ( ) ; 
199+         should ( res ) . be . true ( ) ; 
200+         done ( ) ; 
201+       } ) ; 
202+ 
203+       should ( kuzzle . query ) . be . calledOnce ( ) ; 
204+       kuzzle . query . yield ( null ,  result ) ; 
205+     } ) ; 
206+ 
207+     it ( 'should revolve the callback with an error if one occurs' ,  function  ( done )  { 
208+       var  document  =  new  Document ( collection ) ; 
209+ 
210+       this . timeout ( 50 ) ; 
211+       document . id  =  'foo' ; 
212+ 
213+       document . exists ( function  ( err ,  res )  { 
214+         should ( err ) . be . exactly ( 'foobar' ) ; 
215+         should ( res ) . be . undefined ( ) ; 
216+         done ( ) ; 
217+       } ) ; 
218+       kuzzle . query . yield ( 'foobar' ) ; 
219+     } ) ; 
220+   } ) ; 
221+ 
137222  describe ( '#refresh' ,  function  ( )  { 
138223    beforeEach ( function  ( )  { 
139224      result  =  {  result : { _id : 'foo' ,  _version : 42 ,  _source : { some : 'content' } } } ; 
0 commit comments