Skip to content

Commit

Permalink
СB-5198 adds unit tests fro TempMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyteleshev committed May 30, 2024
1 parent d5a8abe commit 1c9e054
Showing 1 changed file with 282 additions and 0 deletions.
282 changes: 282 additions & 0 deletions webapp/packages/core-utils/src/TempMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { TempMap } from './TempMap';

describe('TempMap', () => {
it('should create empty map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

expect(map.size).toBe(0);
expect(target.size).toBe(0);
});

it('should store values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

expect(map.get('test')).toBe('test value');
expect(map.get('test2')).toBe('test value2');
});

it('should delete values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.delete('test');

expect(map.get('test')).toBeUndefined();
expect(map.isDeleted('test')).toBe(true);
});

it('should clear values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');
map.clear();

expect(map.get('test')).toBeUndefined();
expect(map.get('test2')).toBeUndefined();
});

it('should also clear deleted values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.delete('test');
expect(map.isDeleted('test')).toBe(true);
map.clear();

expect(map.get('test')).toBeUndefined();
expect(map.isDeleted('test')).toBe(false);
});

it('should remove from deleted if set again', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.delete('test');

expect(map.isDeleted('test')).toBe(true);

map.set('test', 'test value2');

expect(map.get('test')).toBe('test value2');
expect(map.isDeleted('test')).toBe(false);
});

it('should for each values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

const values: string[] = [];
const keys: string[] = [];
map.forEach((value, key) => {
values.push(value);
keys.push(key);
});

expect(values).toEqual(['test value', 'test value2']);
expect(keys).toEqual(['test', 'test2']);
});

it('should for each and miss deleted', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');
map.delete('test');

const values: string[] = [];
const keys: string[] = [];
map.forEach((value, key) => {
values.push(value);
keys.push(key);
});

expect(values).toEqual(['test value2']);
expect(keys).toEqual(['test2']);
expect(map.isDeleted('test')).toBe(true);
});

it('should check if key exists', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');
map.delete('test');

expect(map.has('test')).toBe(false);
expect(map.has('test2')).toBe(true);
});

it('should get keys', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

expect(Array.from(map.keys())).toEqual(['test', 'test2']);
});

it('should get values', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

expect(Array.from(map.values())).toEqual(['test value', 'test value2']);
});

it('should get entries', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

expect(Array.from(map.entries())).toEqual([
['test', 'test value'],
['test2', 'test value2'],
]);
});

it('should get size', () => {
const map = new TempMap<string, string>(new Map());

map.set('test', 'test value');
map.set('test2', 'test value2');

expect(map.size).toBe(2);
});

it('should only delete target value', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

map.set('test', 'test value');
target.delete('test');

expect(map.get('test')).toBe('test value');
expect(target.get('test')).toBeUndefined();
});

it('should set target value and also be available in temp map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');

expect(map.get('test')).toBe('test value2');
expect(target.get('test')).toBe('test value2');
});

it('should combine keys from target and temp map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');
map.set('test2', 'test value');

expect(Array.from(map.keys())).toEqual(['test', 'test2']);
});

it('should combine entries from target and temp map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');
map.set('test2', 'test value');

expect(Array.from(map.entries())).toEqual([
['test', 'test value2'],
['test2', 'test value'],
]);
});

it('should combine values from target and temp map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');
map.set('test2', 'test value');

expect(Array.from(map.values())).toEqual(['test value2', 'test value']);
});

it('should has target value in temp map', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');

expect(map.has('test')).toBe(true);
});

it('should be iterable', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

target.set('test', 'test value2');
map.set('test2', 'test value');

const values: string[] = [];
const keys: string[] = [];
for (const [key, value] of map) {
values.push(value);
keys.push(key);
}

expect(values).toEqual(['test value2', 'test value']);
expect(keys).toEqual(['test', 'test2']);
});

it('should not get deleted keys', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

map.set('test', 'test value2');
map.set('test2', 'test value');
map.delete('test2');

expect(Array.from(map.keys())).toEqual(['test']);
});

it('should not get deleted values', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

map.set('test', 'test value2');
map.set('test2', 'test value');
map.delete('test2');

expect(Array.from(map.values())).toEqual(['test value2']);
});

it('should not get deleted entries', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

map.set('test', 'test value2');
map.set('test2', 'test value');
map.delete('test2');

expect(Array.from(map.entries())).toEqual([['test', 'test value2']]);
});

it('should has toStringTag', () => {
const target = new Map();
const map = new TempMap<string, string>(target);

expect(map[Symbol.toStringTag]).toBe('TempMap');
});
});

0 comments on commit 1c9e054

Please sign in to comment.