import React from 'react' ; import { act } from 'react-dom/test-utils' ; import { DebeProvider , makeStore , awaitLoaded } from './index' ; import { render } from '@testing-library/react' ; import { MemoryDebe } from 'debe-memory' ; import '@testing-library/jest-dom/extend-expect' ; import { IQueryInput } from 'debe' ; import * as use from './react' ; import { STOREKEY } from './provider' ; export const collections = [ { name : 'lorem' , index : [ 'name' ] } ] ; export function clean ( obj ) { Object . keys ( obj ) . forEach ( key => { const { result } = obj [ key ] ; if ( result && Array . isArray ( result ) ) { obj [ key ] = result . map ( ( item ) => { const { id , rev , ... rest } = item ; return rest ; } ) ; } else if ( result ) { const { id , rev , ... rest } = result ; obj [ key ] = rest ; } } ) ; return obj ; } export interface ILorem { name : string ; } export function Component ( { children , mode = 'AllOnce' , arg } ) { const [ result , loading ] = use [ `use${ mode }` ] < ILorem > ( 'lorem' , arg ) ; if ( loading ) { return < span > Loading < /span>; } return ( < span > { ( Array . isArray ( result ) ? result : [ result ] ) . map ( ( x ) => x . name ) . join ( ', ' ) } { children } < /span>); } test ( 'react:basic' , async ( cb ) => { let failed = false ; const db = new MemoryDebe ( collections ) ; await db . initialize ( ) ; await db . insert ( 'lorem' , [ { name : 'Beni' } , { name : 'Alex' } ] ) ; const store = makeStore ( db ) ; const { asFragment } = render ( < DebeProvider initialize = { async ( db ) => { } } store = { store } value = { db } render = { ( ) => < Component />} loading={() => <span>Loading...</span > } />); await ( act as any ) ( async ( ) => { await awaitLoaded ( store ) ; } ) ; expect ( failed ) . toBe ( false ) ; expect ( asFragment ( ) ) . toMatchSnapshot ( ) ; expect ( clean ( store . getState ( ) [ STOREKEY ] ) ) . toMatchSnapshot ( ) ; await db . close ( ) ; cb ( ) ; } ) ;