@sap-ux/jest-environment-ui5 5.3.5 → 5.3.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap-ux/jest-environment-ui5",
3
- "version": "5.3.5",
3
+ "version": "5.3.6",
4
4
  "description": "Jest matchers for files and folders",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,8 +23,8 @@
23
23
  "@ui5/project": "^3.9.0 || ^4.0.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@ui5/project": "^3.9.0",
27
26
  "@ui5/cli": "^3",
27
+ "@ui5/project": "^3.9.0",
28
28
  "cross-env": "^7.0.3",
29
29
  "jest": "^29.7.0"
30
30
  },
@@ -7,9 +7,41 @@ const fs = require('fs');
7
7
  * @param {object} pathMappingFn The path mapping function.
8
8
  * @param {object} shimmedFilePath The shimmed file paths.
9
9
  * @param {object} mockData An object containing file content for the mock.
10
+ * @param {object} XHR The real XMLHttpRequest class.
10
11
  * @returns {object} The fake XMLHttpRequest class.
11
12
  */
12
- function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData) {
13
+ function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData, XHR) {
14
+ let realXhr = new XHR();
15
+
16
+ /**
17
+ * Handles the real XHR send.
18
+ * @param mockXHR The mock XHR object.
19
+ * @param data The data to send.
20
+ */
21
+ function handleRealXHRSend(mockXHR, data) {
22
+ realXhr.addEventListener('load', function () {
23
+ mockXHR.responseText = realXhr.responseText;
24
+ if (mockXHR.listeners['load']) {
25
+ mockXHR.listeners['load']?.({
26
+ status: 200,
27
+ responseText: realXhr.responseText
28
+ });
29
+ } else {
30
+ mockXHR['onload']([]);
31
+ }
32
+ });
33
+ if (mockXHR.onload) {
34
+ realXhr.onload = function () {
35
+ mockXHR.responseText = realXhr.responseText;
36
+ if (mockXHR.onload) {
37
+ mockXHR.onload.apply(realXhr, arguments);
38
+ }
39
+ };
40
+ }
41
+
42
+ realXhr.send(data);
43
+ }
44
+
13
45
  return {
14
46
  /**
15
47
  * Returns true if cross-site Access-Control requests should be made using credentials such as cookies or authorization headers; otherwise false.
@@ -22,19 +54,29 @@ function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData) {
22
54
  * @param {string} url The URL of the request.
23
55
  */
24
56
  open: function (type, url) {
25
- if (url.startsWith('./')) {
26
- this.url = url;
27
- } else if (url.endsWith('.js')) {
28
- this.url = url.substring(0, url.length - 3);
57
+ if (url.startsWith('http')) {
58
+ realXhr.open(type, url);
29
59
  } else {
30
- this.url = url;
60
+ realXhr = undefined;
61
+ if (url.startsWith('./')) {
62
+ this.url = url;
63
+ } else if (url.endsWith('.js')) {
64
+ this.url = url.substring(0, url.length - 3);
65
+ } else {
66
+ this.url = url;
67
+ }
31
68
  }
32
69
  },
33
70
  /**
34
71
  * Sends the request.
35
72
  * If the request is asynchronous (which is the default), this method returns as soon as the request is sent.
73
+ * @param {string} data The data to send.
36
74
  */
37
- send: function () {
75
+ send: function (data) {
76
+ if (realXhr) {
77
+ handleRealXHRSend(this, data);
78
+ return;
79
+ }
38
80
  let fileContent = mockData[this.url];
39
81
  if (fileContent) {
40
82
  this.responseText = fileContent;
@@ -95,6 +137,9 @@ function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData) {
95
137
  * @returns {{}|{"Content-Type": string}|string} The response headers.
96
138
  */
97
139
  getAllResponseHeaders: function () {
140
+ if (realXhr) {
141
+ return realXhr.getAllResponseHeaders();
142
+ }
98
143
  if (this.isXML) {
99
144
  return 'Content-Type: application/xml; Last-Modified: 2019-08-29T00:00:00.000Z;ETag: NotYolow';
100
145
  } else if (this.url.endsWith('json')) {
@@ -111,6 +156,9 @@ function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData) {
111
156
  * @returns {null|string} The response header.
112
157
  */
113
158
  getResponseHeader: function (type) {
159
+ if (realXhr) {
160
+ return realXhr.getResponseHeader(type);
161
+ }
114
162
  if (type === 'Content-Type' && this.url.endsWith('xml')) {
115
163
  return 'application/xml';
116
164
  }
@@ -119,8 +167,14 @@ function createMockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData) {
119
167
  /**
120
168
  * Sets the value of an HTTP request header. You must call setRequestHeader() after open(), but before send().
121
169
  * Useless in our case.
170
+ * @param {string} header The name of the header whose value is to be set.
171
+ * @param {string} value The value to set as the body of the header.
122
172
  */
123
- setRequestHeader: () => {},
173
+ setRequestHeader: (header, value) => {
174
+ if (realXhr) {
175
+ realXhr.setRequestHeader(header, value);
176
+ }
177
+ },
124
178
  /**
125
179
  * Adds an event listener to the XMLHttpRequest object.
126
180
  * @param {string} type The type of event to listen for.
@@ -13,8 +13,9 @@ function initUI5Environment(globalWindow, pathMappingFn, isV2, ui5Version) {
13
13
 
14
14
  globalWindow.jestSetup = true;
15
15
 
16
+ const XHR = globalWindow.XMLHttpRequest;
16
17
  globalWindow.XMLHttpRequest = function () {
17
- return mockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData);
18
+ return mockXHR(globalWindow, pathMappingFn, shimmedFilePath, mockData, XHR);
18
19
  };
19
20
  globalWindow.performance.timing = {
20
21
  fetchStart: Date.now(),
package/src/index.js CHANGED
@@ -23,6 +23,7 @@ class UI5DOMEnvironment extends JSDOMEnvironment {
23
23
  this.testEnvironmentOptions = config.testEnvironmentOptions;
24
24
  this.mappingStrategy = config.testEnvironmentOptions ? config.testEnvironmentOptions.mappingStrategy : 'ui5';
25
25
  this.useOptimized = config.testEnvironmentOptions ? !config.testEnvironmentOptions.useDebugSources : true;
26
+ this.allowCSS = config.testEnvironmentOptions ? config.testEnvironmentOptions.allowCSS : false;
26
27
 
27
28
  // make sure that the test path is in POSIX style:
28
29
  // C:\dir1\dir2 --> /dir1/dir2
@@ -51,8 +52,6 @@ class UI5DOMEnvironment extends JSDOMEnvironment {
51
52
  context.testPath = this.testPath;
52
53
  global.window = context;
53
54
  global.Object = context.Object;
54
- global.CanvasRenderingContext2D = function () {};
55
- context.HTMLCanvasElement.prototype.getContext = () => {};
56
55
  window.NewObject = Object;
57
56
  [
58
57
  'sap',
@@ -70,7 +69,6 @@ class UI5DOMEnvironment extends JSDOMEnvironment {
70
69
  global[keyName] = context[keyName];
71
70
  });
72
71
  window.console = console;
73
- window.CanvasRenderingContext2D = function () {};
74
72
  window.matchMedia = (query) => ({
75
73
  matches: false,
76
74
  media: query,
@@ -114,22 +112,23 @@ class UI5DOMEnvironment extends JSDOMEnvironment {
114
112
  this.core.boot();
115
113
  }
116
114
  return new Promise((resolve) => {
117
- this.initUI5Core(resolve);
115
+ this.initUI5Core(resolve, this.allowCSS);
118
116
  });
119
117
  }
120
118
 
121
119
  /**
122
120
  * Initialize the UI5 Core and resolve once ready.
123
121
  * @param {Function} resolve The function to call once the core is ready
122
+ * @param {boolean} allowCSS Whether to allow the UI5 CSS to be loaded
124
123
  */
125
- initUI5Core(resolve) {
124
+ initUI5Core(resolve, allowCSS = false) {
126
125
  sap.ui.require(['sap/ui/core/Core', 'sap/ui/core/date/Gregorian'], async (Core) => {
127
126
  if (Core.ready) {
128
127
  await Core.ready();
129
- this.overwriteUi5Lib(resolve);
128
+ this.overwriteUi5Lib(resolve, allowCSS);
130
129
  } else {
131
130
  Core.attachInit(() => {
132
- this.overwriteUi5Lib(resolve);
131
+ this.overwriteUi5Lib(resolve, allowCSS);
133
132
  });
134
133
  }
135
134
  });
@@ -138,14 +137,15 @@ class UI5DOMEnvironment extends JSDOMEnvironment {
138
137
  /**
139
138
  * Overwrite the UI5 Lib to disable the library CSS.
140
139
  * @param {Function} resolve the function to call once the lib is overwritten
140
+ * @param {boolean} allowCSS Whether to allow the UI5 CSS to be loaded
141
141
  */
142
- overwriteUi5Lib(resolve) {
142
+ overwriteUi5Lib(resolve, allowCSS) {
143
143
  sap.ui.require(
144
144
  ['sap/ui/core/Lib'],
145
145
  function (Lib) {
146
146
  const fnInit = Lib.init;
147
147
  Lib.init = function (mSettings) {
148
- mSettings.noLibraryCSS = true;
148
+ mSettings.noLibraryCSS = !allowCSS;
149
149
  return fnInit.call(this, mSettings);
150
150
  };
151
151
  resolve();