@luigi-project/testing-utilities 2.8.1-dev.20240290024 → 2.9.1-dev.202402191417

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,120 +15,123 @@ export class LuigiMockEngine {
15
15
  static initPostMessageHook() {
16
16
  return async () => {
17
17
  // Check if Luigi Client is running standalone
18
- if (window.parent === window) {
19
- console.debug('Detected standalone mode');
20
- // Check and skip if Luigi environment is already mocked
21
- if (window.luigiMockEnvironment) {
22
- return;
23
- }
24
- // mock target origin
25
- if (window.LuigiClient) {
26
- window.LuigiClient.setTargetOrigin('*');
27
- }
28
- window.luigiMockEnvironment = {
29
- msgListener: function (e) {
30
- if (e.data.msg && (e.data.msg.startsWith('luigi.') || e.data.msg === 'storage')) {
31
- console.debug('Luigi msg', e.data);
32
- if (e.data.msg === 'luigi.get-context') {
33
- window.postMessage({
34
- msg: 'luigi.init',
35
- emulated: true,
36
- internal: {
37
- viewStackSize: 1
38
- },
39
- context: e.data.context
40
- }, '*');
41
- }
42
- // vizualise retrieved event data
43
- LuigiMockEngine.visualize(JSON.stringify(e.data));
44
- // Check and run mocked callback if it exists
45
- const mockListener = window.luigiMockEnvironment.mockListeners[e.data.msg];
46
- if (mockListener) {
47
- mockListener(e);
48
- }
49
- }
50
- },
51
- mockListeners: {
52
- 'luigi.navigation.pathExists': (event) => {
53
- const mockData = window.sessionStorage.getItem('luigiMockData');
54
- let mockDataParsed = mockData ? JSON.parse(mockData) : undefined;
55
- const inputPath = event.data.data.link;
56
- const pathExists = mockDataParsed && mockDataParsed.pathExists && mockDataParsed.pathExists[inputPath];
57
- const response = {
58
- msg: 'luigi.navigation.pathExists.answer',
59
- data: {
60
- correlationId: event.data.data.id,
61
- pathExists: pathExists ? pathExists : false
18
+ console.log('luigiMockModule sapluigi testing-utitlities');
19
+ // if (window.parent === window) {
20
+ console.debug('Detected standalone mode');
21
+ // Check and skip if Luigi environment is already mocked
22
+ if (window.luigiMockEnvironment) {
23
+ return;
24
+ }
25
+ // mock target origin
26
+ console.log('testing utils', window.LuigiClient);
27
+ if (window.LuigiClient) {
28
+ window.LuigiClient.setTargetOrigin('*');
29
+ }
30
+ window.luigiMockEnvironment = {
31
+ msgListener: function (e) {
32
+ console.log('testing utils msg starts with luigi. or storage', e.data.msg && (e.data.msg.startsWith('luigi.') || e.data.msg === 'storage'));
33
+ if (e.data.msg && (e.data.msg.startsWith('luigi.') || e.data.msg === 'storage')) {
34
+ console.debug('Luigi msg', e.data);
35
+ if (e.data.msg === 'luigi.get-context') {
36
+ window.postMessage({
37
+ msg: 'luigi.init',
38
+ emulated: true,
39
+ internal: {
40
+ viewStackSize: 1
62
41
  },
63
- emulated: true
64
- };
65
- window.postMessage(response, '*');
66
- },
67
- //ux
68
- 'luigi.ux.confirmationModal.show': (event) => {
69
- const response = {
70
- msg: 'luigi.ux.confirmationModal.hide',
71
- data: event.data,
72
- emulated: true
73
- };
74
- window.postMessage(response, '*');
75
- },
76
- 'luigi.ux.alert.show': (event) => {
77
- const response = {
78
- msg: 'luigi.ux.alert.hide',
79
- data: event.data,
80
- emulated: true
81
- };
82
- window.postMessage(response, '*');
83
- },
84
- 'luigi.ux.set-current-locale': (event) => {
85
- const response = {
86
- msg: 'luigi.current-locale-changed',
87
- currentLocale: event.data.data.currentLocale,
88
- emulated: true
89
- };
90
- window.postMessage(response, '*');
91
- },
92
- // linkManager
93
- 'luigi.navigation.open': (event) => {
94
- const response = {
95
- msg: 'luigi.navigate.ok',
96
- data: event.data,
97
- emulated: true
98
- };
99
- window.postMessage(response, '*');
100
- },
101
- 'luigi.navigation.splitview.close': (event) => {
102
- const response = {
103
- msg: 'luigi.navigate.ok',
104
- data: event.data,
105
- emulated: true
106
- };
107
- window.postMessage(response, '*');
108
- },
109
- 'luigi.navigation.splitview.collapse': (event) => {
110
- const response = {
111
- msg: 'luigi.navigate.ok',
112
- data: event.data,
113
- emulated: true
114
- };
115
- window.postMessage(response, '*');
116
- },
117
- 'luigi.navigation.splitview.expand': (event) => {
118
- const response = {
119
- msg: 'luigi.navigate.ok',
120
- data: event.data,
121
- emulated: true
122
- };
123
- window.postMessage(response, '*');
124
- },
125
- // storage
126
- storage: () => { }
42
+ context: e.data.context
43
+ }, '*');
44
+ }
45
+ // vizualise retrieved event data
46
+ LuigiMockEngine.visualize(JSON.stringify(e.data));
47
+ // Check and run mocked callback if it exists
48
+ const mockListener = window.luigiMockEnvironment.mockListeners[e.data.msg];
49
+ if (mockListener) {
50
+ mockListener(e);
51
+ }
127
52
  }
128
- };
129
- // Listen to the global 'message' event of the window object
130
- window.addEventListener('message', window.luigiMockEnvironment.msgListener);
131
- }
53
+ },
54
+ mockListeners: {
55
+ 'luigi.navigation.pathExists': (event) => {
56
+ const mockData = window.sessionStorage.getItem('luigiMockData');
57
+ let mockDataParsed = mockData ? JSON.parse(mockData) : undefined;
58
+ const inputPath = event.data.data.link;
59
+ const pathExists = mockDataParsed && mockDataParsed.pathExists && mockDataParsed.pathExists[inputPath];
60
+ const response = {
61
+ msg: 'luigi.navigation.pathExists.answer',
62
+ data: {
63
+ correlationId: event.data.data.id,
64
+ pathExists: pathExists ? pathExists : false
65
+ },
66
+ emulated: true
67
+ };
68
+ window.postMessage(response, '*');
69
+ },
70
+ //ux
71
+ 'luigi.ux.confirmationModal.show': (event) => {
72
+ const response = {
73
+ msg: 'luigi.ux.confirmationModal.hide',
74
+ data: event.data,
75
+ emulated: true
76
+ };
77
+ window.postMessage(response, '*');
78
+ },
79
+ 'luigi.ux.alert.show': (event) => {
80
+ const response = {
81
+ msg: 'luigi.ux.alert.hide',
82
+ data: event.data,
83
+ emulated: true
84
+ };
85
+ window.postMessage(response, '*');
86
+ },
87
+ 'luigi.ux.set-current-locale': (event) => {
88
+ const response = {
89
+ msg: 'luigi.current-locale-changed',
90
+ currentLocale: event.data.data.currentLocale,
91
+ emulated: true
92
+ };
93
+ window.postMessage(response, '*');
94
+ },
95
+ // linkManager
96
+ 'luigi.navigation.open': (event) => {
97
+ const response = {
98
+ msg: 'luigi.navigate.ok',
99
+ data: event.data,
100
+ emulated: true
101
+ };
102
+ window.postMessage(response, '*');
103
+ },
104
+ 'luigi.navigation.splitview.close': (event) => {
105
+ const response = {
106
+ msg: 'luigi.navigate.ok',
107
+ data: event.data,
108
+ emulated: true
109
+ };
110
+ window.postMessage(response, '*');
111
+ },
112
+ 'luigi.navigation.splitview.collapse': (event) => {
113
+ const response = {
114
+ msg: 'luigi.navigate.ok',
115
+ data: event.data,
116
+ emulated: true
117
+ };
118
+ window.postMessage(response, '*');
119
+ },
120
+ 'luigi.navigation.splitview.expand': (event) => {
121
+ const response = {
122
+ msg: 'luigi.navigate.ok',
123
+ data: event.data,
124
+ emulated: true
125
+ };
126
+ window.postMessage(response, '*');
127
+ },
128
+ // storage
129
+ storage: () => { }
130
+ }
131
+ };
132
+ // Listen to the global 'message' event of the window object
133
+ window.addEventListener('message', window.luigiMockEnvironment.msgListener);
134
+ // }
132
135
  };
133
136
  }
134
137
  /*
@@ -4,6 +4,7 @@ export declare class LuigiMockUtil {
4
4
  constructor(browser: any);
5
5
  /**
6
6
  * Parses the elements added by LuigiMockModule into the DOM and assigns them to the local this.messages variable
7
+ * @returns {Promise<void>} - A Promise that resolves when parsing is complete.
7
8
  */
8
9
  parseLuigiMockedMessages(): Promise<void>;
9
10
  /**
@@ -5,9 +5,20 @@ export class LuigiMockUtil {
5
5
  * @param mockContext an object representing the context to be mocked
6
6
  */
7
7
  this.mockContext = (mockContext) => {
8
- this.browser.executeScript((mockContext) => {
9
- globalThis.postMessage({ msg: 'luigi.get-context', context: mockContext }, '*');
10
- }, mockContext);
8
+ const postMessageToLuigi = (context) => {
9
+ globalThis.postMessage({ msg: 'luigi.get-context', context }, '*');
10
+ };
11
+ try {
12
+ if (this.browser.executeScript) {
13
+ this.browser.executeScript(postMessageToLuigi, mockContext);
14
+ }
15
+ else {
16
+ this.browser(postMessageToLuigi, mockContext);
17
+ }
18
+ }
19
+ catch (e) {
20
+ console.debug('Failed to mock context: ', e);
21
+ }
11
22
  };
12
23
  /**
13
24
  * This method serves as a mock for the luigi client pathExists() function.
@@ -26,7 +37,13 @@ export class LuigiMockUtil {
26
37
  *
27
38
  */
28
39
  this.mockPathExists = (path, exists) => {
29
- this.browser.executeScript((path, exists) => {
40
+ /**
41
+ * Sets the path exists mock data in sessionStorage.
42
+ * @param {string} path - The path for which mock data is to be set.
43
+ * @param {boolean} exists - Boolean indicating whether the path exists.
44
+ * @returns {void}
45
+ */
46
+ const setPathExistsMockData = (path, exists) => {
30
47
  globalThis.sessionStorage.clear();
31
48
  let pathExistsMockData = {
32
49
  pathExists: {
@@ -34,17 +51,32 @@ export class LuigiMockUtil {
34
51
  }
35
52
  };
36
53
  globalThis.sessionStorage.setItem('luigiMockData', JSON.stringify(pathExistsMockData));
37
- }, path, exists);
54
+ };
55
+ try {
56
+ if (this.browser.executeScript) {
57
+ this.browser.executeScript(setPathExistsMockData, path, exists);
58
+ }
59
+ else {
60
+ this.browser(setPathExistsMockData, path, exists);
61
+ }
62
+ }
63
+ catch (e) {
64
+ console.debug('Failed to mock path exists: ', e);
65
+ }
38
66
  };
39
67
  this.messages = [];
40
68
  this.browser = browser;
41
69
  }
42
70
  /**
43
71
  * Parses the elements added by LuigiMockModule into the DOM and assigns them to the local this.messages variable
72
+ * @returns {Promise<void>} - A Promise that resolves when parsing is complete.
44
73
  */
45
74
  async parseLuigiMockedMessages() {
46
75
  try {
47
- const textElements = await this.browser.executeScript(() => Array.from(document.getElementById('luigi-debug-vis-cnt').childNodes).map(item => item.textContent));
76
+ const getTextNodeValues = async () => Array.from(document.getElementById('luigi-debug-vis-cnt').childNodes).map((item) => item.textContent);
77
+ const textElements = this.browser.executeScript
78
+ ? await this.browser.executeScript(getTextNodeValues)
79
+ : await this.browser(getTextNodeValues);
48
80
  this.messages = textElements
49
81
  .map((item) => {
50
82
  try {
@@ -54,7 +86,7 @@ export class LuigiMockUtil {
54
86
  return undefined;
55
87
  }
56
88
  })
57
- .filter(item => item !== undefined);
89
+ .filter((item) => item !== undefined);
58
90
  }
59
91
  catch (e) {
60
92
  console.debug('Failed to parse luigi mocked messages: ', e);
package/package.json CHANGED
@@ -2,21 +2,11 @@
2
2
  "name": "@luigi-project/testing-utilities",
3
3
  "description": "Luigi testing utilities for standalone testing of microfrontends",
4
4
  "license": "Apache-2.0",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "build": "tsc && npm run copy",
9
- "copy": "ncp package.json ./dist/package.json && ncp README.md ./dist/README.md",
10
- "bundle": "npm run build"
11
- },
12
- "types": "dist/index.d.ts",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
13
7
  "publishConfig": {
14
8
  "tag": "testing-utilities"
15
9
  },
16
- "devDependencies": {
17
- "ncp": "^2.0.0",
18
- "typescript": "4.8.2"
19
- },
20
10
  "repository": {
21
11
  "type": "git",
22
12
  "url": "ssh://github.com/SAP/luigi.git"
@@ -29,7 +19,7 @@
29
19
  "microfrontends",
30
20
  "testing"
31
21
  ],
32
- "version": "2.8.1-dev.20240290024",
22
+ "version": "2.9.1-dev.202402191417",
33
23
  "engines": {
34
24
  "node": ">=18"
35
25
  }