2019-03-07 04:22:05 -05:00
|
|
|
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
|
2019-04-03 07:38:46 -04:00
|
|
|
import { decorateFiles, splitParent, escapeFileUrl } from '~/ide/lib/files';
|
2019-03-07 04:22:05 -05:00
|
|
|
import { decorateData } from '~/ide/stores/utils';
|
|
|
|
|
|
|
|
const TEST_BRANCH_ID = 'lorem-ipsum';
|
|
|
|
const TEST_PROJECT_ID = 10;
|
|
|
|
|
|
|
|
const createEntries = paths => {
|
|
|
|
const createEntry = (acc, { path, type, children }) => {
|
|
|
|
// Sometimes we need to end the url with a '/'
|
|
|
|
const createUrl = base => (type === 'tree' ? `${base}/` : base);
|
|
|
|
|
|
|
|
const { name, parent } = splitParent(path);
|
|
|
|
const parentEntry = acc[parent];
|
2019-07-03 04:19:32 -04:00
|
|
|
const previewMode = viewerInformationForPath(name);
|
2019-03-07 04:22:05 -05:00
|
|
|
|
|
|
|
acc[path] = {
|
|
|
|
...decorateData({
|
|
|
|
projectId: TEST_PROJECT_ID,
|
|
|
|
branchId: TEST_BRANCH_ID,
|
|
|
|
id: path,
|
|
|
|
name,
|
|
|
|
path,
|
2019-04-03 07:38:46 -04:00
|
|
|
url: createUrl(`/${TEST_PROJECT_ID}/${type}/${TEST_BRANCH_ID}/-/${escapeFileUrl(path)}`),
|
2019-03-07 04:22:05 -05:00
|
|
|
type,
|
2019-07-03 04:19:32 -04:00
|
|
|
previewMode,
|
|
|
|
binary: (previewMode && previewMode.binary) || false,
|
2019-03-07 04:22:05 -05:00
|
|
|
parentPath: parent,
|
|
|
|
parentTreeUrl: parentEntry
|
|
|
|
? parentEntry.url
|
|
|
|
: createUrl(`/${TEST_PROJECT_ID}/${type}/${TEST_BRANCH_ID}`),
|
|
|
|
}),
|
2019-04-03 07:38:46 -04:00
|
|
|
tree: children.map(childName => expect.objectContaining({ name: childName })),
|
2019-03-07 04:22:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
};
|
|
|
|
|
|
|
|
const entries = paths.reduce(createEntry, {});
|
|
|
|
|
2019-04-03 07:38:46 -04:00
|
|
|
// Wrap entries in expect.objectContaining.
|
2019-03-07 04:22:05 -05:00
|
|
|
// We couldn't do this earlier because we still need to select properties from parent entries.
|
|
|
|
return Object.keys(entries).reduce((acc, key) => {
|
2019-04-03 07:38:46 -04:00
|
|
|
acc[key] = expect.objectContaining(entries[key]);
|
2019-03-07 04:22:05 -05:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('IDE lib decorate files', () => {
|
|
|
|
it('creates entries and treeList', () => {
|
2019-04-03 07:38:46 -04:00
|
|
|
const data = ['app/assets/apples/foo.js', 'app/bugs.js', 'app/#weird#file?.txt', 'README.md'];
|
2019-03-07 04:22:05 -05:00
|
|
|
const expectedEntries = createEntries([
|
2019-04-03 07:38:46 -04:00
|
|
|
{ path: 'app', type: 'tree', children: ['assets', '#weird#file?.txt', 'bugs.js'] },
|
2019-03-07 04:22:05 -05:00
|
|
|
{ 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: [] },
|
2019-04-03 07:38:46 -04:00
|
|
|
{ path: 'app/#weird#file?.txt', type: 'blob', children: [] },
|
2019-03-07 04:22:05 -05:00
|
|
|
{ path: 'README.md', type: 'blob', children: [] },
|
|
|
|
]);
|
|
|
|
|
|
|
|
const { entries, treeList } = decorateFiles({
|
|
|
|
data,
|
|
|
|
branchId: TEST_BRANCH_ID,
|
|
|
|
projectId: TEST_PROJECT_ID,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Here we test the keys and then each key/value individually because `expect(entries).toEqual(expectedEntries)`
|
2019-04-03 07:38:46 -04:00
|
|
|
// was taking a very long time for some reason. Probably due to large objects and nested `expect.objectContaining`.
|
2019-03-07 04:22:05 -05:00
|
|
|
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']]);
|
|
|
|
});
|
|
|
|
});
|