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: use jest-env-ui5 for preview middleware test #2951

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions packages/preview-middleware-client/.babelrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ignore": [
"**/*.d.ts"
],
"presets": [
"@babel/preset-env",
"transform-ui5",
Expand Down
38 changes: 19 additions & 19 deletions packages/preview-middleware-client/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const config = require('../../jest.base');
config.testEnvironment = 'jsdom';
config.moduleNameMapper = {
'^sap/(.+)$': '<rootDir>/test/__mock__/sap/$1.ts',
// Jest will try to load browser version, because environment is set to jsdom, but that is not what we want
// https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28#packagejson-exports
'^@sap-ux/i18n$': require.resolve('@sap-ux/i18n'),
'^mock/(.+)$': '<rootDir>/test/__mock__/$1.ts',
'^open/ux/preview/client/(.+)$': '<rootDir>/src/$1.ts'
};
config.transform = {
'^.+\\.ts$': [
'ts-jest',
{
tsConfig: 'tsconfig.eslint.json'
}
]
};
module.exports = config;
// const config = require('../../jest.base');
// config.testEnvironment = 'jsdom';
// config.moduleNameMapper = {
// '^sap/(.+)$': '<rootDir>/test/__mock__/sap/$1.ts',
// // Jest will try to load browser version, because environment is set to jsdom, but that is not what we want
// // https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28#packagejson-exports
// '^@sap-ux/i18n$': require.resolve('@sap-ux/i18n'),
// '^mock/(.+)$': '<rootDir>/test/__mock__/$1.ts',
// '^open/ux/preview/client/(.+)$': '<rootDir>/src/$1.ts'
// };
// // config.transform = {
// // '^.+\\.ts$': [
// // 'ts-jest',
// // {
// // tsConfig: 'tsconfig.eslint.json'
// // }
// // ]
// // };
// module.exports = config;
24 changes: 24 additions & 0 deletions packages/preview-middleware-client/jest.ui5.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { resolve } = require('path');
const config = require('../../jest.base');
config.testEnvironment = '@sap-ux/jest-environment-ui5';
config.testEnvironmentOptions = {
// mappingStrategy: 'tsconfig',
// configPath: resolve(__dirname, 'tsconfig.eslint.json'),
// rootFolder: __dirname,
configPath: 'ui5-test.yaml',
force: true
};
config.modulePathIgnorePatterns = config.modulePathIgnorePatterns.filter((pattern) => pattern !== '<rootDir>/test/integration');
config.moduleNameMapper = {
// '^sap/(.+)$': '<rootDir>/test/__mock__/sap/$1.ts',
// Jest will try to load browser version, because environment is set to jsdom, but that is not what we want
// https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28#packagejson-exports
'^@sap-ux/i18n$': require.resolve('@sap-ux/i18n'),
'^mock/(.+)$': '<rootDir>/test/__mock__/$1.ts',
// '^sap/(.+)$': '<rootDir>/test/__mock__/sap/$1.ts'
};
config.transform = {
'^.+\\.[t]s?$': ['babel-jest', { configFile: resolve(__dirname, '.babelrc.json') }]
},

module.exports = config;
6 changes: 4 additions & 2 deletions packages/preview-middleware-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"format": "prettier --write '**/*.{js,json,ts,yaml,yml}' --ignore-path ../../.prettierignore",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "jest --ci --forceExit --detectOpenHandles --colors"
"test": "jest --config=jest.ui5.config.js --runInBand --ci --forceExit --detectOpenHandles --colors"
},
"files": [
"dist"
Expand All @@ -35,7 +35,9 @@
"@sap-ux/i18n": "workspace:*",
"@ui5/cli": "3.8.0",
"npm-run-all2": "6.2.0",
"ui5-tooling-transpile": "3.4.0"
"ui5-tooling-transpile": "3.4.0",
"@sap-ux/jest-environment-ui5": "workspace:*",
"babel-jest": "29.7.0"
},
"browserslist": "defaults"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { EnableTableEmptyRowModeQuickAction } from './op-enable-empty-row-mode';
import { AddNewAnnotationFile } from '../common/add-new-annotation-file';
import { EnableObjectPageVariantManagementQuickAction } from './op-enable-variant-management';
import { EnableListReportVariantManagementQuickAction } from './lr-enable-variant-management';
import { isA } from '../../../utils/core';
type PageName = 'listReport' | 'objectPage' | 'analyticalListPage';

const OBJECT_PAGE_TYPE = 'sap.suite.ui.generic.template.ObjectPage.view.Details';
Expand Down Expand Up @@ -104,8 +105,8 @@ export default class FEV2QuickActionRegistry extends QuickActionDefinitionRegist

protected getComponentContainerFromPage(page: Control): ComponentContainer | undefined {
// in ui5 version 1.71 there is no XMLView wrapper around ComponentContainer
const componentContainer = page instanceof XMLView ? page.getContent()[0] : page;
if (componentContainer instanceof ComponentContainer) {
const componentContainer = isA<XMLView>('sap.ui.core.mvc.XMLView', page) ? page.getContent()[0] : page;
if (isA<ComponentContainer>('sap.ui.core.ComponentContainer', componentContainer)) {
return componentContainer;
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ComponentContainer from 'sap/ui/core/ComponentContainer';
import { QuickActionActivationContext, QuickActionDefinitionGroup } from './quick-action-definition';

import type { ControlTreeIndex } from '../types';
import { getControlById } from '../../utils/core';
import { getControlById, isA } from '../../utils/core';
import { getRootControlFromComponentContainer } from '../utils';

const NAV_CONTAINER_CONTROL_TYPE = 'sap.m.NavContainer';
Expand Down Expand Up @@ -46,7 +46,7 @@ export abstract class QuickActionDefinitionRegistry<T extends string> {
* @returns ComponentContainer control.
*/
protected getComponentContainerFromPage(page: Control): ComponentContainer | undefined {
if (page instanceof ComponentContainer) {
if (isA<ComponentContainer>('sap.ui.core.ComponentContainer', page) ) {
return page;
}
return undefined;
Expand Down Expand Up @@ -108,7 +108,7 @@ export abstract class QuickActionDefinitionRegistry<T extends string> {
const navContainerNode = controlIndex[NAV_CONTAINER_CONTROL_TYPE]?.[0];
if (navContainerNode) {
const control = getControlById(navContainerNode.controlId);
if (control instanceof NavContainer) {
if (isA<NavContainer>('sap.m.NavContainer', control)) {
return [control.getCurrentPage()];
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/preview-middleware-client/src/cpe/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ComponentContainer from 'sap/ui/core/ComponentContainer';
import XMLView from 'sap/ui/core/mvc/XMLView';
import UIComponent from 'sap/ui/core/UIComponent';

import { getComponent } from '../utils/core';
import { getComponent, isA } from '../utils/core';
import { isLowerThanMinimalUi5Version, Ui5VersionInfo } from '../utils/version';
import { DesigntimeSetting } from 'sap/ui/dt/DesignTimeMetadata';
import { ChangeService } from './changes';
Expand Down Expand Up @@ -118,9 +118,9 @@ export function getRootControlFromComponentContainer(container?: ComponentContai
if (container) {
const componentId = container.getComponent();
const component = getComponent(componentId);
if (component instanceof UIComponent) {
if (isA<UIComponent>('sap.ui.core.UIComponent', component)) {
const rootControl = component.getRootControl();
if (rootControl instanceof XMLView) {
if (isA<XMLView>('sap.ui.core.mvc.XMLView', rootControl)) {
return rootControl;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// add required functionality for testing here
export default class {
export default class NavContainer {
getCurrentPage = jest.fn();
isA = jest.fn().mockReturnValue(true);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import UIComponentMOck from './UIComponent';

export default class {
export default class ComponentContainer {
getComponent() {
new UIComponentMOck();
};
isA() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default class UIComponentMock {
getRootControl = jest.fn();
isA = jest.fn().mockReturnValue(true);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// add required functionality for testing here
export default class {
export default class XMLView {
isA = jest.fn().mockImplementation((type) => type === 'sap.ui.core.mvc.XMLView')
create = jest.fn();
getContent = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const mockOverlay = {
getElementInstance: jest.fn()
};

export default {
getOverlay: jest.fn().mockReturnValue(mockOverlay)
export default class OverlayRegistry {
getOverlay = jest.fn().mockReturnValue(mockOverlay)
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// add required functionality for testing here
export default {
checkControlId: jest.fn().mockReturnValue(true),
getViewForControl: jest.fn().mockReturnValue({
export default class Utils {
checkControlId = jest.fn().mockReturnValue(true);
static getViewForControl = jest.fn().mockReturnValue({
getId: jest.fn()
}),
getAppComponentForControl: jest.fn()
});
getAppComponentForControl= jest.fn()
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// add required functionality for testing here
export default class CommandFactory {
constructor(_: any) {}

Expand Down
Loading