redux-devtools/packages/map2tree/test/map2tree.spec.ts

234 lines
4.6 KiB
TypeScript
Raw Normal View History

import map2tree, { Node } from '../src';
import * as immutable from 'immutable';
2019-01-08 19:35:12 +03:00
test('# rootNodeKey', () => {
const map = {};
2019-01-10 21:51:14 +03:00
const options = { key: 'foo' };
expect((map2tree(map, options) as Node).name).toBe('foo');
});
2019-01-08 19:35:12 +03:00
describe('# shallow map', () => {
test('## null', () => {
const map = {
a: null,
};
const expected = {
name: 'state',
children: [{ name: 'a', value: null }],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## value', () => {
const map = {
a: 'foo',
b: 'bar',
};
const expected = {
name: 'state',
children: [
{ name: 'a', value: 'foo' },
{ name: 'b', value: 'bar' },
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## object', () => {
const map = {
a: { aa: 'foo' },
};
const expected = {
name: 'state',
children: [{ name: 'a', children: [{ name: 'aa', value: 'foo' }] }],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## immutable Map', () => {
const map = {
a: immutable.fromJS({ aa: 'foo', ab: 'bar' }),
};
const expected = {
name: 'state',
children: [
2019-01-10 21:51:14 +03:00
{
name: 'a',
children: [
{ name: 'aa', value: 'foo' },
{ name: 'ab', value: 'bar' },
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
2019-01-10 21:51:14 +03:00
});
});
2019-01-08 19:35:12 +03:00
describe('# deep map', () => {
test('## null', () => {
const map = {
a: { aa: null },
};
const expected = {
name: 'state',
children: [
{
name: 'a',
children: [
{
name: 'aa',
value: null,
},
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## object', () => {
const map = {
a: { aa: { aaa: 'foo' } },
};
const expected = {
name: 'state',
children: [
{
name: 'a',
children: [
{
name: 'aa',
children: [{ name: 'aaa', value: 'foo' }],
},
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
});
2019-01-08 19:35:12 +03:00
describe('# array map', () => {
const map = {
a: [1, 2],
};
2019-01-08 19:35:12 +03:00
test('## push', () => {
const expected = {
name: 'state',
2019-01-10 21:51:14 +03:00
children: [
{
name: 'a',
children: [
{ name: 'a[0]', value: 1 },
{ name: 'a[1]', value: 2 },
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## unshift', () => {
const options = { pushMethod: 'unshift' as const };
const expected = {
name: 'state',
2019-01-10 21:51:14 +03:00
children: [
{
name: 'a',
children: [
{ name: 'a[1]', value: 2 },
{ name: 'a[0]', value: 1 },
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map, options)).toEqual(expected);
expect(map2tree(immutable.fromJS(map), options)).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## null', () => {
const map = {
a: [null],
};
const expected = {
name: 'state',
2019-01-10 21:51:14 +03:00
children: [
{
name: 'a',
children: [{ name: 'a[0]', value: null }],
},
],
};
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
});
});
2019-01-08 19:35:12 +03:00
describe('# collection map', () => {
test('## value', () => {
const map = {
a: [{ aa: 1 }, { aa: 2 }],
};
const expected = {
name: 'state',
children: [
{
name: 'a',
children: [
2019-01-10 21:51:14 +03:00
{ name: 'a[0]', object: { aa: 1 } },
{ name: 'a[1]', object: { aa: 2 } },
],
},
],
};
2019-01-08 19:35:12 +03:00
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
2019-01-08 19:35:12 +03:00
test('## object', () => {
const map = {
a: [{ aa: { aaa: 'foo' } }],
};
const expected = {
name: 'state',
children: [
{
name: 'a',
children: [{ name: 'a[0]', object: { aa: { aaa: 'foo' } } }],
},
],
};
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
});
});