summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/idb-wpt-ported/idbobjectstore-get.test.ts
blob: 0c9d30b7d72fa08f2556ceac4e3201e033acd969 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import test from "ava";
import { BridgeIDBKeyRange, BridgeIDBRequest } from "..";
import { IDBDatabase } from "../idbtypes";
import { createdb } from "./wptsupport";

// IDBObjectStore.get() - key is a number
test.cb("WPT idbobjectstore_get.htm", (t) => {
  var db: any,
    record = { key: 3.14159265, property: "data" };

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    db.createObjectStore("store", { keyPath: "key" }).add(record);
  };

  open_rq.onsuccess = function (e: any) {
    var rq = db.transaction("store").objectStore("store").get(record.key);

    rq.onsuccess = function (e: any) {
      t.deepEqual(e.target.result.key, record.key);
      t.deepEqual(e.target.result.property, record.property);
      t.end();
    };
  };
});

// IDBObjectStore.get() - key is a string
test.cb("WPT idbobjectstore_get2.htm", (t) => {
  var db: any,
    record = { key: "this is a key that's a string", property: "data" };

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    db.createObjectStore("store", { keyPath: "key" }).add(record);
  };

  open_rq.onsuccess = function (e: any) {
    var rq = db.transaction("store").objectStore("store").get(record.key);

    rq.onsuccess = function (e: any) {
      t.deepEqual(e.target.result.key, record.key);
      t.deepEqual(e.target.result.property, record.property);
      t.end();
    };
  };
});

// IDBObjectStore.get() - key is a date
test.cb("WPT idbobjectstore_get3.htm", (t) => {
  var db: any;
  const record = { key: new Date(), property: "data" };

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    db.createObjectStore("store", { keyPath: "key" }).add(record);
  };

  open_rq.onsuccess = function (e: any) {
    var rq = db.transaction("store").objectStore("store").get(record.key);

    rq.onsuccess = function (e: any) {
      t.deepEqual(e.target.result.key.valueOf(), record.key.valueOf());
      t.deepEqual(e.target.result.property, record.property);
      t.end();
    };
  };
});

// IDBObjectStore.get() - attempt to retrieve a record that doesn't exist
test.cb("WPT idbobjectstore_get4.htm", (t) => {
  var db: any;

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    var rq = db.createObjectStore("store", { keyPath: "key" }).get(1);
    rq.onsuccess = function (e: any) {
      t.deepEqual(e.target.results, undefined);
      setTimeout(function () {
        t.end();
      }, 10);
    };
  };

  open_rq.onsuccess = function () {};
});

// IDBObjectStore.get() - returns the record with the first key in the range
test.cb("WPT idbobjectstore_get5.htm", (t) => {
  var db: any;
  var open_rq = createdb(t);

  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    var os = db.createObjectStore("store");

    for (var i = 0; i < 10; i++) os.add("data" + i, i);
  };

  open_rq.onsuccess = function (e: any) {
    db
      .transaction("store")
      .objectStore("store")
      .get(BridgeIDBKeyRange.bound(3, 6)).onsuccess = function (e: any) {
      t.deepEqual(e.target.result, "data3", "get(3-6)");
      t.end();
    };
  };
});

// IDBObjectStore.get() - throw TransactionInactiveError on aborted transaction
test.cb("WPT idbobjectstore_get6.htm", (t) => {
  var db: any;

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    db.createObjectStore("store", { keyPath: "key" });
  };

  open_rq.onsuccess = function (e: any) {
    var store = db.transaction("store").objectStore("store");
    store.transaction.abort();
    t.throws(
      function () {
        store.get(1);
      },
      { name: "TransactionInactiveError" },
      "throw TransactionInactiveError on aborted transaction.",
    );
    t.end();
  };
});

// IDBObjectStore.get() - throw DataError when using invalid key
test.cb("WPT idbobjectstore_get7.htm", (t) => {
  var db: any;

  var open_rq = createdb(t);
  open_rq.onupgradeneeded = function (e: any) {
    db = e.target.result;
    db.createObjectStore("store", { keyPath: "key" });
  };

  open_rq.onsuccess = function (e: any) {
    var store = db.transaction("store").objectStore("store");
    t.throws(
      function () {
        store.get(null);
      },
      { name: "DataError" },
      "throw DataError when using invalid key.",
    );
    t.end();
  };
});