summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/import_projects/store/getters_spec.js
blob: fced5670f25b3edbc202242e53662efa998cbc8d (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { STATUSES } from '~/import_entities/constants';
import {
  isImportingAnyRepo,
  hasIncompatibleRepos,
  hasImportableRepos,
  importAllCount,
  getImportTarget,
} from '~/import_entities/import_projects/store/getters';
import state from '~/import_entities/import_projects/store/state';

const IMPORTED_REPO = {
  importSource: {},
  importedProject: { fullPath: 'some/path', importStatus: STATUSES.FINISHED },
};

const IMPORTABLE_REPO = {
  importSource: { id: 'some-id', sanitizedName: 'sanitized' },
  importedProject: null,
};

const INCOMPATIBLE_REPO = {
  importSource: { incompatible: true },
};

describe('import_projects store getters', () => {
  let localState;

  beforeEach(() => {
    localState = state();
  });

  it.each`
    importStatus           | value
    ${STATUSES.NONE}       | ${false}
    ${STATUSES.SCHEDULING} | ${true}
    ${STATUSES.SCHEDULED}  | ${true}
    ${STATUSES.STARTED}    | ${true}
    ${STATUSES.FINISHED}   | ${false}
  `(
    'isImportingAnyRepo returns $value when project with $importStatus status is available',
    ({ importStatus, value }) => {
      localState.repositories = [{ importedProject: { importStatus } }];

      expect(isImportingAnyRepo(localState)).toBe(value);
    },
  );

  it('isImportingAnyRepo returns false when project with no defined importStatus status is available', () => {
    localState.repositories = [{ importSource: {} }];

    expect(isImportingAnyRepo(localState)).toBe(false);
  });

  describe('hasIncompatibleRepos', () => {
    it('returns true if there are any incompatible projects', () => {
      localState.repositories = [IMPORTABLE_REPO, IMPORTED_REPO, INCOMPATIBLE_REPO];

      expect(hasIncompatibleRepos(localState)).toBe(true);
    });

    it('returns false if there are no incompatible projects', () => {
      localState.repositories = [IMPORTABLE_REPO, IMPORTED_REPO];

      expect(hasIncompatibleRepos(localState)).toBe(false);
    });
  });

  describe('hasImportableRepos', () => {
    it('returns true if there are any importable projects', () => {
      localState.repositories = [IMPORTABLE_REPO, IMPORTED_REPO, INCOMPATIBLE_REPO];

      expect(hasImportableRepos(localState)).toBe(true);
    });

    it('returns false if there are no importable projects', () => {
      localState.repositories = [IMPORTED_REPO, INCOMPATIBLE_REPO];

      expect(hasImportableRepos(localState)).toBe(false);
    });
  });

  describe('importAllCount', () => {
    it('returns count of available importable projects', () => {
      localState.repositories = [
        IMPORTABLE_REPO,
        IMPORTABLE_REPO,
        IMPORTED_REPO,
        INCOMPATIBLE_REPO,
      ];

      expect(importAllCount(localState)).toBe(2);
    });
  });

  describe('getImportTarget', () => {
    it('returns default value if no custom target available', () => {
      localState.defaultTargetNamespace = 'default';
      localState.repositories = [IMPORTABLE_REPO];

      expect(getImportTarget(localState)(IMPORTABLE_REPO.importSource.id)).toStrictEqual({
        newName: IMPORTABLE_REPO.importSource.sanitizedName,
        targetNamespace: localState.defaultTargetNamespace,
      });
    });

    it('returns custom import target if available', () => {
      const fakeTarget = { newName: 'something', targetNamespace: 'ns' };
      localState.repositories = [IMPORTABLE_REPO];
      localState.customImportTargets[IMPORTABLE_REPO.importSource.id] = fakeTarget;

      expect(getImportTarget(localState)(IMPORTABLE_REPO.importSource.id)).toStrictEqual(
        fakeTarget,
      );
    });
  });
});