2018-12-19 17:50:39 +03:00
|
|
|
import map2tree from '../src';
|
|
|
|
import immutable from 'immutable';
|
|
|
|
|
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
|
|
|
|
2019-01-08 19:35:12 +03:00
|
|
|
expect(map2tree(map, options).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 = {
|
|
|
|
a: null
|
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +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',
|
|
|
|
b: 'bar'
|
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +03:00
|
|
|
children: [{ name: 'a', value: 'foo' }, { 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 = {
|
2019-01-10 21:51:14 +03:00
|
|
|
a: { aa: 'foo' }
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
2019-01-10 21:51:14 +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 = {
|
2019-01-10 21:51:14 +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',
|
|
|
|
children: [{ name: 'aa', value: 'foo' }, { 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 = {
|
2019-01-10 21:51:14 +03:00
|
|
|
a: { aa: null }
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
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 = {
|
2019-01-10 21:51:14 +03:00
|
|
|
a: { aa: { aaa: 'foo' } }
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'aa',
|
2019-01-10 21:51:14 +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 = {
|
2019-01-10 21:51:14 +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',
|
|
|
|
children: [{ name: 'a[0]', value: 1 }, { 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', () => {
|
2019-01-10 21:51:14 +03:00
|
|
|
const options = { pushMethod: 'unshift' };
|
2018-12-19 17:50:39 +03:00
|
|
|
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 }]
|
|
|
|
}
|
|
|
|
]
|
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 = {
|
2019-01-10 21:51:14 +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',
|
|
|
|
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 = {
|
2019-01-10 21:51:14 +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 } },
|
|
|
|
{ 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 = {
|
2019-01-10 21:51:14 +03:00
|
|
|
a: [{ aa: { aaa: 'foo' } }]
|
2018-12-19 17:50:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'state',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'a',
|
2019-01-10 21:51:14 +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
|
|
|
});
|