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

/* exported testSteps */
async function testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";
  const objectStoreName = "foo";
  const indexName = "bar",
    keyPath = "bar";

  info("Opening database");

  let request = indexedDB.open(name);
  let event = await expectingUpgrade(request);

  let db = event.target.result;

  info("Creating objectStore");

  let objectStore = db.createObjectStore(objectStoreName);

  info("Creating a duplicated object store to get an error");

  try {
    db.createObjectStore(objectStoreName);
    ok(
      false,
      "ConstraintError should be thrown if object store already exists"
    );
  } catch (e) {
    ok(true, "ConstraintError should be thrown if object store already exists");
    is(
      e.message,
      "IDBDatabase.createObjectStore: Object store named '" +
        objectStoreName +
        "' already exists at index '0'",
      "Threw with correct error message"
    );
  }

  info("Creating an index");

  objectStore.createIndex(indexName, keyPath);

  info("Creating a duplicated indexes to verify the error message");

  try {
    objectStore.createIndex(indexName, keyPath);

    ok(false, "ConstraintError should be thrown if index already exists");
  } catch (e) {
    ok(true, "ConstraintError should be thrown if index already exists");
    is(
      e.message,
      `IDBObjectStore.createIndex: Index named '${indexName}' already exists at index '0'`,
      "Threw with correct error message"
    );
  }

  await expectingSuccess(request);
  db.close();
}