import { expect } from 'chai' ; import Dexie from 'dexie' ; import setGlobalVars from 'indexeddbshim' ; import { schemaAddon , SchemasTableName } from '.' ; import { personSchema , shouldHaveThrown } from '../../utils/spec.utils' ; const { indexedDB , IDBKeyRange } = setGlobalVars ( { } , { checkOrigin : false } ) ; const databaseName = 'schema' ; describe ( 'ThreadDB' , function ( ) { context ( 'schema middleware' , function ( ) { let db ; before ( async function ( ) { db = new Dexie ( databaseName , { indexedDB , IDBKeyRange , addons : [ ... Dexie . addons , schemaAddon ] , } ) ; db . version ( 1 ) . stores ( { person : '++id,name,age' , } ) ; expect ( db . table ( SchemasTableName ) ) . to . not . be . undefined ; await db . open ( ) ; } ) ; after ( async function ( ) { db . close ( ) ; await db . delete ( ) ; } ) ; it ( 'should work' , async function ( ) { this . timeout ( 100000 ) ; const person = db . table ( 'person' ) ; await person . put ( { _id : 'test' , name : 'dev' , extra : 'would throw' , } ) ; expect ( await person . count ( ) ) . to . equal ( 1 ) ; await person . setSchema ( personSchema ) ; expect ( await db . table ( SchemasTableName ) . count ( ) ) . to . equal ( 1 ) ; try { await db . transaction ( 'readwrite' , [ 'person' ] , async ( tx ) => { const person = tx . table ( 'person' ) ; const friend = await person . get ( { name : 'dev' } ) ; friend . age = undefined ; await person . put ( friend ) ; } ) ; throw shouldHaveThrown ; } catch ( err ) { expect ( err . toString ( ) ) . to . include ( 'validation failed' ) ; } const schemas = db . table ( SchemasTableName ) ; const array = await schemas . find ( ) . toArray ( ) ; expect ( array ) . to . have . lengthOf ( 1 ) ; } ) ; } ) ; } ) ;