Testing DefaultMap Custom Collection Implementation in Parcel
This test suite validates the DefaultMap utility class implementation, which extends JavaScript’s Map functionality with default value generation. The tests ensure proper initialization, default value handling, and edge cases for map operations.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
parcel-bundler/parcel
packages/core/utils/test/DefaultMap.test.js
// @flow strict-local
import assert from 'assert';
import {DefaultMap} from '../src/DefaultMap';
describe('DefaultMap', () => {
it('constructs with entries just like Map', () => {
let map = new DefaultMap(
k => k,
[
[1, 3],
[2, 27],
],
);
assert.equal(map.get(1), 3);
assert.deepEqual(Array.from(map.entries()), [
[1, 3],
[2, 27],
]);
});
it("returns a default value based on a key if it doesn't exist", () => {
let map = new DefaultMap(k => k);
assert.equal(map.get(3), 3);
});
it("sets a default value based on a key if it doesn't exist", () => {
let map = new DefaultMap(k => k);
map.get(3);
assert.deepEqual(Array.from(map.entries()), [[3, 3]]);
});
it('respects undefined/null if it already existed in the map', () => {
let map = new DefaultMap<number, number | void | null>(k => k);
map.set(3, undefined);
assert.equal(map.get(3), undefined);
map.set(4, null);
assert.equal(map.get(4), null);
});
});