import { expect } from 'chai' ; import Dexie from 'dexie' ; import setGlobalVars from 'indexeddbshim' ; import { changesAddon , ChangeTableName } from '.' ; const { indexedDB , IDBKeyRange } = setGlobalVars ( { } , { checkOrigin : false } ) ; const databaseName = 'changes' ; describe ( 'ThreadDB' , function ( ) { context ( 'changes middleware' , function ( ) { let db ; before ( async function ( ) { db = new Dexie ( databaseName , { indexedDB , IDBKeyRange , addons : [ ... Dexie . addons , changesAddon ] , } ) ; db . version ( 1 ) . stores ( { friends : '++id,name,shoeSize,address.city' , other : '++id' , } ) ; expect ( db . table ( ChangeTableName ) ) . to . not . be . undefined ; await db . open ( ) ; } ) ; after ( async function ( ) { db . close ( ) ; await db . delete ( ) ; } ) ; it ( 'should work' , async function ( ) { const friends = db . table ( 'friends' ) ; await friends . put ( { id : 'test' , name : 'dev' , shoeSize : 1 , address : { city : 'victoria' , } , } ) ; expect ( await friends . count ( ) ) . to . equal ( 1 ) ; await db . transaction ( 'readwrite' , [ 'friends' ] , async ( tx ) => { const friends = tx . table ( 'friends' ) ; const friend = await friends . get ( { name : 'dev' } ) ; ++ friend . shoeSize ; await friends . put ( friend ) ; await db . transaction ( 'readwrite' , friends , async ( ) => { const id = await friends . bulkAdd ( [ { id : 'steve' , name : 'steve' , shoeSize : 99 , address : { city : 'nothing' , } , } , { id : 'blah' , name : 'guy' , shoeSize : 88 , address : { city : 'unknown' , } , } , ] ) ; expect ( await friends . count ( ) ) . to . equal ( 3 ) ; const friend = await friends . get ( id ) ; friend . name = 'other' ; await friends . put ( friend ) ; expect ( await friends . count ( ) ) . to . equal ( 3 ) ; await friends . delete ( id ) ; } ) ; } ) ; expect ( await friends . count ( ) ) . to . equal ( 2 ) ; const changes = db . table ( ChangeTableName ) ; const array = await changes . find ( ) . toArray ( ) ; expect ( array ) . to . have . lengthOf ( 6 ) ; } ) ; } ) ; } ) ;