Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle higher layer changes #2536

Merged
merged 26 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1621379
fix: handle higher layer changes
GDamyanov Nov 6, 2024
6da00db
chore: add changeset
GDamyanov Nov 6, 2024
1fa02cb
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 6, 2024
94e72fc
fix: disable lint error
GDamyanov Nov 7, 2024
5416121
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 11, 2024
c2c0266
fix: fix review comments
GDamyanov Nov 11, 2024
6d6f900
fix: update version condition
GDamyanov Nov 11, 2024
62fce62
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 11, 2024
b0bc564
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 11, 2024
652a18e
fix: review comments
GDamyanov Nov 12, 2024
c64fc41
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 12, 2024
be89ac8
fix: format code
GDamyanov Nov 12, 2024
aaf2e36
fix: remove timeout
GDamyanov Nov 12, 2024
646d740
test: update test name
GDamyanov Nov 12, 2024
cecaa00
fix: revert formatted line
GDamyanov Nov 12, 2024
26f8f27
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 12, 2024
a584e48
fix: update message
GDamyanov Nov 13, 2024
e0b6258
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 13, 2024
1d15285
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 13, 2024
d290e9a
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 13, 2024
b628491
Merge branch 'main' into fix/2535/handle-higher-layer-changes
GDamyanov Nov 15, 2024
89206fd
Merge branch 'main' into fix/2535/handle-higher-layer-changes
voicis Nov 15, 2024
220f8b1
Merge branch 'main' into fix/2535/handle-higher-layer-changes
voicis Nov 15, 2024
3e7a22b
Merge branch 'main' into fix/2535/handle-higher-layer-changes
voicis Nov 15, 2024
0461f58
Merge branch 'main' into fix/2535/handle-higher-layer-changes
voicis Nov 15, 2024
dcc2669
Merge branch 'main' into fix/2535/handle-higher-layer-changes
voicis Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/lazy-schools-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-ux-private/preview-middleware-client': patch
'@sap-ux/preview-middleware': patch
---

handle higher layer changes
39 changes: 35 additions & 4 deletions packages/preview-middleware-client/src/flp/init.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Log from 'sap/base/Log';
import type AppLifeCycle from 'sap/ushell/services/AppLifeCycle';
import type { InitRtaScript, RTAPlugin, StartAdaptation } from 'sap/ui/rta/api/startAdaptation';
import { SCENARIO, type Scenario } from '@sap-ux-private/control-property-editor-common';
import { SCENARIO, showMessage, type Scenario } from '@sap-ux-private/control-property-editor-common';
import type { FlexSettings, RTAOptions } from 'sap/ui/rta/RuntimeAuthoring';
import IconPool from 'sap/ui/core/IconPool';
import ResourceBundle from 'sap/base/i18n/ResourceBundle';
import AppState from 'sap/ushell/services/AppState';
import { getManifestAppdescr } from '../adp/api-handler';
import { getError } from '../utils/error';
import initConnectors from './initConnectors';
import { getUi5Version, isLowerThanMinimalUi5Version } from '../utils/version';
import { getUi5Version, isLowerThanMinimalUi5Version, Ui5VersionInfo } from '../utils/version';
import { CommunicationService } from '../cpe/communication-service';
import { getTextBundle } from '../i18n';

/**
* SAPUI5 delivered namespaces from https://ui5.sap.com/#/api/sap
Expand Down Expand Up @@ -304,7 +306,11 @@ export async function init({
libs,
// eslint-disable-next-line no-shadow
async function (startAdaptation: StartAdaptation | InitRtaScript, pluginScript: RTAPlugin) {
await startAdaptation(options, pluginScript);
try {
await startAdaptation(options, pluginScript);
} catch (error) {
await handleHigherLayerChanges(error, ui5VersionInfo);
}
}
);
});
Expand All @@ -318,7 +324,7 @@ export async function init({

// Load custom library paths if configured
if (appUrls) {
await registerComponentDependencyPaths(JSON.parse(appUrls) as string[] ?? [], urlParams);
await registerComponentDependencyPaths((JSON.parse(appUrls) as string[]) ?? [], urlParams);
}

// Load rta connector
Expand Down Expand Up @@ -351,3 +357,28 @@ if (bootstrapConfig) {
Log.error('Sandbox initialization failed: ' + error.message);
});
}

/**
* Handle higher layer changes when starting UI Adaptation.
* When RTA detects higher layer changes an error with Reload triggered text is thrown, the RTA instance is destroyed and the application is reloaded.
* For UI5 version lower than 1.84.0 RTA is showing a popup with notification text about the detection of higher layer changes.
*
* @param error the error thrown when there are higher layer changes when starting UI Adaptation.
* @param ui5VersionInfo ui5 version info
*/
export async function handleHigherLayerChanges(error: unknown, ui5VersionInfo: Ui5VersionInfo): Promise<void> {
const err = getError(error);
if (err.message.includes('Reload triggered')) {
if (!isLowerThanMinimalUi5Version(ui5VersionInfo, { major: 1, minor: 84 })) {
const bundle = await getTextBundle();
const action = showMessage({
message: bundle.getText('HIGHER_LAYER_CHANGES_INFO_MESSAGE'),
shouldHideIframe: false
});
CommunicationService.sendAction(action);
}

// eslint-disable-next-line fiori-custom/sap-no-location-reload
window.location.reload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ ADP_REUSE_COMPONENTS_MESSAGE = Reuse components are detected for some views in t
CPE_CHANGES_VISIBLE_AFTER_SAVE_AND_RELOAD_MESSAGE = Note: The change will be visible after save and reload.

TABLE_ROWS_NEEDED_TO_CREATE_CUSTOM_COLUMN=At least one table row is required to create new custom column. Make sure the table data is loaded and try again.

HIGHER_LAYER_CHANGES_INFO_MESSAGE=The preview of the project was reloaded due to detected changes in higher layer than the one used in your project.
79 changes: 79 additions & 0 deletions packages/preview-middleware-client/test/unit/flp/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import { fetchMock, sapMock } from 'mock/window';
import type { InitRtaScript, RTAPlugin, StartAdaptation } from 'sap/ui/rta/api/startAdaptation';
import type { Scenario } from '@sap-ux-private/control-property-editor-common';
import VersionInfo from 'mock/sap/ui/VersionInfo';
import { CommunicationService } from '../../../src/cpe/communication-service';

jest.mock('../../../src/i18n', () => {
return {
...jest.requireActual('../../../src/i18n'),
getTextBundle: async () => {
return {
hasText: jest.fn().mockReturnValueOnce(true),
getText: jest
.fn()
.mockReturnValueOnce('The application was reloaded because of changes in a higher layer.')
};
}
};
});

Object.defineProperty(window, 'location', {
value: {
...window.location,
reload: jest.fn()
},
writable: true
});
voicis marked this conversation as resolved.
Show resolved Hide resolved

describe('flp/init', () => {
test('registerSAPFonts', () => {
Expand Down Expand Up @@ -190,10 +213,24 @@ describe('flp/init', () => {
});

describe('init', () => {
const reloadSpy = jest.fn();
const location = window.location;
beforeEach(() => {
sapMock.ushell.Container.attachRendererCreatedEvent.mockReset();
sapMock.ui.require.mockReset();
jest.clearAllMocks();

Object.defineProperty(window, 'location', {
value: {
reload: reloadSpy
}
});
});

afterEach(() => {
Object.defineProperty(window, 'location', {
value: location
});
});

test('nothing configured', async () => {
Expand Down Expand Up @@ -298,5 +335,47 @@ describe('flp/init', () => {

expect(sapMock.ui.require).toBeCalledWith([customInit]);
});

test('handle higher layer changes', async () => {
const flexSettings = {
layer: 'VENDOR',
pluginScript: 'my/script'
};

VersionInfo.load.mockResolvedValueOnce({ name: 'sap.ui.core', version: '1.84.50' });

// Mocking `sap.ui.require` to throw the correct error structure
sapMock.ui.require.mockImplementationOnce((libs, callback) => {
callback(async () => {
throw 'Reload triggered';
}, {});
});

const sendActionSpy = jest.spyOn(CommunicationService, 'sendAction');
await init({ flex: JSON.stringify(flexSettings) });
const rendererCb = sapMock.ushell.Container.attachRendererCreatedEvent.mock
.calls[0][0] as () => Promise<void>;
const mockService = {
attachAppLoaded: jest.fn().mockImplementation((callback) => {
callback({ getParameter: jest.fn() });
})
};
sapMock.ushell.Container.getServiceAsync.mockResolvedValueOnce(mockService);

await rendererCb();

const loadedCb = mockService.attachAppLoaded.mock.calls[0][0] as (event: unknown) => Promise<void>;
await loadedCb({ getParameter: () => {} });

expect(sendActionSpy).toHaveBeenCalled();
expect(sendActionSpy).toHaveBeenNthCalledWith(1, {
type: '[ext] show-dialog-message',
payload: {
message: 'The application was reloaded because of changes in a higher layer.',
shouldHideIframe: false
}
});
expect(reloadSpy).toHaveBeenCalled();
});
});
});
Loading