summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_add_put.js
blob: ba880957c025859636874f3f7f5a3438e5e02b96 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
 * 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";
  let openRequest = indexedDB.open(name, 1);
  openRequest.onerror = errorHandler;
  openRequest.onupgradeneeded = grabEventAndContinueHandler;
  openRequest.onsuccess = unexpectedSuccessHandler;
  let event = yield undefined;
  let db = event.target.result;

  for (let autoincrement of [true, false]) {
    for (let keypath of [false, true, "missing", "invalid"]) {
      for (let method of ["put", "add"]) {
        for (let explicit of [true, false, undefined, "invalid"]) {
          for (let existing of [true, false]) {
            let speccedNoKey = (!keypath || keypath == "missing") && !explicit;

            // We can't do 'existing' checks if we use autogenerated key
            if (speccedNoKey && autoincrement && existing) {
              continue;
            }

            // Create store
            if (db.objectStoreNames.contains("mystore")) {
              db.deleteObjectStore("mystore");
            }
            let store = db.createObjectStore("mystore", {
              autoIncrement: autoincrement,
              keyPath: keypath ? "id" : null,
            });

            let test =
              " for test " +
              JSON.stringify({
                autoincrement,
                keypath,
                method,
                explicit: explicit === undefined ? "undefined" : explicit,
                existing,
              });

            // Insert "existing" data if needed
            if (existing) {
              if (keypath) {
                store.add({
                  existing: "data",
                  id: 5,
                }).onsuccess = grabEventAndContinueHandler;
              } else {
                store.add({ existing: "data" }, 5).onsuccess =
                  grabEventAndContinueHandler;
              }

              let e = yield undefined;
              is(e.type, "success", "success inserting existing" + test);
              is(e.target.result, 5, "inserted correct key" + test);
            }

            // Set up value to be inserted
            let value = { theObj: true };
            if (keypath === true) {
              value.id = 5;
            } else if (keypath === "invalid") {
              value.id = /x/;
            }

            // Which arguments are passed to function
            let args = [value];
            if (explicit === true) {
              args.push(5);
            } else if (explicit === undefined) {
              args.push(undefined);
            } else if (explicit === "invalid") {
              args.push(/x/);
            }

            let expected = expectedResult(
              method,
              keypath,
              explicit,
              autoincrement,
              existing
            );

            let valueJSON = JSON.stringify(value);

            ok(true, "making call" + test);

            // Make function call for throwing functions
            if (expected === "throw") {
              try {
                store[method].apply(store, args);
                ok(false, "should have thrown" + test);
              } catch (ex) {
                ok(true, "did throw" + test);
                ok(ex instanceof DOMException, "Got a DOMException" + test);
                is(ex.name, "DataError", "expect a DataError" + test);
                is(ex.code, 0, "expect zero" + test);
                is(
                  JSON.stringify(value),
                  valueJSON,
                  "call didn't modify value" + test
                );
              }
              continue;
            }

            // Make non-throwing function call
            let req = store[method].apply(store, args);
            is(
              JSON.stringify(value),
              valueJSON,
              "call didn't modify value" + test
            );

            req.onsuccess = req.onerror = grabEventAndContinueHandler;
            let e = yield undefined;

            // Figure out what key we used
            let key = 5;
            if (autoincrement && speccedNoKey) {
              key = 1;
            }

            // Adjust value if expected
            if (autoincrement && keypath && speccedNoKey) {
              value.id = key;
            }

            // Check result
            if (expected === "error") {
              is(e.type, "error", "write should fail" + test);
              e.preventDefault();
              e.stopPropagation();
              continue;
            }

            is(e.type, "success", "write should succeed" + test);
            is(e.target.result, key, "write should return correct key" + test);

            store.get(key).onsuccess = grabEventAndContinueHandler;
            e = yield undefined;
            is(e.type, "success", "read back should succeed" + test);
            is(
              JSON.stringify(e.target.result),
              JSON.stringify(value),
              "read back should return correct value" + test
            );
          }
        }
      }
    }
  }

  function expectedResult(method, keypath, explicit, autoincrement, existing) {
    if (keypath && explicit) {
      return "throw";
    }
    if (!keypath && !explicit && !autoincrement) {
      return "throw";
    }
    if (keypath == "invalid") {
      return "throw";
    }
    if (keypath == "missing" && !autoincrement) {
      return "throw";
    }
    if (explicit == "invalid") {
      return "throw";
    }

    if (method == "add" && existing) {
      return "error";
    }

    return "success";
  }

  openRequest.onsuccess = grabEventAndContinueHandler;
  yield undefined;

  finishTest();
}