-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProjectImportFilesystemHelpers.test.js
More file actions
256 lines (209 loc) · 7.01 KB
/
ProjectImportFilesystemHelpers.test.js
File metadata and controls
256 lines (209 loc) · 7.01 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* eslint-env jest */
/* eslint-disable no-console */
import fs from 'fs-extra';
import path from 'path-extra';
// helpers
import * as ProjectImportFilesystemHelpers from '../js/helpers/Import/ProjectImportFilesystemHelpers';
// constants
import { PROJECTS_PATH, IMPORTS_PATH } from '../js/common/constants';
jest.mock('fs-extra');
const projectName = 'aa_tit_text_ulb';
const fromPath = path.join(IMPORTS_PATH, projectName);
const toPath = path.join(PROJECTS_PATH, projectName);
const reimportCompoundMsg = 'projects.project_exists projects.reimporting_not_supported';
const noProjectInImportsFolderRejectMsg = {
'data': {
'fromPath': fromPath,
'projectName': projectName,
},
'message': 'projects.not_found',
};
describe('ProjectImportFilesystemHelpers.move',()=> {
beforeEach(()=>{
fs.__resetMockFS();
});
test('ProjectImportFilesystemHelpers.move verifies that it does not reimport a project', () => {
fs.__setMockFS({
[toPath]: '',
[fromPath]: '',
});
expect.assertions(1);
return expect(ProjectImportFilesystemHelpers.moveProject(projectName, k=>k)).rejects.toEqual(reimportCompoundMsg);
});
test('ProjectImportFilesystemHelpers.move should fail/reject if the specified project is not found in the imports folder', () => {
expect.assertions(1);
return expect(ProjectImportFilesystemHelpers.moveProject(projectName, jest.fn())).rejects.toEqual(noProjectInImportsFolderRejectMsg);
});
test('ProjectImportFilesystemHelpers.move should move the file from imports folder to projects folder', () => {
fs.__setMockFS({ [fromPath]: '' });
expect(fs.existsSync(toPath)).toBeFalsy();
expect(fs.existsSync(fromPath)).toBeTruthy();
return expect(ProjectImportFilesystemHelpers.moveProject(projectName, jest.fn())).resolves.toBe(toPath);
});
});
describe('ProjectImportFilesystemHelpers.projectExistsInProjectsFolder',()=> {
beforeEach(()=>{
fs.__resetMockFS();
});
const projectManifest = {
'target_language': {
'id': 'amo',
'name': 'Amo',
},
'project': {
'id': 'tit',
'name': 'Titus',
},
};
const uniqueManifest = {
'target_language': {
'id': 'hi',
'name': 'Hindi',
},
'project': {
'id': 'tit',
'name': 'Titus',
},
};
test('should verify that the given project exists in the projects folder', () => {
fs.__setMockFS({
[PROJECTS_PATH]:[projectName],
[fromPath]:[],
[toPath]:[],
[path.join(toPath, 'manifest.json')]: projectManifest,
[path.join(fromPath, 'manifest.json')]: projectManifest,
});
expect(ProjectImportFilesystemHelpers.projectExistsInProjectsFolder(fromPath)).toEqual(true);
});
test('should verify that the given project does not exist in the projects folder', () => {
fs.__setMockFS({
[PROJECTS_PATH]:[projectName],
[path.join(toPath, 'manifest.json')]: projectManifest,
[path.join(fromPath, 'manifest.json')]: uniqueManifest,
});
expect(ProjectImportFilesystemHelpers.projectExistsInProjectsFolder(fromPath)).toEqual(false);
});
});
describe('areStringsEqualCaseInsensitive()', () => {
test('expect "HI" == "HI"', () => {
// given
const expectedResult = true;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('HI', 'HI');
// then
expect(results).toEqual(expectedResult);
});
test('expect "hI" == "Hi"', () => {
// given
const expectedResult = true;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('hI', 'Hi');
// then
expect(results).toEqual(expectedResult);
});
test('expect "hi" == "hi"', () => {
// given
const expectedResult = true;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('hi', 'hi');
// then
expect(results).toEqual(expectedResult);
});
test('expect "hi" == "HI"', () => {
// given
const expectedResult = true;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('hi', 'HI');
// then
expect(results).toEqual(expectedResult);
});
test('expect "" != "HI"', () => {
// given
const expectedResult = false;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('', 'HI');
// then
expect(results).toEqual(expectedResult);
});
test('expect null != "HI"', () => {
// given
const expectedResult = false;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive(null, 'HI');
// then
expect(results).toEqual(expectedResult);
});
test('expect "hi" != undefined', () => {
// given
const expectedResult = false;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('hi', undefined);
// then
expect(results).toEqual(expectedResult);
});
test('expect "hi" != 5', () => {
// given
const expectedResult = false;
// when
const results = ProjectImportFilesystemHelpers.areStringsEqualCaseInsensitive('hi', 5);
// then
expect(results).toEqual(expectedResult);
});
});
describe('ProjectDetailsActions.getProjectsByType()', () => {
const projectsPath = path.join(PROJECTS_PATH);
beforeEach(() => {
// reset mock filesystem data
fs.__resetMockFS();
// Set up mock filesystem before each test
fs.ensureDirSync(projectsPath);
createMockProject(projectsPath, 'en_ult_eph_book', 'en', 'eph', 'ult');
});
test('finds project', () => {
// given
const expectedResults = ['en_ult_eph_book'];
const langID = 'en';
const bookID = 'eph';
const resourceId = 'ult';
// when
const results = ProjectImportFilesystemHelpers.getProjectsByType(langID, bookID, resourceId);
// then
expect(results).toEqual(expectedResults);
});
test('finds project with upppercase', () => {
// given
const expectedResults = ['en_ult_eph_book'];
const langID = 'EN';
const bookID = 'EPH';
const resourceId = 'ULT';
// when
const results = ProjectImportFilesystemHelpers.getProjectsByType(langID, bookID, resourceId);
// then
expect(results).toEqual(expectedResults);
});
test('does not find project with different resource', () => {
// given
const expectedResults = [ ];
const langID = 'EN';
const bookID = 'EPH';
const resourceId = 'ULTT';
// when
const results = ProjectImportFilesystemHelpers.getProjectsByType(langID, bookID, resourceId);
// then
expect(results).toEqual(expectedResults);
});
});
//
// helpers
//
function createMockProject(projectsPath, fileName, langID, bookID, resourceID) {
fileName = fileName || (langID + '_' + resourceID + '_' + bookID + '_book');
const projectFolderPath = path.join(projectsPath, fileName.toLowerCase());
fs.ensureDirSync(projectFolderPath);
const manifest = {
target_language: { id: langID },
project: { id: bookID },
resource: { id: resourceID },
};
fs.outputJsonSync(path.join(projectFolderPath, 'manifest.json'), manifest);
}