-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProjectAPI.test.js
More file actions
135 lines (120 loc) · 5.17 KB
/
ProjectAPI.test.js
File metadata and controls
135 lines (120 loc) · 5.17 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
/* eslint-env jest */
import fs from 'fs-extra';
import path from 'path-extra';
import _ from 'lodash';
import ProjectAPI from '../js/helpers/ProjectAPI';
// constants
import {
PROJECTS_PATH, TRANSLATION_NOTES, USER_RESOURCES_PATH,
} from '../js/common/constants';
describe('importCategoryGroupData()', () => {
const sourceResourcesPath = path.join('src/__tests__/fixtures/resources');
const projectName = 'en_gal';
const projectDir = path.join(PROJECTS_PATH, projectName);
const bookId = 'gal';
const tnIndexPath = path.join(projectDir, '.apps', 'translationCore', 'index', TRANSLATION_NOTES, bookId);
const manifest = { 'project':{ 'id': bookId } };
const galIndexPath = 'en/translationHelps/translationNotes/v15_Door43-Catalog/culture/groups/gal';
beforeAll(() => {
jest.restoreAllMocks(); // remove all mocks
});
beforeEach(() => {
jest.restoreAllMocks(); // remove all mocks
// reset mock filesystem data
fs.__resetMockFS();
const copyResourceFiles = [galIndexPath];
fs.__loadFilesIntoMockFs(copyResourceFiles, sourceResourcesPath, USER_RESOURCES_PATH);
fs.outputJsonSync(path.join(projectDir, 'manifest.json'), manifest);
});
it('imports new group data', () => {
// given
const p = new ProjectAPI(projectDir);
const toolName = TRANSLATION_NOTES;
const groupId = 'figs-explicit';
const groupFileName = groupId + '.json';
const tnHelpsGroupDataPath = path.join(USER_RESOURCES_PATH, galIndexPath, groupFileName);
const groupsDataLoaded = [];
fs.outputJsonSync(path.join(projectDir, 'manifest.json'), manifest);
// when
const results = p.importCategoryGroupData(toolName, tnHelpsGroupDataPath, groupsDataLoaded);
// then
expect(results).toBeTruthy();
const projectGroupIdPath = path.join(tnIndexPath, groupFileName);
expect(fs.existsSync(projectGroupIdPath)).toBeTruthy();
const tnHelpsGroupData = fs.readJsonSync(tnHelpsGroupDataPath);
const projectGroupData = fs.readJsonSync(projectGroupIdPath);
expect(projectGroupData).toEqual(tnHelpsGroupData);
});
it('updates new group contextId without overwriting existing data', () => {
// given
const p = new ProjectAPI(projectDir);
const toolName = TRANSLATION_NOTES;
const groupId = 'figs-explicit';
const groupFileName = groupId + '.json';
const tnHelpsGroupDataPath = path.join(USER_RESOURCES_PATH, galIndexPath, groupFileName);
const groupsDataLoaded = [];
const groupIdDataPath = path.join(tnIndexPath, groupId + '.json');
const groupIdData = fs.readJsonSync(tnHelpsGroupDataPath);
const groupItemNumber = 4;
const { preExistingGroupData, oldNote } = mockExistingGroupData(groupIdData, groupItemNumber, groupIdDataPath);
// when
const results = p.importCategoryGroupData(toolName, tnHelpsGroupDataPath, groupsDataLoaded);
// then
expect(results).toBeTruthy();
const projectGroupIdPath = path.join(tnIndexPath, groupFileName);
expect(fs.existsSync(projectGroupIdPath)).toBeTruthy();
const projectGroupData = fs.readJsonSync(projectGroupIdPath);
verifyArraysMatchExceptItem(projectGroupData, preExistingGroupData, groupItemNumber);
const shouldMatch = _.cloneDeep(preExistingGroupData[groupItemNumber]);
shouldMatch.contextId.occurrenceNote = oldNote;
expect(projectGroupData[groupItemNumber]).toEqual(shouldMatch); // data should be preserved but contextId is updated
});
it('skips importing existing group data', () => {
// given
const p = new ProjectAPI(projectDir);
const toolName = TRANSLATION_NOTES;
const groupId = 'figs-explicit';
const groupFileName = groupId + '.json';
const tnHelpsGroupDataPath = path.join(USER_RESOURCES_PATH, galIndexPath, groupFileName);
const groupsDataLoaded = [groupId];
// when
p.importCategoryGroupData(toolName, tnHelpsGroupDataPath, groupsDataLoaded);
// then
const results = p.importCategoryGroupData(toolName, tnHelpsGroupDataPath, groupsDataLoaded);
// then
expect(results).toBeFalsy();
});
});
//
// helpers
//
/**
* match all items in array except for item at exceptItem
* @param {Array} resultsArray
* @param {Array} expectedArray
* @param {Number} exceptItem
*/
function verifyArraysMatchExceptItem(resultsArray, expectedArray, exceptItem) {
const compArray1 = _.cloneDeep(resultsArray);
compArray1[exceptItem] = {};
const compArray2 = _.cloneDeep(expectedArray);
compArray2[exceptItem] = {};
expect (compArray1).toEqual(compArray2);
}
/**
* mock existing data by modifying groupIdData and saving to project index
* @param groupIdData
* @param groupItemNumber
* @param groupIdDataPath
* @return {{preExistingGroupData: *, oldNote: *}}
*/
function mockExistingGroupData(groupIdData, groupItemNumber, groupIdDataPath) {
const preExistingGroupData = _.cloneDeep(groupIdData);
const preExistingGroupItem = preExistingGroupData[groupItemNumber];
const oldNote = preExistingGroupItem.contextId.occurrenceNote;
preExistingGroupItem.contextId.occurrenceNote = 'old Note';
preExistingGroupItem.invalidated = true;
preExistingGroupItem.selections = ['by hearing with faith'];
fs.outputJsonSync(groupIdDataPath, preExistingGroupData);
return { preExistingGroupData, oldNote };
}