summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/idb-wpt-ported/cursor-overloads.test.ts
blob: 7da0ea8ff9c7e2bc1ab2d2886716051274f7fba5 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import test from "ava";
import { BridgeIDBCursor, BridgeIDBKeyRange } from "..";
import { BridgeIDBCursorWithValue } from "../bridge-idb";
import { IDBRequest } from "../idbtypes";
import { createdb } from "./wptsupport";

const IDBKeyRange = BridgeIDBKeyRange;

// Validate the overloads of IDBObjectStore.openCursor(),
// IDBIndex.openCursor() and IDBIndex.openKeyCursor()
test.cb("WPT test cursor-overloads.htm", (t) => {
  var db: any, store: any, index: any;

  var request = createdb(t);
  request.onupgradeneeded = function (e) {
    db = request.result;
    store = db.createObjectStore("store");
    index = store.createIndex("index", "value");
    store.put({ value: 0 }, 0);
    const trans = request.transaction!;
    trans.oncomplete = verifyOverloads;
  };

  async function verifyOverloads() {
    const trans = db.transaction("store");
    store = trans.objectStore("store");
    index = store.index("index");

    await checkCursorDirection(store.openCursor(), "next");
    await checkCursorDirection(store.openCursor(0), "next");
    await checkCursorDirection(store.openCursor(0, 'next'), "next");
    await checkCursorDirection(store.openCursor(0, 'nextunique'), "nextunique");
    await checkCursorDirection(store.openCursor(0, 'prev'), "prev");
    await checkCursorDirection(store.openCursor(0, 'prevunique'), "prevunique");

    await checkCursorDirection(store.openCursor(IDBKeyRange.only(0)), "next");
    await checkCursorDirection(
      store.openCursor(BridgeIDBKeyRange.only(0), 'next'),
      "next",
    );
    await checkCursorDirection(
      store.openCursor(IDBKeyRange.only(0), 'nextunique'),
      "nextunique",
    );
    await checkCursorDirection(
      store.openCursor(IDBKeyRange.only(0), 'prev'),
      "prev",
    );
    await checkCursorDirection(
      store.openCursor(IDBKeyRange.only(0), 'prevunique'),
      "prevunique",
    );

    await checkCursorDirection(index.openCursor(), "next");
    await checkCursorDirection(index.openCursor(0), "next");
    await checkCursorDirection(index.openCursor(0, 'next'), "next");
    await checkCursorDirection(index.openCursor(0, 'nextunique'), "nextunique");
    await checkCursorDirection(index.openCursor(0, 'prev'), "prev");
    await checkCursorDirection(index.openCursor(0, 'prevunique'), "prevunique");

    await checkCursorDirection(index.openCursor(IDBKeyRange.only(0)), "next");
    await checkCursorDirection(
      index.openCursor(IDBKeyRange.only(0), 'next'),
      "next",
    );
    await checkCursorDirection(
      index.openCursor(IDBKeyRange.only(0), 'nextunique'),
      "nextunique",
    );
    await checkCursorDirection(
      index.openCursor(IDBKeyRange.only(0), 'prev'),
      "prev",
    );
    await checkCursorDirection(
      index.openCursor(IDBKeyRange.only(0), 'prevunique'),
      "prevunique",
    );

    await checkCursorDirection(index.openKeyCursor(), "next");
    await checkCursorDirection(index.openKeyCursor(0), "next");
    await checkCursorDirection(index.openKeyCursor(0, 'next'), "next");
    await checkCursorDirection(index.openKeyCursor(0, 'nextunique'), "nextunique");
    await checkCursorDirection(index.openKeyCursor(0, 'prev'), "prev");
    await checkCursorDirection(index.openKeyCursor(0, 'prevunique'), "prevunique");

    await checkCursorDirection(index.openKeyCursor(IDBKeyRange.only(0)), "next");
    await checkCursorDirection(
      index.openKeyCursor(IDBKeyRange.only(0), 'next'),
      "next",
    );
    await checkCursorDirection(
      index.openKeyCursor(IDBKeyRange.only(0), 'nextunique'),
      "nextunique",
    );
    await checkCursorDirection(
      index.openKeyCursor(IDBKeyRange.only(0), 'prev'),
      "prev",
    );
    await checkCursorDirection(
      index.openKeyCursor(IDBKeyRange.only(0), 'prevunique'),
      "prevunique",
    );

    t.end();
  }

  function checkCursorDirection(
    request: IDBRequest,
    direction: string,
  ): Promise<void> {
    return new Promise<void>((resolve, reject) => {
      request.onsuccess = function (event: any) {
        t.notDeepEqual(event.target.result, null, "Check the result is not null");
        t.deepEqual(
          event.target.result.direction,
          direction,
          "Check the result direction",
        );
        resolve();
      };
    });
  }
});