Files
teleport/web/packages/shared/utils/highbar.test.ts
T
Jeff Pihach 5eafe86fa4 Remove usage of lodash methods (#21567)
* Create new local util lib to replace lodash.

* Replace usage of isInteger and debounce from lodash with highbar.

* Create isObject and runOnce utility methods.

* remove use of at, isObject, and once lodash method usage.

* remove map and transform lodash calls.

* Add memoize function to highbar.

* remove memoize lodash usage.

* remove merge and isEqual lodash methods and update other missing refs to highbar.

* convert the throttle to debounce.

* add throttle method to highbar.

* use the new throttle method instead of debounce where necessary.

* Add mergeDeep function for init config merge.

* remove lodash from the build process.

* Fix introduced bug in workspacesService.

* Added tests for highbar mergeDeep and expanded its functionality to support arrays.

* review updates.

* Added types to mergeDeep function.

* Add missing MapCache prototype methods.

* Add license notices, types and missing hash code.

* First pass at compare an array objects function.

* use new compareArrayObjs fn

* Add missing not

* Added types to arrayObjectIsEqual

* Add tests for arrayObjectIsEqual and fix some edge case bugs.

* update util fn name
2023-02-21 17:25:36 +00:00

222 lines
5.2 KiB
TypeScript

/**
* Copyright 2023 Gravitational, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { arrayObjectIsEqual, mergeDeep } from './highbar';
describe('mergeDeep can merge two', () => {
it('objects together', () => {
const a = { a: 1, b: 2, c: 3, e: 5 };
const b = { a: 3, b: 2, c: 1, d: 4 };
expect(mergeDeep(a, b)).toStrictEqual({
a: 3,
b: 2,
c: 1,
d: 4,
e: 5,
});
});
it('nested objects together', () => {
const a = { a: 1, b: 2, c: { d: 3, e: 6, g: 8 } };
const b = { a: 1, b: 2, c: { d: 4, e: 6, f: 7 } };
expect(mergeDeep(a, b)).toStrictEqual({
a: 1,
b: 2,
c: { d: 4, e: 6, f: 7, g: 8 },
});
});
it('objects together that contain arrays', () => {
const a = { a: 1, b: ['a', 'b', 'd'] };
const b = { a: 2, b: ['b', 'c'] };
expect(mergeDeep(a, b)).toStrictEqual({
a: 2,
b: ['b', 'c', 'd'],
});
const c = { a: 1, b: ['b', 'c'] };
const d = { a: 2, b: ['a', 'b', 'd'] };
expect(mergeDeep(c, d)).toStrictEqual({
a: 2,
b: ['a', 'b', 'd'],
});
});
it('objects together that contain arrays of arrays', () => {
const a = { a: [['b', 'c', 'f']] };
const b = { a: [['d', 'e']] };
expect(mergeDeep(a, b)).toStrictEqual({
a: [['d', 'e', 'f']],
});
const c = { a: [['d', 'e']] };
const d = { a: [['b', 'c', 'f']] };
expect(mergeDeep(c, d)).toStrictEqual({
a: [['b', 'c', 'f']],
});
});
it('objects together that contain arrays that contain objects', () => {
const a = { a: 1, b: [{ c: 3, d: 4, e: 5 }, 'b'] };
const b = { a: 2, b: [{ c: 3, d: 4, f: 6 }, 'c'] };
expect(mergeDeep(a, b)).toStrictEqual({
a: 2,
b: [{ c: 3, d: 4, e: 5, f: 6 }, 'c'],
});
});
it('objects with arrays with undefined indexes', () => {
const a = {
a: false,
b: {
c: 'foo',
d: 'bar',
},
c: {
a: 'no',
b: [],
},
};
const b = {
a: true,
b: {
d: 'baz',
e: 'bax',
},
c: {
a: 'ok',
b: [
{
a: 'foo',
b: 'bar',
},
],
},
};
expect(mergeDeep(a, b)).toStrictEqual({
a: true,
b: {
c: 'foo',
d: 'baz',
e: 'bax',
},
c: {
a: 'ok',
b: [
{
a: 'foo',
b: 'bar',
},
],
},
});
});
});
describe('arrayObjectIsEqual correctly compares', () => {
it('simple arrays', () => {
const a = [{ foo: 'bar' }];
const b = [{ foo: 'bar' }];
expect(arrayObjectIsEqual(a, b)).toBe(true);
const c = [{ foo: 'bar' }];
const d = [{ foo: 'baz' }];
expect(arrayObjectIsEqual(c, d)).toBe(false);
});
it('arrays with complex objects', () => {
const a = [
{
'/clusters/test-uri': {
accessRequests: {
pending: {
app: {},
db: {},
kube_cluster: {},
node: {},
role: {},
windows_desktop: {},
},
isBarCollapsed: false,
},
localClusterUri: '/clusters/test-uri',
documents: [
{
kind: 'doc.cluster',
title: 'Cluster Test',
clusterUri: '/clusters/test-uri',
uri: '/docs/test-cluster-uri',
},
],
location: '/docs/test-cluster-uri',
previous: {
documents: [
{
kind: 'doc.terminal_shell',
uri: '/docs/some_uri',
title: '/Users/alice/Documents',
},
],
location: '/docs/some_uri',
},
},
},
];
const b = [
{
'/clusters/test-uri': {
accessRequests: {
pending: {
app: {},
db: {},
kube_cluster: {},
node: {},
role: {},
windows_desktop: {},
},
isBarCollapsed: false,
},
localClusterUri: '/clusters/test-uri',
documents: [
{
kind: 'doc.cluster',
title: 'Cluster Test',
clusterUri: '/clusters/test-uri',
uri: '/docs/test-cluster-uri',
},
],
location: '/docs/test-cluster-uri',
previous: {
documents: [
{
kind: 'doc.terminal_shell',
uri: '/docs/some_uri',
title: '/Users/alice/Documents',
},
],
location: '/docs/some_uri',
},
},
},
];
expect(arrayObjectIsEqual(a, b)).toBe(true);
});
});