@@ -22,7 +22,6 @@ const settings = {
2222 }
2323}
2424
25-
2625describe ( 'the message connector has the correct structure' , ( ) => {
2726 var dbConnector
2827
@@ -200,11 +199,35 @@ describe( 'sets and gets values', () => {
200199 } )
201200} )
202201
203-
204202describe ( 'advanced sets' , ( ) => {
205203 var dbConnector , lastMessage = null , messages = [ ]
206204 const ITEM_NAME = 'some-other-table/some-other-key'
207205
206+ /**
207+ * A promisified version of dbConnector#set.
208+ *
209+ * Written mainly for testcases that call dbConnector#set successively,
210+ * Written as to avoid potential race conditions.
211+ *
212+ * #used in:
213+ * writes multiple values in quick succession to an existing table
214+ * writes multiple values in quick succession to a new table
215+ * writes a combination of values in quick succession
216+ *
217+ * @param {String } path
218+ * @param {Object } Value Object
219+ * @return {Promise } Resolves an object {_done: true, _error: null}, Rejects with an error
220+ */
221+ let set = ( dbConnector , path , { val} ) => new Promise ( ( fulfill , reject ) => {
222+ dbConnector . set ( path , { val} , ( err ) => {
223+ if ( err ) reject ( err )
224+ fulfill ( {
225+ _done : true ,
226+ _error : err
227+ } )
228+ } )
229+ } )
230+
208231 it ( 'creates the dbConnector' , ( done ) => {
209232 dbConnector = new DbConnector ( settings )
210233 expect ( dbConnector . isReady ) . to . equal ( false )
@@ -261,14 +284,18 @@ describe( 'advanced sets', () => {
261284 } )
262285
263286 it ( 'writes multiple values in quick succession to an existing table' , ( done ) => {
264- dbConnector . set ( 'some-table/itemA' , { val : 1 } , ( ) => { } )
265- dbConnector . set ( 'some-table/itemA' , { val : 2 } , ( ) => { } )
266- dbConnector . set ( 'some-table/itemA' , { val : 3 } , ( ) => { } )
267- dbConnector . set ( 'some-table/itemA' , { val : 4 } , ( ) => { } )
268- dbConnector . set ( 'some-table/itemA' , { val : 5 } , ( error ) => {
269- expect ( error ) . to . be . null
270- done ( )
271- } )
287+ let dbConnSet = set . bind ( null , dbConnector ) ;
288+
289+ dbConnSet ( 'some-table/itemA' , { val : 1 } )
290+ . then ( dbConnSet . bind ( null , 'some-table/itemA' , { val : 2 } ) )
291+ . then ( dbConnSet . bind ( null , 'some-table/itemA' , { val : 3 } ) )
292+ . then ( dbConnSet . bind ( null , 'some-table/itemA' , { val : 4 } ) )
293+ . then ( dbConnSet . bind ( null , 'some-table/itemA' , { val : 5 } ) )
294+ . then ( ( { _done, _error} ) => {
295+ expect ( _done ) . to . be . true
296+ expect ( _error ) . to . be . null
297+ done ( )
298+ } )
272299 } )
273300
274301 it ( 'retrieves the latest item from the last operation' , ( done ) => {
@@ -289,14 +316,18 @@ describe( 'advanced sets', () => {
289316
290317
291318 it ( 'writes multiple values in quick succession to a new table' , ( done ) => {
292- dbConnector . set ( 'new-table/itemA' , { val : 6 } , ( ) => { } )
293- dbConnector . set ( 'new-table/itemA' , { val : 7 } , ( ) => { } )
294- dbConnector . set ( 'new-table/itemA' , { val : 8 } , ( ) => { } )
295- dbConnector . set ( 'new-table/itemA' , { val : 9 } , ( ) => { } )
296- dbConnector . set ( 'new-table/itemA' , { val : 10 } , ( error ) => {
297- expect ( error ) . to . be . null
298- done ( )
299- } )
319+ let dbConnSet = set . bind ( null , dbConnector ) ;
320+
321+ dbConnSet ( 'new-table/itemA' , { val : 6 } )
322+ . then ( dbConnSet . bind ( null , 'new-table/itemA' , { val : 7 } ) )
323+ . then ( dbConnSet . bind ( null , 'new-table/itemA' , { val : 8 } ) )
324+ . then ( dbConnSet . bind ( null , 'new-table/itemA' , { val : 9 } ) )
325+ . then ( dbConnSet . bind ( null , 'new-table/itemA' , { val : 10 } ) )
326+ . then ( ( { _done, _error} ) => {
327+ expect ( _done ) . to . be . true
328+ expect ( _error ) . to . be . null
329+ done ( )
330+ } )
300331 } )
301332
302333 it ( 'retrieves the latest item from the last operation' , ( done ) => {
@@ -315,13 +346,17 @@ describe( 'advanced sets', () => {
315346 } )
316347
317348 it ( 'writes a combination of values in quick succession' , ( done ) => {
318- dbConnector . set ( 'table-a/item-a' , { val : 'aa' } , ( ) => { } )
319- dbConnector . set ( 'table-a/item-b' , { val : 'ab' } , ( ) => { } )
320- dbConnector . set ( 'table-b/item-a' , { val : 'ba' } , ( ) => { } )
321- dbConnector . set ( 'table-b/item-b' , { val : 'bb' } , ( error ) => {
322- expect ( error ) . to . be . null
323- done ( )
324- } )
349+ let dbConnSet = set . bind ( null , dbConnector ) ;
350+
351+ dbConnSet ( 'table-a/item-a' , { val : 'aa' } )
352+ . then ( dbConnSet . bind ( null , 'table-a/item-b' , { val : 'ab' } ) )
353+ . then ( dbConnSet . bind ( null , 'table-b/item-a' , { val : 'ba' } ) )
354+ . then ( dbConnSet . bind ( null , 'table-b/item-b' , { val : 'bb' } ) )
355+ . then ( ( { _done, _error} ) => {
356+ expect ( _done ) . to . be . true
357+ expect ( _error ) . to . be . null
358+ done ( )
359+ } )
325360 } )
326361
327362 it ( 'retrieves item aa' , ( done ) => {
0 commit comments