2019-12-21 20:55:43 +05:30
|
|
|
import { decorateFiles, splitParent } from '~/ide/lib/files';
|
2020-01-01 13:55:28 +05:30
|
|
|
import { decorateData } from '~/ide/stores/utils';
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
const createEntries = paths => {
|
|
|
|
const createEntry = (acc, { path, type, children }) => {
|
|
|
|
const { name, parent } = splitParent(path);
|
|
|
|
|
|
|
|
acc[path] = {
|
|
|
|
...decorateData({
|
|
|
|
id: path,
|
|
|
|
name,
|
|
|
|
path,
|
|
|
|
type,
|
|
|
|
parentPath: parent,
|
|
|
|
}),
|
|
|
|
tree: children.map(childName => expect.objectContaining({ name: childName })),
|
|
|
|
};
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
};
|
|
|
|
|
|
|
|
const entries = paths.reduce(createEntry, {});
|
|
|
|
|
|
|
|
// Wrap entries in expect.objectContaining.
|
|
|
|
// We couldn't do this earlier because we still need to select properties from parent entries.
|
|
|
|
return Object.keys(entries).reduce((acc, key) => {
|
|
|
|
acc[key] = expect.objectContaining(entries[key]);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('IDE lib decorate files', () => {
|
|
|
|
it('creates entries and treeList', () => {
|
|
|
|
const data = ['app/assets/apples/foo.js', 'app/bugs.js', 'app/#weird#file?.txt', 'README.md'];
|
|
|
|
const expectedEntries = createEntries([
|
|
|
|
{ path: 'app', type: 'tree', children: ['assets', '#weird#file?.txt', 'bugs.js'] },
|
|
|
|
{ path: 'app/assets', type: 'tree', children: ['apples'] },
|
|
|
|
{ path: 'app/assets/apples', type: 'tree', children: ['foo.js'] },
|
|
|
|
{ path: 'app/assets/apples/foo.js', type: 'blob', children: [] },
|
|
|
|
{ path: 'app/bugs.js', type: 'blob', children: [] },
|
|
|
|
{ path: 'app/#weird#file?.txt', type: 'blob', children: [] },
|
|
|
|
{ path: 'README.md', type: 'blob', children: [] },
|
|
|
|
]);
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const { entries, treeList } = decorateFiles({ data });
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
// Here we test the keys and then each key/value individually because `expect(entries).toEqual(expectedEntries)`
|
|
|
|
// was taking a very long time for some reason. Probably due to large objects and nested `expect.objectContaining`.
|
|
|
|
const entryKeys = Object.keys(entries);
|
|
|
|
|
|
|
|
expect(entryKeys).toEqual(Object.keys(expectedEntries));
|
|
|
|
entryKeys.forEach(key => {
|
|
|
|
expect(entries[key]).toEqual(expectedEntries[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(treeList).toEqual([expectedEntries.app, expectedEntries['README.md']]);
|
|
|
|
});
|
|
|
|
});
|