-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathui5-xml-id-validator.test.ts
More file actions
111 lines (97 loc) · 3.96 KB
/
ui5-xml-id-validator.test.ts
File metadata and controls
111 lines (97 loc) · 3.96 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
import { isUI5IdUnique } from '../../src/project/ui5-xml-id-validator';
describe('isUI5IdUnique', () => {
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', () => {
const result = isUI5IdUnique('newButton', [sampleView, sampleFragment]);
expect(result).toBe(true);
});
test('should return false when id exists in view', () => {
const result = isUI5IdUnique('submitButton', [sampleView, sampleFragment]);
expect(result).toBe(false);
});
test('should return false when id exists in fragment', () => {
const result = isUI5IdUnique('confirmDialog', [sampleView, sampleFragment]);
expect(result).toBe(false);
});
test('should return true when id is unique across multiple files', () => {
const result = isUI5IdUnique('uniqueId', [sampleView, sampleFragment, sampleViewWithNamespace]);
expect(result).toBe(true);
});
test('should return false when id exists in nested elements', () => {
const result = isUI5IdUnique('dataTable', [sampleView]);
expect(result).toBe(false);
});
test('should return false when id exists in fragment dialog content', () => {
const result = isUI5IdUnique('dialogText', [sampleFragment]);
expect(result).toBe(false);
});
test('should return true for empty files array', () => {
const result = isUI5IdUnique('anyId', []);
expect(result).toBe(true);
});
test('should return true when XML parsing fails', () => {
// fast-xml-parser is lenient, but completely invalid content should fail
const invalidXml = '<<<>>><invalid';
const result = isUI5IdUnique('test', [invalidXml]);
expect(result).toBe(true);
});
test('should return true when files contain empty strings', () => {
const result = isUI5IdUnique('testId', ['', '', sampleView]);
expect(result).toBe(true);
});
test('should handle ids with special characters', () => {
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>`;
expect(isUI5IdUnique('button-with-dash', [xmlWithSpecialId])).toBe(false);
expect(isUI5IdUnique('button_with_underscore', [xmlWithSpecialId])).toBe(false);
expect(isUI5IdUnique('button.with.dot', [xmlWithSpecialId])).toBe(false);
expect(isUI5IdUnique('button-not-exists', [xmlWithSpecialId])).toBe(true);
});
});