summaryrefslogtreecommitdiff
path: root/spec/frontend/import_projects/store/getters_spec.js
blob: 1ce42e534eab7ed04e7d540568ff196aafa61ba8 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
  isLoading,
  isImportingAnyRepo,
  hasIncompatibleRepos,
  hasImportableRepos,
  importAllCount,
  getImportTarget,
} from '~/import_projects/store/getters';
import { STATUSES } from '~/import_projects/constants';
import state from '~/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`
    isLoadingRepos | isLoadingNamespaces | isLoadingValue
    ${false}       | ${false}            | ${false}
    ${true}        | ${false}            | ${true}
    ${false}       | ${true}             | ${true}
    ${true}        | ${true}             | ${true}
  `(
    'isLoading returns $isLoadingValue when isLoadingRepos is $isLoadingRepos and isLoadingNamespaces is $isLoadingNamespaces',
    ({ isLoadingRepos, isLoadingNamespaces, isLoadingValue }) => {
      Object.assign(localState, {
        isLoadingRepos,
        isLoadingNamespaces,
      });

      expect(isLoading(localState)).toBe(isLoadingValue);
    },
  );

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