Skip to content

Commit

Permalink
refactoring of VersionInfo.load() usage
Browse files Browse the repository at this point in the history
  • Loading branch information
heimwege committed Jul 31, 2024
1 parent 26ebcae commit b2209e5
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/preview-middleware-client/src/adp/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import VersionInfo from 'sap/ui/VersionInfo';
import { getUI5VersionValidationMessage } from './ui5-version-utils';
import UI5Element from 'sap/ui/dt/Element';
import { getError } from '../cpe/error-utils';
import type { VersionInformation } from '../../types/global';
import type {SingleVersionInfo} from '../../types/global';

export default async function (rta: RuntimeAuthoring) {
const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version as string;
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
const versionParts = version.split('.');
const minor = parseInt(versionParts[1], 10);
const flexSettings = rta.getFlexSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExternalAction, storageFileChanged } from '@sap-ux-private/control-property-editor-common';
import { ActionSenderFunction } from './types';
import VersionInfo from 'sap/ui/VersionInfo';
import type { VersionInformation } from '../../types/global';
import type {SingleVersionInfo} from '../../types/global';

/**
* A Class of WorkspaceConnectorService
Expand All @@ -13,7 +13,7 @@ export class WorkspaceConnectorService {
* @param sendAction action sender function
*/
public async init(sendAction: ActionSenderFunction): Promise<void> {
const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version ?? '';
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
const versionParts = version.split('.');
const minor = parseInt(versionParts[1], 10);
if (minor > 72) {
Expand Down
4 changes: 2 additions & 2 deletions packages/preview-middleware-client/src/cpe/outline/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { OutlineViewNode } from 'sap/ui/rta/command/OutlineService';
import type { Scenario } from 'sap/ui/fl/Scenario';
import { isEditable, isReuseComponent } from './utils';
import VersionInfo from 'sap/ui/VersionInfo';
import type { VersionInformation } from '../../../types/global';
import type {SingleVersionInfo} from '../../../types/global';

interface AdditionalData {
text?: string;
Expand Down Expand Up @@ -88,7 +88,7 @@ export async function transformNodes(
): Promise<OutlineNode[]> {
const stack = [...input];
const items: OutlineNode[] = [];
const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version as string;
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
const versionParts = version.split('.');
const minor = parseInt(versionParts[1], 10);
while (stack.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import merge from 'sap/base/util/merge';
import ObjectStorageConnector from 'sap/ui/fl/write/api/connectors/ObjectStorageConnector';
import Layer from 'sap/ui/fl/Layer';
import VersionInfo from 'sap/ui/VersionInfo';
import type { VersionInformation } from '../../types/global';
import type {SingleVersionInfo} from '../../types/global';
import { CHANGES_API_PATH, FlexChange, getFlexSettings } from './common';

const connector = merge({}, ObjectStorageConnector, {
Expand Down Expand Up @@ -70,7 +70,7 @@ const connector = merge({}, ObjectStorageConnector, {
loadFeatures: async function () {
const features = await ObjectStorageConnector.loadFeatures();

const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version as string;
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
const [majorVersion, minorVersion] = version.split('.').map((v: string) => parseInt(v, 10));
features.isVariantAdaptationEnabled = majorVersion >= 1 && minorVersion >= 90;
const settings = getFlexSettings();
Expand Down
4 changes: 2 additions & 2 deletions packages/preview-middleware-client/src/flp/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getManifestAppdescr } from '../adp/api-handler';
import VersionInfo from 'sap/ui/VersionInfo';
import { getError } from '../cpe/error-utils';
import initConnectors from './initConnectors';
import type { VersionInformation } from '../../types/global';
import type {SingleVersionInfo} from '../../types/global';

/**
* SAPUI5 delivered namespaces from https://ui5.sap.com/#/api/sap
Expand Down Expand Up @@ -270,7 +270,7 @@ export async function init({
const urlParams = new URLSearchParams(window.location.search);
const container = sap?.ushell?.Container ?? sap.ui.require('sap/ushell/Container');
let scenario: string = '';
const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version as string;
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
// Register RTA if configured
if (flex) {
const flexSettings = JSON.parse(flex) as FlexSettings;
Expand Down
4 changes: 2 additions & 2 deletions packages/preview-middleware-client/src/flp/initConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import VersionInfo from 'sap/ui/VersionInfo';
import type { VersionInformation } from '../../types/global';
import type {SingleVersionInfo} from '../../types/global';

/**
* Initializes UI5 connectors based on the current UI5 version.
Expand All @@ -12,7 +12,7 @@ import type { VersionInformation } from '../../types/global';
* intiConnectors(); // Simply call the function without any arguments.
*/
export default async function initConnectors(): Promise<void> {
const version = (await VersionInfo.load() as VersionInformation)?.libraries?.find((lib) => lib.name === 'sap.ui.core')?.version as string;
const version = (await VersionInfo.load({library:'sap.ui.core'}) as SingleVersionInfo)?.version;
const versionArray = version ? version.split('.') : ['2', '99'];
const minor = parseInt(versionArray[1], 10);
const major = parseInt(versionArray[0], 10);
Expand Down
8 changes: 4 additions & 4 deletions packages/preview-middleware-client/test/unit/adp/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('adp', () => {
return { dispose: jest.fn(), sendAction: jest.fn() };
});
const enableTelemetry = jest.spyOn(common, 'enableTelemetry');
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.118.1' });

await init(rtaMock as unknown as RuntimeAuthoring);

Expand Down Expand Up @@ -93,7 +93,7 @@ describe('adp', () => {
return { dispose: jest.fn(), sendAction: sendActionMock };
});

VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.70.0'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.70.0' });

await init(rtaMock as unknown as RuntimeAuthoring);

Expand Down Expand Up @@ -133,7 +133,7 @@ describe('adp', () => {
'application-app-preview-component---fin.ar.lineitems.display.appView': mockUI5Element
});

VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.123.1'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.123.1' });

await init(rtaMock as unknown as RuntimeAuthoring);

Expand Down Expand Up @@ -172,7 +172,7 @@ describe('adp', () => {

Element.registry.filter.mockReturnValue([mockUI5Element]);

VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.118.1' });

await init(rtaMock as unknown as RuntimeAuthoring);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('connector-service', () => {
fetchMock.mockRestore();
});
test('init - ui5 > v1.72', async () => {
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.120.4'}] });
VersionInfo.load.mockResolvedValue({ name: 'sap.ui.core', version: '1.120.4' });
const wsConnector = new WorkspaceConnectorService();
await wsConnector.init(sendActionMock);

Expand All @@ -25,7 +25,7 @@ describe('connector-service', () => {
});

test('init - ui5 < v1.72', async () => {
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.71.67'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.71.67' });
const wsConnector = new WorkspaceConnectorService();
await wsConnector.init(sendActionMock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('outline nodes', () => {
});

beforeAll(() => {
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.118.1' });
})

describe('transformNodes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ describe('flp/WorkspaceConnector', () => {
});

test('version >= 1.90, no developerMode', async () => {
VersionInfo.load.mockResolvedValueOnce({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValueOnce({name: 'sap.ui.core', version: '1.118.1' });
const features = await connector.loadFeatures();
expect(features.isVariantAdaptationEnabled).toBe(true);
});

test('version < 1.90', async () => {
VersionInfo.load.mockResolvedValueOnce({ libraries: [{name: 'sap.ui.core', version: '1.89.3'}] });
VersionInfo.load.mockResolvedValueOnce({name: 'sap.ui.core', version: '1.89.3' });
const features = await connector.loadFeatures();
expect(features.isVariantAdaptationEnabled).toBe(false);
});

test('version >= 1.90, developerMode=true', async () => {
VersionInfo.load.mockResolvedValueOnce({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValueOnce({name: 'sap.ui.core', version: '1.118.1' });
documentMock.getElementById.mockReturnValueOnce({
getAttribute: () => JSON.stringify({ developerMode: true })
});
Expand All @@ -118,7 +118,7 @@ describe('flp/WorkspaceConnector', () => {
});

test('scenario=ADAPTATION_PROJECT', async () => {
VersionInfo.load.mockResolvedValueOnce({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValueOnce({name: 'sap.ui.core', version: '1.118.1' });
documentMock.getElementById.mockReturnValueOnce({
getAttribute: () => JSON.stringify({ scenario: 'ADAPTATION_PROJECT' })
});
Expand Down
10 changes: 5 additions & 5 deletions packages/preview-middleware-client/test/unit/flp/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('flp/init', () => {
});

test('nothing configured', async () => {
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.118.1'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.118.1' });
await init({});
expect(sapMock.ushell.Container.attachRendererCreatedEvent).not.toBeCalled();
expect(sapMock.ushell.Container.createRenderer).toBeCalledWith(undefined, true);
Expand All @@ -209,7 +209,7 @@ describe('flp/init', () => {
layer: 'CUSTOMER_BASE',
pluginScript: 'my/script'
};
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.84.50'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.84.50' });
await init({ flex: JSON.stringify(flexSettings) });
expect(sapMock.ushell.Container.attachRendererCreatedEvent).toBeCalled();
expect(sapMock.ushell.Container.createRenderer).toBeCalledWith(undefined, true);
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('flp/init', () => {
layer: 'CUSTOMER_BASE',
pluginScript: 'my/script'
};
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.71.60'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.71.60' });
await init({ flex: JSON.stringify(flexSettings) });
expect(sapMock.ushell.Container.attachRendererCreatedEvent).toBeCalled();
expect(sapMock.ushell.Container.createRenderer).toBeCalledWith(undefined, true);
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('flp/init', () => {

test('custom init module configured & ui5 version is 1.120.9', async () => {
const customInit = 'my/app/test/integration/opaTests.qunit';
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.120.9'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.120.9' });

await init({ customInit: customInit });

Expand All @@ -292,7 +292,7 @@ describe('flp/init', () => {

test('custom init module configured & ui5 version is 2.0.0', async () => {
const customInit = 'my/app/test/integration/opaTests.qunit';
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '2.0.0'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '2.0.0' });

await init({ customInit: customInit });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('flp/initConnectors', () => {

test('enables fake lrep connector when ui5 version is 1.71', async () => {
sapMock.ui.version = '1.71.60';
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.71.60'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.71.60' });
await initConnectors();

expect(sapMock.ui.require).toBeCalledWith(
Expand All @@ -26,7 +26,7 @@ describe('flp/initConnectors', () => {

test('defines a local connector for writing and applying changes and returns it', async () => {
sapMock.ui.version = '1.120.4';
VersionInfo.load.mockResolvedValue({ libraries: [{name: 'sap.ui.core', version: '1.120.4'}] });
VersionInfo.load.mockResolvedValue({name: 'sap.ui.core', version: '1.120.4' });

await initConnectors();

Expand Down
2 changes: 1 addition & 1 deletion packages/preview-middleware-client/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface Window {
};
}

type SingleVersionInfo = {
export type SingleVersionInfo = {
/**
* The name of the library or application
*/
Expand Down

0 comments on commit b2209e5

Please sign in to comment.