testing/web-platform/tests/IndexedDB/key_valid.any.js
author Duncan McIntosh <dmcintosh@mozilla.com>
Wed, 09 Jul 2025 19:42:02 +0000 (3 hours ago)
changeset 795924 9ccc6a2267cbf69c621fec973bd28573c2a45a1f
parent 782087 bfe62da1d6d3bdc9979ac66fa627339db3cd558b
permissions -rw-r--r--
Bug 1966586 - Reuse other browser windows when opening _blank links in Taskbar Tabs windows. r=nrishel This doesn't affect other tab additions, nor does it stop the tab bar from appearing altogether. The idea is that _if_ another tab is somehow made, the user should see it; but we should not create new tabs if we can avoid it. This also adds tests for opening URIs in popups and taskbar tabs to make it less likely that this breaks in future. Differential Revision: https://phabricator.services.mozilla.com/D253726
// META: global=window,worker
// META: title=Valid key
// META: script=resources/support.js

// Spec: https://w3c.github.io/IndexedDB/#key-construct

'use strict';

const valid_key = (desc, key) => {
  async_test(t => {
    let db;
    const open_rq = createdb(t);
    open_rq.onupgradeneeded = t.step_func(e => {
      db = e.target.result;
      const store = db.createObjectStore('store');
      assert_true(store.add('value', key) instanceof IDBRequest);

      const store2 = db.createObjectStore('store2', {
        keyPath: ['x', 'keypath'],
      });
      assert_true(store2.add({x: 'v', keypath: key}) instanceof IDBRequest);
    });

    open_rq.onsuccess = t.step_func(e => {
      const rq =
          db.transaction('store', 'readonly').objectStore('store').get(key);
      rq.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 'value');
        const rq2 =
            db.transaction('store2', 'readonly').objectStore('store2').get([
              'v', key
            ]);
        rq2.onsuccess = t.step_func(e => {
          assert_equals(e.target.result.x, 'v');
          assert_key_equals(e.target.result.keypath, key);
          t.done();
        });
      });
    });
  }, 'Valid key - ' + desc);
};

// Date
valid_key('new Date()', new Date());
valid_key('new Date(0)', new Date(0));

// Array
valid_key('[]', []);
valid_key('new Array()', new Array());

valid_key('["undefined"]', ['undefined']);

// Float
valid_key('Infinity', Infinity);
valid_key('-Infinity', -Infinity);
valid_key('0', 0);
valid_key('1.5', 1.5);
valid_key('3e38', 3e38);
valid_key('3e-38', 3e38);

// String
valid_key('"foo"', 'foo');
valid_key('"\\n"', '\n');
valid_key('""', '');
valid_key('"\\""', '"');
valid_key('"\\u1234"', '\u1234');
valid_key('"\\u0000"', '\u0000');
valid_key('"NaN"', 'NaN');