/
indexed-db.js
44 lines (40 loc) · 1.31 KB
/
indexed-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { set } from '@ember/object';
import { run } from '@ember/runloop';
import { settled } from '@ember/test-helpers';
/**
* ## Test Helpers
*
* This test helper can be used to interact with the database in your tests.
* It works with the new test syntax, and can be used in all styles of tests (e.g. acceptance, integration, unit).
*
* ```js
* import { setupIndexedDb } from 'ember-indexeddb/test-support/helpers/indexed-db';
*
* module('Acceptance | my test', function(hooks) {
* setupApplicationTest(hooks);
* setupIndexedDb(hooks);
*
* // add your actual tests here
* });
* ```
*
* This will setup & open a randomly named database for this test, and close & delete it again after the test.
*
* @module Test Helpers
* @public
*/
export function setupIndexedDb(hooks) {
hooks.beforeEach(function () {
let indexedDb = this.owner.lookup('service:indexed-db');
set(indexedDb, 'databaseName', `test-indexed-db-${+new Date()}`);
return indexedDb.setupTask.perform();
});
hooks.afterEach(async function () {
let indexedDb = this.owner.lookup('service:indexed-db');
await settled();
await run(() => indexedDb.waitForQueueTask.perform());
await run(() => indexedDb.waitForQueueTask.perform());
await indexedDb.dropDatabaseTask.perform();
});
}
export default setupIndexedDb;