2020-09-21 02:05:37 +03:00
|
|
|
import map2tree, { Node } from '../src';
|
2020-09-20 02:56:40 +03:00
|
|
|
import * as immutable from 'immutable';
|
2018-12-19 17:50:39 +03:00
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('# rootNodeKey', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {};
|
2019-01-10 21:51:14 +03:00
|
|
|
const options = { key: 'foo' };
|
2018-12-19 17:50:39 +03:00
|
|
|
|
2020-09-21 02:05:37 +03:00
|
|
|
expect((map2tree(map, options) as Node).name).toBe('foo');
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
describe('# shallow map', () => {
|
|
|
|
test('## null', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: null,
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2020-08-08 23:26:39 +03:00
|
|
|
children: [{ name: 'a', value: null }],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## value', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
|
|
|
a: 'foo',
|
2020-08-08 23:26:39 +03:00
|
|
|
b: 'bar',
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2020-08-05 16:12:31 +03:00
|
|
|
children: [
|
|
|
|
{ name: 'a', value: 'foo' },
|
2020-08-08 23:26:39 +03:00
|
|
|
{ name: 'b', value: 'bar' },
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## object', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: { aa: 'foo' },
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2020-08-08 23:26:39 +03:00
|
|
|
children: [{ name: 'a', children: [{ name: 'aa', value: 'foo' }] }],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## immutable Map', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: immutable.fromJS({ aa: 'foo', ab: 'bar' }),
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
2019-01-10 21:51:14 +03:00
|
|
|
{
|
|
|
|
name: 'a',
|
2020-08-05 16:12:31 +03:00
|
|
|
children: [
|
|
|
|
{ name: 'aa', value: 'foo' },
|
2020-08-08 23:26:39 +03:00
|
|
|
{ name: 'ab', value: 'bar' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
2019-01-10 21:51:14 +03:00
|
|
|
});
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
describe('# deep map', () => {
|
|
|
|
test('## null', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: { aa: null },
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'aa',
|
2020-08-08 23:26:39 +03:00
|
|
|
value: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## object', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: { aa: { aaa: 'foo' } },
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'aa',
|
2020-08-08 23:26:39 +03:00
|
|
|
children: [{ name: 'aaa', value: 'foo' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
describe('# array map', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: [1, 2],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## push', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +03:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
2020-08-05 16:12:31 +03:00
|
|
|
children: [
|
|
|
|
{ name: 'a[0]', value: 1 },
|
2020-08-08 23:26:39 +03:00
|
|
|
{ name: 'a[1]', value: 2 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## unshift', () => {
|
2020-09-20 02:56:40 +03:00
|
|
|
const options = { pushMethod: 'unshift' as const };
|
2018-12-19 17:50:39 +03:00
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +03:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
2020-08-05 16:12:31 +03:00
|
|
|
children: [
|
|
|
|
{ name: 'a[1]', value: 2 },
|
2020-08-08 23:26:39 +03:00
|
|
|
{ name: 'a[0]', value: 1 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map, options)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map), options)).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## null', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: [null],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +03:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
2020-08-08 23:26:39 +03:00
|
|
|
children: [{ name: 'a[0]', value: null }],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2019-01-10 21:51:14 +03:00
|
|
|
});
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
describe('# collection map', () => {
|
|
|
|
test('## value', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: [{ aa: 1 }, { aa: 2 }],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
|
|
|
children: [
|
2019-01-10 21:51:14 +03:00
|
|
|
{ name: 'a[0]', object: { aa: 1 } },
|
2020-08-08 23:26:39 +03:00
|
|
|
{ name: 'a[1]', object: { aa: 2 } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
test('## object', () => {
|
2018-12-19 17:50:39 +03:00
|
|
|
const map = {
|
2020-08-08 23:26:39 +03:00
|
|
|
a: [{ aa: { aaa: 'foo' } }],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
2020-08-08 23:26:39 +03:00
|
|
|
children: [{ name: 'a[0]', object: { aa: { aaa: 'foo' } } }],
|
|
|
|
},
|
|
|
|
],
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map)).toEqual(expected);
|
|
|
|
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
|
2019-01-10 21:51:14 +03:00
|
|
|
});
|
2018-12-19 17:50:39 +03:00
|
|
|
});
|