|
| 1 | +import { isUI5IdUnique } from '../../src/project/ui5-xml-id-validator'; |
| 2 | + |
| 3 | +describe('isUI5IdUnique', () => { |
| 4 | + const sampleView = `<mvc:View |
| 5 | + xmlns:mvc="sap.ui.core.mvc" |
| 6 | + xmlns="sap.m" |
| 7 | + controllerName="my.app.controller.Main"> |
| 8 | + <Page id="mainPage" title="Main View"> |
| 9 | + <content> |
| 10 | + <Button id = "submitButton" text="Submit" /> |
| 11 | + <Input id="nameInput" placeholder="Enter name" /> |
| 12 | + <Table id ="dataTable"> |
| 13 | + <columns> |
| 14 | + <Column> |
| 15 | + <Text text="Name" /> |
| 16 | + </Column> |
| 17 | + </columns> |
| 18 | + </Table> |
| 19 | + </content> |
| 20 | + </Page> |
| 21 | +</mvc:View>`; |
| 22 | + |
| 23 | + const sampleFragment = `<core:FragmentDefinition |
| 24 | + xmlns="sap.m" |
| 25 | + xmlns:core="sap.ui.core"> |
| 26 | + <Dialog id="confirmDialog" title="Confirm Action"> |
| 27 | + <content> |
| 28 | + <Text id= "dialogText" text="Are you sure?" /> |
| 29 | + </content> |
| 30 | + <beginButton> |
| 31 | + <Button id="confirmButton" text="Confirm" press="onConfirm" /> |
| 32 | + </beginButton> |
| 33 | + <endButton> |
| 34 | + <Button id="cancelButton" text="Cancel" press="onCancel" /> |
| 35 | + </endButton> |
| 36 | + </Dialog> |
| 37 | +</core:FragmentDefinition>`; |
| 38 | + |
| 39 | + const sampleViewWithNamespace = `<mvc:View |
| 40 | + xmlns:mvc="sap.ui.core.mvc" |
| 41 | + xmlns="sap.m" |
| 42 | + xmlns:f="sap.ui.layout.form"> |
| 43 | + <f:SimpleForm id="detailForm"> |
| 44 | + <f:content> |
| 45 | + <Label text="Title" /> |
| 46 | + <Input id="titleInput" /> |
| 47 | + </f:content> |
| 48 | + </f:SimpleForm> |
| 49 | +</mvc:View>`; |
| 50 | + |
| 51 | + test('should return true when id does not exist in any files', () => { |
| 52 | + const result = isUI5IdUnique('newButton', [sampleView, sampleFragment]); |
| 53 | + expect(result).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should return false when id exists in view', () => { |
| 57 | + const result = isUI5IdUnique('submitButton', [sampleView, sampleFragment]); |
| 58 | + expect(result).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should return false when id exists in fragment', () => { |
| 62 | + const result = isUI5IdUnique('confirmDialog', [sampleView, sampleFragment]); |
| 63 | + expect(result).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should return true when id is unique across multiple files', () => { |
| 67 | + const result = isUI5IdUnique('uniqueId', [sampleView, sampleFragment, sampleViewWithNamespace]); |
| 68 | + expect(result).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should return false when id exists in nested elements', () => { |
| 72 | + const result = isUI5IdUnique('dataTable', [sampleView]); |
| 73 | + expect(result).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + test('should return false when id exists in fragment dialog content', () => { |
| 77 | + const result = isUI5IdUnique('dialogText', [sampleFragment]); |
| 78 | + expect(result).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should return true for empty files array', () => { |
| 82 | + const result = isUI5IdUnique('anyId', []); |
| 83 | + expect(result).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should return true when XML parsing fails', () => { |
| 87 | + // fast-xml-parser is lenient, but completely invalid content should fail |
| 88 | + const invalidXml = '<<<>>><invalid'; |
| 89 | + const result = isUI5IdUnique('test', [invalidXml]); |
| 90 | + expect(result).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should return true when files contain empty strings', () => { |
| 94 | + const result = isUI5IdUnique('testId', ['', '', sampleView]); |
| 95 | + expect(result).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should handle ids with special characters', () => { |
| 99 | + const xmlWithSpecialId = `<?xml version="1.0" encoding="UTF-8"?> |
| 100 | +<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"> |
| 101 | + <Button id="button-with-dash" text="Test" /> |
| 102 | + <Button id="button_with_underscore" text="Test" /> |
| 103 | + <Button id="button.with.dot" text="Test" /> |
| 104 | +</mvc:View>`; |
| 105 | + |
| 106 | + expect(isUI5IdUnique('button-with-dash', [xmlWithSpecialId])).toBe(false); |
| 107 | + expect(isUI5IdUnique('button_with_underscore', [xmlWithSpecialId])).toBe(false); |
| 108 | + expect(isUI5IdUnique('button.with.dot', [xmlWithSpecialId])).toBe(false); |
| 109 | + expect(isUI5IdUnique('button-not-exists', [xmlWithSpecialId])).toBe(true); |
| 110 | + }); |
| 111 | +}); |
0 commit comments