summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_create_index.js
blob: a925bd79d630d906c73ae4b52238976cf78a8c61 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/* exported testGenerator */
var testGenerator = testSteps();

function* testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";
  const objectStoreInfo = [
    { name: "a", options: { keyPath: "id", autoIncrement: true } },
    { name: "b", options: { keyPath: "id", autoIncrement: false } },
  ];
  const indexInfo = [
    { name: "1", keyPath: "unique_value", options: { unique: true } },
    { name: "2", keyPath: "value", options: { unique: false } },
    { name: "3", keyPath: "value", options: { unique: false } },
    { name: "", keyPath: "value", options: { unique: false } },
    { name: null, keyPath: "value", options: { unique: false } },
    { name: undefined, keyPath: "value", options: { unique: false } },
  ];

  let request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  let event = yield undefined;
  let db = event.target.result;

  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];
    let objectStore = info.hasOwnProperty("options")
      ? db.createObjectStore(info.name, info.options)
      : db.createObjectStore(info.name);

    try {
      request = objectStore.createIndex("Hola");
      ok(false, "createIndex with no keyPath should throw");
    } catch (e) {
      ok(true, "createIndex with no keyPath should throw");
    }

    let ex;
    try {
      objectStore.createIndex("Hola", ["foo"], { multiEntry: true });
    } catch (e) {
      ex = e;
    }
    ok(ex, "createIndex with array keyPath and multiEntry should throw");
    is(ex.name, "InvalidAccessError", "should throw right exception");
    ok(ex instanceof DOMException, "should throw right exception");
    is(
      ex.code,
      DOMException.INVALID_ACCESS_ERR,
      "should throw right exception"
    );

    try {
      objectStore.createIndex("foo", "bar", 10);
      ok(false, "createIndex with bad options should throw");
    } catch (e) {
      ok(true, "createIndex with bad options threw");
    }

    ok(
      objectStore.createIndex("foo", "bar", { foo: "" }),
      "createIndex with unknown options should not throw"
    );
    objectStore.deleteIndex("foo");

    // Test index creation, and that it ends up in indexNames.
    let objectStoreName = info.name;
    for (let j = 0; j < indexInfo.length; j++) {
      let info = indexInfo[j];
      let count = objectStore.indexNames.length;
      let index = info.hasOwnProperty("options")
        ? objectStore.createIndex(info.name, info.keyPath, info.options)
        : objectStore.createIndex(info.name, info.keyPath);

      let name = info.name;
      if (name === null) {
        name = "null";
      } else if (name === undefined) {
        name = "undefined";
      }

      is(index.name, name, "correct name");
      is(index.keyPath, info.keyPath, "correct keyPath");
      is(index.unique, info.options.unique, "correct uniqueness");

      is(objectStore.indexNames.length, count + 1, "indexNames grew in size");
      let found = false;
      for (let k = 0; k < objectStore.indexNames.length; k++) {
        if (objectStore.indexNames.item(k) == name) {
          found = true;
          break;
        }
      }
      ok(found, "Name is on objectStore.indexNames");

      ok(event.target.transaction, "event has a transaction");
      ok(event.target.transaction.db === db, "transaction has the right db");
      is(
        event.target.transaction.mode,
        "versionchange",
        "transaction has the correct mode"
      );
      is(
        event.target.transaction.objectStoreNames.length,
        i + 1,
        "transaction only has one object store"
      );
      ok(
        event.target.transaction.objectStoreNames.contains(objectStoreName),
        "transaction has the correct object store"
      );
    }
  }

  request.onsuccess = grabEventAndContinueHandler;
  request.onupgradeneeded = unexpectedSuccessHandler;

  event = yield undefined;

  finishTest();
}