Welcome to mirror list, hosted at ThFree Co, Russian Federation.

files_spec.js « lib « ide « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50738af0e33f7b22977a8003c6d5f55385126be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { decorateFiles, splitParent } from '~/ide/lib/files';
import { decorateData } from '~/ide/stores/utils';

const TEST_BLOB_DATA = { mimeType: 'test/mime' };

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 })),
      ...(type === 'blob' ? TEST_BLOB_DATA : {}),
    };

    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: [] },
    ]);

    const { entries, treeList } = decorateFiles({ data, blobData: TEST_BLOB_DATA });

    // 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']]);
  });
});