-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1189 from XYOracleNetwork/feature/indexed-db-init…
…ialization-state-detection Indexed DB Initialization State Detection
- Loading branch information
Showing
6 changed files
with
281 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
.../packages/diviner/packages/indexeddb/packages/boundwitness/src/spec/Diviner.Error.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable max-nested-callbacks */ | ||
import { Account } from '@xyo-network/account' | ||
import { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb' | ||
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' | ||
import { BoundWitness } from '@xyo-network/boundwitness-model' | ||
import { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model' | ||
import { MemoryNode } from '@xyo-network/node-memory' | ||
import { | ||
IDBCursor, | ||
IDBCursorWithValue, | ||
IDBDatabase, | ||
IDBFactory, | ||
IDBIndex, | ||
IDBKeyRange, | ||
IDBObjectStore, | ||
IDBOpenDBRequest, | ||
IDBRequest, | ||
IDBTransaction, | ||
IDBVersionChangeEvent, | ||
indexedDB, | ||
} from 'fake-indexeddb' | ||
|
||
import { IndexedDbBoundWitnessDiviner } from '../Diviner' | ||
|
||
// Augment window with prototypes to ensure instance of comparisons work | ||
window.IDBCursor = IDBCursor | ||
window.IDBCursorWithValue = IDBCursorWithValue | ||
window.IDBDatabase = IDBDatabase | ||
window.IDBFactory = IDBFactory | ||
window.IDBIndex = IDBIndex | ||
window.IDBKeyRange = IDBKeyRange | ||
window.IDBObjectStore = IDBObjectStore | ||
window.IDBOpenDBRequest = IDBOpenDBRequest | ||
window.IDBRequest = IDBRequest | ||
window.IDBTransaction = IDBTransaction | ||
window.IDBVersionChangeEvent = IDBVersionChangeEvent | ||
window.indexedDB = indexedDB | ||
|
||
/** | ||
* @group module | ||
* @group diviner | ||
*/ | ||
describe('IndexedDbBoundWitnessDiviner.Errors', () => { | ||
const dbName = 'testDb' | ||
const storeName = 'testStore' | ||
let sut: IndexedDbBoundWitnessDiviner | ||
const values: BoundWitness[] = [] | ||
describe('divine', () => { | ||
const createTestNode = async (testDbName = 'INCORRECT-DB-NAME', testStoreName = 'INCORRECT-STORE-NAME') => { | ||
const archivist = await IndexedDbArchivist.create({ | ||
account: Account.randomSync(), | ||
config: { dbName, schema: IndexedDbArchivist.configSchema, storeName }, | ||
}) | ||
const [bw] = await new BoundWitnessBuilder().build() | ||
values.push(bw) | ||
await archivist.insert(values) | ||
const sut = await IndexedDbBoundWitnessDiviner.create({ | ||
account: Account.randomSync(), | ||
config: { | ||
archivist: archivist.address, | ||
dbName: testDbName, | ||
schema: IndexedDbBoundWitnessDiviner.configSchema, | ||
storeName: testStoreName, | ||
}, | ||
}) | ||
const node = await MemoryNode.create({ | ||
account: Account.randomSync(), | ||
config: { schema: MemoryNode.configSchema }, | ||
}) | ||
const modules = [archivist, sut] | ||
await node.start() | ||
await Promise.all( | ||
modules.map(async (mod) => { | ||
await node.register(mod) | ||
await node.attach(mod.address, true) | ||
}), | ||
) | ||
return sut | ||
} | ||
describe('when DB and store do not exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode('INCORRECT-DB-NAME', 'INCORRECT-STORE-NAME') | ||
}) | ||
it('returns empty array', async () => { | ||
const result = await sut.divine([{ schema: BoundWitnessDivinerQuerySchema }]) | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
describe('when DB exists but store does not exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode(dbName, 'INCORRECT-STORE-NAME') | ||
}) | ||
it('returns empty array', async () => { | ||
const result = await sut.divine([{ schema: BoundWitnessDivinerQuerySchema }]) | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
describe('when DB and store exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode(dbName, storeName) | ||
}) | ||
it('returns values', async () => { | ||
const result = await sut.divine([{ schema: BoundWitnessDivinerQuerySchema }]) | ||
expect(result).toEqual(values) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...dules/packages/diviner/packages/indexeddb/packages/payload/src/spec/Diviner.Error.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable max-nested-callbacks */ | ||
import { Account } from '@xyo-network/account' | ||
import { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb' | ||
import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model' | ||
import { MemoryNode } from '@xyo-network/node-memory' | ||
import { | ||
IDBCursor, | ||
IDBCursorWithValue, | ||
IDBDatabase, | ||
IDBFactory, | ||
IDBIndex, | ||
IDBKeyRange, | ||
IDBObjectStore, | ||
IDBOpenDBRequest, | ||
IDBRequest, | ||
IDBTransaction, | ||
IDBVersionChangeEvent, | ||
indexedDB, | ||
} from 'fake-indexeddb' | ||
|
||
import { IndexedDbPayloadDiviner } from '../Diviner' | ||
|
||
// Augment window with prototypes to ensure instance of comparisons work | ||
window.IDBCursor = IDBCursor | ||
window.IDBCursorWithValue = IDBCursorWithValue | ||
window.IDBDatabase = IDBDatabase | ||
window.IDBFactory = IDBFactory | ||
window.IDBIndex = IDBIndex | ||
window.IDBKeyRange = IDBKeyRange | ||
window.IDBObjectStore = IDBObjectStore | ||
window.IDBOpenDBRequest = IDBOpenDBRequest | ||
window.IDBRequest = IDBRequest | ||
window.IDBTransaction = IDBTransaction | ||
window.IDBVersionChangeEvent = IDBVersionChangeEvent | ||
window.indexedDB = indexedDB | ||
|
||
/** | ||
* @group module | ||
* @group diviner | ||
*/ | ||
describe('IndexedDbPayloadDiviner.Errors', () => { | ||
const dbName = 'testDb' | ||
const storeName = 'testStore' | ||
let sut: IndexedDbPayloadDiviner | ||
const values = [ | ||
{ | ||
schema: 'network.xyo.test', | ||
url: 'https://xyo.network', | ||
}, | ||
] | ||
describe('divine', () => { | ||
const createTestNode = async (testDbName = 'INCORRECT-DB-NAME', testStoreName = 'INCORRECT-STORE-NAME') => { | ||
const archivist = await IndexedDbArchivist.create({ | ||
account: Account.randomSync(), | ||
config: { dbName, schema: IndexedDbArchivist.configSchema, storeName }, | ||
}) | ||
await archivist.insert(values) | ||
const sut = await IndexedDbPayloadDiviner.create({ | ||
account: Account.randomSync(), | ||
config: { | ||
archivist: archivist.address, | ||
dbName: testDbName, | ||
schema: IndexedDbPayloadDiviner.configSchema, | ||
storeName: testStoreName, | ||
}, | ||
}) | ||
const node = await MemoryNode.create({ | ||
account: Account.randomSync(), | ||
config: { schema: MemoryNode.configSchema }, | ||
}) | ||
const modules = [archivist, sut] | ||
await node.start() | ||
await Promise.all( | ||
modules.map(async (mod) => { | ||
await node.register(mod) | ||
await node.attach(mod.address, true) | ||
}), | ||
) | ||
return sut | ||
} | ||
describe('when DB and store do not exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode('INCORRECT-DB-NAME', 'INCORRECT-STORE-NAME') | ||
}) | ||
it('returns empty array', async () => { | ||
const result = await sut.divine([{ schema: PayloadDivinerQuerySchema }]) | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
describe('when DB exists but store does not exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode(dbName, 'INCORRECT-STORE-NAME') | ||
}) | ||
it('returns empty array', async () => { | ||
const result = await sut.divine([{ schema: PayloadDivinerQuerySchema }]) | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
describe('when DB and store exist', () => { | ||
beforeAll(async () => { | ||
sut = await createTestNode(dbName, storeName) | ||
}) | ||
it('returns values', async () => { | ||
const result = await sut.divine([{ schema: PayloadDivinerQuerySchema }]) | ||
expect(result).toEqual(values) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters