summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/stores/integration_spec.js
blob: f95f036f5723f49a733470855ae54535321122b6 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { decorateFiles } from '~/ide/lib/files';
import { createStore } from '~/ide/stores';

const TEST_BRANCH = 'test_branch';
const TEST_NAMESPACE = 'test_namespace';
const TEST_PROJECT_ID = `${TEST_NAMESPACE}/test_project`;
const TEST_PATH_DIR = 'src';
const TEST_PATH = `${TEST_PATH_DIR}/foo.js`;
const TEST_CONTENT = `Lorem ipsum dolar sit
Lorem ipsum dolar
Lorem ipsum
Lorem
`;

jest.mock('~/ide/ide_router');

describe('IDE store integration', () => {
  let store;

  beforeEach(() => {
    store = createStore();
    store.replaceState({
      ...store.state,
      projects: {
        [TEST_PROJECT_ID]: {
          web_url: 'test_web_url',
          branches: [],
        },
      },
      currentProjectId: TEST_PROJECT_ID,
      currentBranchId: TEST_BRANCH,
    });
  });

  describe('with project and files', () => {
    beforeEach(() => {
      const { entries, treeList } = decorateFiles({
        data: [`${TEST_PATH_DIR}/`, TEST_PATH, 'README.md'],
        projectId: TEST_PROJECT_ID,
        branchId: TEST_BRANCH,
      });

      Object.assign(entries[TEST_PATH], {
        raw: TEST_CONTENT,
      });

      store.replaceState({
        ...store.state,
        trees: {
          [`${TEST_PROJECT_ID}/${TEST_BRANCH}`]: {
            tree: treeList,
          },
        },
        entries,
      });
    });

    describe('when a file is deleted and readded', () => {
      beforeEach(() => {
        store.dispatch('deleteEntry', TEST_PATH);
        store.dispatch('createTempEntry', { name: TEST_PATH, type: 'blob' });
      });

      it('is added to staged as modified', () => {
        expect(store.state.stagedFiles).toEqual([
          expect.objectContaining({
            path: TEST_PATH,
            deleted: false,
            staged: true,
            changed: true,
            tempFile: false,
          }),
        ]);
      });

      it('cleans up after commit', () => {
        const expected = expect.objectContaining({
          path: TEST_PATH,
          staged: false,
          changed: false,
          tempFile: false,
          deleted: false,
        });
        store.dispatch('stageChange', TEST_PATH);

        store.dispatch('commit/updateFilesAfterCommit', { data: {} });

        expect(store.state.entries[TEST_PATH]).toEqual(expected);
        expect(store.state.entries[TEST_PATH_DIR].tree.find(x => x.path === TEST_PATH)).toEqual(
          expected,
        );
      });
    });
  });
});