-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathhelpers.test.ts
More file actions
264 lines (236 loc) · 9.2 KB
/
helpers.test.ts
File metadata and controls
264 lines (236 loc) · 9.2 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
257
258
259
260
261
262
263
264
import { join } from 'node:path';
import {
checkDependencies,
getReuseLibs,
getLibraryDesc,
getManifestDesc,
validateId
} from '../../src/library/helpers';
import * as manifestJson from '../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/reuse/ex/test/lib/attachmentservice/manifest.json';
import type { LibraryXml, Manifest, ReuseLib } from '../../src';
import * as fileUtils from '../../src/file';
describe('library utils', () => {
test('should return library choices', async () => {
const findFilesSpy = jest.spyOn(fileUtils, 'findFiles');
const libChoices = await getReuseLibs([
{
projectRoot: join(__dirname, '../test-data/libs/sap.reuse.ex.test.lib.attachmentservice'),
manifestPath: join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/se/mi/plm/lib/attachmentservice/manifest.json'
),
manifest: manifestJson as Manifest,
libraryPath: join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/se/mi/plm/lib/attachmentservice/.library'
)
}
]);
expect(findFilesSpy).toHaveBeenCalledTimes(3);
expect(libChoices).toHaveLength(4);
libChoices.sort((a, b) => a.name.localeCompare(b.name));
expect(libChoices[0].name).toBe('sap.reuse.ex.test.lib.attachmentservice');
expect(libChoices[1].name).toBe('sap.reuse.ex.test.lib.attachmentservice.attachment');
expect(libChoices[2].name).toBe('sap.reuse.ex.test.lib.attachmentservice.attachment.components.fscomponent');
expect(libChoices[3].name).toBe('sap.reuse.ex.test.lib.attachmentservice.attachment.components.stcomponent');
expect(libChoices[0].path).toBe(
join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/reuse/ex/test/lib/attachmentservice'
)
);
expect(libChoices[1].path).toBe(
join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/reuse/ex/test/lib/attachmentservice/attachment'
)
);
expect(libChoices[2].path).toBe(
join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/reuse/ex/test/lib/attachmentservice/attachment/components/fscomponent'
)
);
expect(libChoices[3].path).toBe(
join(
__dirname,
'../test-data/libs/sap.reuse.ex.test.lib.attachmentservice/src/sap/reuse/ex/test/lib/attachmentservice/attachment/components/stcomponent'
)
);
for (const lib of libChoices) {
expect(lib.description).toBe('UI Library for Fiori Reuse Attachment Service');
}
});
test('should return missing dependencies', async () => {
const reuseLibAnswers = [
{
name: 'lib1',
dependencies: ['dep1', 'dep2', 'dep3']
}
] as ReuseLib[];
const allReuseLibs = [
{
name: 'dep1'
},
{
name: 'dep3'
}
] as ReuseLib[];
const missingDeps = checkDependencies(reuseLibAnswers, allReuseLibs);
expect(missingDeps).toEqual('dep2');
});
test('should return manifest description', async () => {
const manifest = {
'sap.app': {
description: 'test description'
}
} as Manifest;
const description = await getManifestDesc(manifest, 'mock/path');
expect(description).toEqual('test description');
});
test('should return library description', async () => {
const lib = {
'library': {
documentation: 'test description'
}
} as unknown as LibraryXml;
const description = await getLibraryDesc(lib, 'mock/path');
expect(description).toEqual('test description');
});
});
describe('validateId', () => {
const sampleView = `<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
controllerName="my.app.controller.Main">
<Page id="mainPage" title="Main View">
<content>
<Button id = "submitButton" text="Submit" />
<Input id="nameInput" placeholder="Enter name" />
<Table id ="dataTable">
<columns>
<Column>
<Text text="Name" />
</Column>
</columns>
</Table>
</content>
</Page>
</mvc:View>`;
const sampleFragment = `<core:FragmentDefinition
xmlns="sap.m"
xmlns:core="sap.ui.core">
<Dialog id="confirmDialog" title="Confirm Action">
<content>
<Text id= "dialogText" text="Are you sure?" />
</content>
<beginButton>
<Button id="confirmButton" text="Confirm" press="onConfirm" />
</beginButton>
<endButton>
<Button id="cancelButton" text="Cancel" press="onCancel" />
</endButton>
</Dialog>
</core:FragmentDefinition>`;
const sampleViewWithNamespace = `<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:f="sap.ui.layout.form">
<f:SimpleForm id="detailForm">
<f:content>
<Label text="Title" />
<Input id="titleInput" />
</f:content>
</f:SimpleForm>
</mvc:View>`;
test('should return true when id does not exist in any files', async () => {
const result = await validateId('newButton', undefined, {
files: [sampleView, sampleFragment]
});
expect(result).toBe(true);
});
test('should return false when id exists in view', async () => {
const result = await validateId('submitButton', undefined, {
files: [sampleView, sampleFragment]
});
expect(result).toBe(false);
});
test('should return false when id exists in fragment', async () => {
const result = await validateId('confirmDialog', undefined, {
files: [sampleView, sampleFragment]
});
expect(result).toBe(false);
});
test('should return false when id is in validatedIds array', async () => {
const result = await validateId('newButton', ['newButton', 'anotherButton'], {
files: [sampleView, sampleFragment]
});
expect(result).toBe(false);
});
test('should return true when id is unique across multiple files', async () => {
const result = await validateId('uniqueId', undefined, {
files: [sampleView, sampleFragment, sampleViewWithNamespace]
});
expect(result).toBe(true);
});
test('should return false when id exists in nested elements', async () => {
const result = await validateId('dataTable', undefined, {
files: [sampleView]
});
expect(result).toBe(false);
});
test('should return false when id exists in fragment dialog content', async () => {
const result = await validateId('dialogText', undefined, {
files: [sampleFragment]
});
expect(result).toBe(false);
});
test('should return true for empty files array', async () => {
const result = await validateId('anyId', undefined, {
files: []
});
expect(result).toBe(true);
});
test('should return true when XML parsing fails', async () => {
// fast-xml-parser is lenient, but completely invalid content should fail
const invalidXml = '<<<>>><invalid';
const result = await validateId('test', undefined, {
files: [invalidXml]
});
expect(result).toBe(true);
});
test('should return true when files contain empty strings', async () => {
const result = await validateId('testId', undefined, {
files: ['', '', sampleView]
});
expect(result).toBe(true);
});
test('should handle ids with special characters', async () => {
const xmlWithSpecialId = `<?xml version="1.0" encoding="UTF-8"?>
<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m">
<Button id="button-with-dash" text="Test" />
<Button id="button_with_underscore" text="Test" />
<Button id="button.with.dot" text="Test" />
</mvc:View>`;
const result1 = await validateId('button-with-dash', undefined, {
files: [xmlWithSpecialId]
});
expect(result1).toBe(false);
const result2 = await validateId('button_with_underscore', undefined, {
files: [xmlWithSpecialId]
});
expect(result2).toBe(false);
const result3 = await validateId('button.with.dot', undefined, {
files: [xmlWithSpecialId]
});
expect(result3).toBe(false);
const result4 = await validateId('button-not-exists', undefined, {
files: [xmlWithSpecialId]
});
expect(result4).toBe(true);
});
test('should return true when no options provided', async () => {
const result = await validateId('anyId');
expect(result).toBe(true);
});
});