File: esnext.map.update-or-insert.js

package info (click to toggle)
node-core-js 3.33.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,828 kB
  • sloc: javascript: 87,204; makefile: 13
file content (36 lines) | stat: -rw-r--r-- 1,654 bytes parent folder | download | duplicates (2)
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
QUnit.test('Map#updateOrInsert', assert => {
  const { updateOrInsert } = Map.prototype;
  assert.isFunction(updateOrInsert);
  assert.arity(updateOrInsert, 2);
  assert.looksNative(updateOrInsert);
  assert.nonEnumerable(Map.prototype, 'updateOrInsert');

  const map = new Map([['a', 2]]);
  assert.same(map.updateOrInsert('a', function (value) {
    assert.same(arguments.length, 1, 'correct number of callback arguments');
    assert.same(value, 2, 'correct value in callback');
    return value ** 2;
  }, () => {
    assert.avoid();
    return 3;
  }), 4, 'returns a correct value');
  assert.same(map.updateOrInsert('b', value => {
    assert.avoid();
    return value ** 2;
  }, function () {
    assert.same(arguments.length, 0, 'correct number of callback arguments');
    return 3;
  }), 3, 'returns a correct value');
  assert.same(map.size, 2, 'correct size');
  assert.same(map.get('a'), 4, 'correct result #1');
  assert.same(map.get('b'), 3, 'correct result #2');

  assert.same(new Map([['a', 2]]).updateOrInsert('b', null, () => 3), 3);
  assert.same(new Map([['a', 2]]).updateOrInsert('a', value => value ** 2), 4);

  assert.throws(() => new Map().updateOrInsert('a'), TypeError);
  assert.throws(() => updateOrInsert.call({}, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError);
  assert.throws(() => updateOrInsert.call([], 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError);
  assert.throws(() => updateOrInsert.call(undefined, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError);
  assert.throws(() => updateOrInsert.call(null, 'a', () => { /* empty */ }, () => { /* empty */ }), TypeError);
});