import { Engine , EngineAttributes , Filters , Documents } from 'soukai' ; import Clock from '@/utils/Clock' ; export default class TestingEngine implements Engine { private engine : Engine ; private error ? : Error ; private delay ? : number ; constructor ( engine ) { this . engine = engine ; } public setError ( error ) { this . error = error ; } public setDelay ( delay ) { this . delay = delay ; } public async create ( collection , attributes , id ? ) { await this . executeModifications ( ) ; return this . engine . create ( collection , attributes , id ) ; } public async readOne ( collection , id ) { await this . executeModifications ( ) ; return this . engine . readOne ( collection , id ) ; } public async readMany ( collection , filters ? ) { await this . executeModifications ( ) ; return this . engine . readMany ( collection , filters ) ; } public async update ( collection , id , updatedAttributes , removedAttributes ) { await this . executeModifications ( ) ; return this . engine . update ( collection , id , updatedAttributes , removedAttributes ) ; } public async delete ( collection , id ) { await this . executeModifications ( ) ; return this . engine . delete ( collection , id ) ; } private async executeModifications ( ) { await this . waitForDelay ( ) ; this . throwError ( ) ; } private throwError ( ) { if ( ! this . error ) return ; throw this . error ; } private async waitForDelay ( ) { if ( ! this . delay ) return ; await Clock . sleep ( this . delay ) ; } }