UNPKG

@progress/sitefinity-widget-designers-sdk

Version:

This package aims to create a parity for widget designer generation similar to the [autogenerated widget designers](https://www.progress.com/documentation/sitefinity-cms/next.js-autogenerated-field-types) in Sitefinity. Due to some limitations in Typescri

129 lines (128 loc) 7.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyJsonProperty = exports.verifyProperty = exports.verifySection = exports.verifyCategory = exports.verifyFullDesigner = exports.verifyPropertyKeyValues = void 0; function verifyPropertyKeyValues(property, expectedValues) { for (const key in expectedValues) { if (Object.prototype.hasOwnProperty.call(expectedValues, key)) { const element = expectedValues[key]; expect(property[key]).toBe(element); } } } exports.verifyPropertyKeyValues = verifyPropertyKeyValues; function verifyFullDesigner(actual, expected) { expect(actual.Name).toBe(expected.Name); expect(actual.Caption).toBe(expected.Caption); expect(`${actual.Name} properties count: ${actual.PropertyMetadata.length}`) .toBe(`${expected.Name} properties count: ${expected.PropertyMetadata.length}`); // go on each section actual.PropertyMetadata.forEach(actualCategory => { const expectedCategory = expected.PropertyMetadata.find(x => x.Name === actualCategory.Name); verifyCategory(actual.Name, actualCategory, expectedCategory); }); } exports.verifyFullDesigner = verifyFullDesigner; function verifyCategory(widgetName, actual, expected) { expect(`${widgetName}'s category => ${actual.Name}`).toBe(`${widgetName}'s category => ${expected.Name}`); const actualSections = actual.Sections.map(x => x.Name).join(', '); const expectedSections = expected.Sections.map(x => x.Name).join(', '); expect(`${widgetName}'s ${actual.Name} sections: ${actualSections}`) .toBe(`${widgetName}'s ${expected.Name} sections: ${expectedSections}`); // verify each section actual.Sections.forEach((actualSection, i) => { const expectedSection = expected.Sections[i]; expect(expectedSection).not.toBeNull(); expect(`${widgetName}'s section => ${actualSection.Name}`).toBe(`${widgetName}'s section => ${expectedSection.Name}`); verifySection(widgetName, actualSection, expectedSection); }); } exports.verifyCategory = verifyCategory; function verifySection(widgetName, actual, expected) { expect(`${widgetName}'s ${actual.CategoryName} section name => ${actual.Name}`) .toBe(`${widgetName}'s ${expected.CategoryName} section name => ${expected.Name}`); expect(`${widgetName}'s ${actual.CategoryName} category's section title => ${actual.Title}`) .toBe(`${widgetName}'s ${expected.CategoryName} category's section title => ${expected.Title}`); expect(`${widgetName}'s ${actual.Name} section category => ${actual.CategoryName}`) .toBe(`${widgetName}'s ${expected.Name} section category => ${expected.CategoryName}`); expect(`${widgetName}'s ${actual.Name} ${actual.Properties.map(x => x.Name).join(', ')}`) .toBe(`${widgetName}'s ${expected.Name} ${expected.Properties.map(x => x.Name).join(', ')}`); // verify each property actual.Properties.forEach(actualProp => { const expectedProp = expected.Properties.find(x => x.Name === actualProp.Name); expect(expectedProp).not.toBeNull(); verifyProperty(actualProp, expectedProp); }); } exports.verifySection = verifySection; function verifyProperty(actual, expected) { expect(`name: ${actual.Name}`).toBe(`name: ${expected === null || expected === void 0 ? void 0 : expected.Name}`); expect(`${actual.Name} title: ${actual.Title}`).toBe(`${expected.Name} title: ${expected.Title}`); // null fallbacks to string in iris if (!(actual.Type === 'string' && expected.Type === null)) { expect(`${actual.Name} type: ${actual.Type}`) .toBe(`${expected.Name} type: ${expected.Type}`); } expect(actual.CategoryName).toBe(expected.CategoryName); if (expected.SectionName !== null && expected.SectionName !== '') { expect(`${actual.Name}'s section: ${actual.SectionName}`) .toBe(`${expected.Name}'s section: ${expected.SectionName}`); } expect(`${actual.Name} default value: ${actual.DefaultValue} with type: ${typeof (actual.DefaultValue)}`) .toBe(`${expected.Name} default value: ${expected.DefaultValue} with type: ${typeof (expected.DefaultValue)}`); expect(`${actual.Name} position: ${actual.Position}`).toBe(`${expected.Name} position: ${expected.Position}`); // verify properties count const actualPropertyKeys = Object.keys(actual.Properties).sort().join(', '); const expectedPropertyKeys = Object.keys(expected.Properties).sort().join(', '); expect(`${actual.Name} ${actualPropertyKeys}`).toBe(`${expected.Name} ${expectedPropertyKeys}`); // eslint-disable-next-line guard-for-in for (const key in expected.Properties) { const expectedElement = expected.Properties[key]; const actualElement = actual.Properties[key]; switch (key) { case 'Meta_LengthDependsOn_ExtraRecords': case 'Meta_ViewMetaData': case 'Meta_Choices': case 'Meta_Choice_Choices': verifyJsonProperty(`${actual.Name}.${key}`, actualElement, expectedElement); break; default: expect(`${actual.Name}:${key} -> ${actualElement} with type: ${typeof (actualElement)}`) .toBe(`${expected.Name}:${key} -> ${expectedElement} with type: ${typeof (expectedElement)}`); break; } } // verify typechildproperties expect(`${actual.Name} TypeChildProperties count: ${actual.TypeChildProperties.length}`) .toBe(`${expected.Name} TypeChildProperties count: ${expected.TypeChildProperties.length}`); const actualTypeChildPropertiesNames = actual.TypeChildProperties.map(x => x.Name).join(', '); const expectedTypeChildPropertiesNames = expected.TypeChildProperties.map(x => x.Name).join(', '); expect(`TypeChildProperties order for ${actual.Name}: ${actualTypeChildPropertiesNames}`) .toBe(`TypeChildProperties order for ${expected.Name}: ${expectedTypeChildPropertiesNames}`); actual.TypeChildProperties.forEach(typeChildProp => { const expectedProperty = expected.TypeChildProperties.find(x => x.Name === typeChildProp.Name); verifyProperty(typeChildProp, expectedProperty); }); } exports.verifyProperty = verifyProperty; function verifyJsonProperty(propName, actual, expected) { if (expected == null || actual == null) { expect(`${propName} -> ${actual}`).toBe(`${propName} -> ${expected}`); return; } const expectedParsed = typeof (expected) === 'string' ? JSON.parse(expected) : expected; const actualParsed = typeof (actual) === 'string' ? JSON.parse(actual) : actual; const expectedKeys = Object.keys(expectedParsed); const actualKeys = Object.keys(actualParsed); expect(`${propName} keys: ${actualKeys.sort().join(', ')}`).toBe(`${propName} keys: ${expectedKeys.sort().join(', ')}`); expectedKeys.forEach(key => { const actualValue = actualParsed[key]; const expectedValue = expectedParsed[key]; if (expectedValue != null && typeof (expectedValue) === 'object') { verifyJsonProperty(`${propName}.${key}`, actualValue, expectedValue); } else { expect(`${propName}.${key} -> ${actualValue} with type ${typeof (actualValue)}`).toBe(`${propName}.${key} -> ${expectedValue} with type ${typeof (expectedValue)}`); } }); } exports.verifyJsonProperty = verifyJsonProperty;