ng-zorro-antd-core-testing.mjs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import * as i0 from '@angular/core';
  2. import { NgZone, EventEmitter, Injectable, NO_ERRORS_SCHEMA } from '@angular/core';
  3. import { CommonModule } from '@angular/common';
  4. import { TestBed } from '@angular/core/testing';
  5. import { NoopAnimationsModule } from '@angular/platform-browser/animations';
  6. /**
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  9. */
  10. /** Creates a browser MouseEvent with the specified options. */
  11. function createMouseEvent(type, x = 0, y = 0, button = 0) {
  12. const event = document.createEvent('MouseEvent');
  13. event.initMouseEvent(type, true /* canBubble */, false /* cancelable */, window /* view */, 0 /* detail */, x /* screenX */, y /* screenY */, x /* clientX */, y /* clientY */, false /* ctrlKey */, false /* altKey */, false /* shiftKey */, false /* metaKey */, button /* button */, null /* relatedTarget */);
  14. // `initMouseEvent` doesn't allow us to pass the `buttons` and
  15. // defaults it to 0 which looks like a fake event.
  16. Object.defineProperty(event, 'buttons', { get: () => 1 });
  17. return event;
  18. }
  19. /** Creates a browser TouchEvent with the specified pointer coordinates. */
  20. function createTouchEvent(type, pageX = 0, pageY = 0) {
  21. // In favor of creating events that work for most of the browsers, the event is created
  22. // as a basic UI Event. The necessary details for the event will be set manually.
  23. const event = new UIEvent(type, { detail: 0, view: window });
  24. const touchDetails = { pageX, pageY, clientX: pageX, clientY: pageY };
  25. // Most of the browsers don't have a "initTouchEvent" method that can be used to define
  26. // the touch details.
  27. Object.defineProperties(event, {
  28. touches: { value: [touchDetails] },
  29. targetTouches: { value: [touchDetails] },
  30. changedTouches: { value: [touchDetails] }
  31. });
  32. return event;
  33. }
  34. /** Dispatches a keydown event from an element. */
  35. function createKeyboardEvent(type, keyCode, target, key, ctrlKey, metaKey, shiftKey) {
  36. const event = document.createEvent('KeyboardEvent');
  37. const originalPreventDefault = event.preventDefault;
  38. // Firefox does not support `initKeyboardEvent`, but supports `initKeyEvent`.
  39. if (event.initKeyEvent) {
  40. event.initKeyEvent(type, true, true, window, 0, 0, 0, 0, 0, keyCode);
  41. }
  42. else {
  43. event.initKeyboardEvent(type, true, true, window, 0, key, 0, '', false);
  44. }
  45. // Webkit Browsers don't set the keyCode when calling the init function.
  46. // See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
  47. Object.defineProperties(event, {
  48. keyCode: { get: () => keyCode },
  49. key: { get: () => key },
  50. target: { get: () => target },
  51. ctrlKey: { get: () => ctrlKey },
  52. metaKey: { get: () => metaKey },
  53. shiftKey: { get: () => shiftKey }
  54. });
  55. // IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
  56. event.preventDefault = function () {
  57. Object.defineProperty(event, 'defaultPrevented', { get: () => true, configurable: true });
  58. // eslint-disable-next-line prefer-rest-params
  59. return originalPreventDefault.apply(this, arguments);
  60. };
  61. return event;
  62. }
  63. /** Creates a fake event object with any desired event type. */
  64. function createFakeEvent(type, canBubble = true, cancelable = true) {
  65. const event = document.createEvent('Event');
  66. event.initEvent(type, canBubble, cancelable);
  67. return event;
  68. }
  69. /**
  70. * Use of this source code is governed by an MIT-style license that can be
  71. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  72. */
  73. /** Utility to dispatch any event on a Node. */
  74. function dispatchEvent(node, event) {
  75. node.dispatchEvent(event);
  76. return event;
  77. }
  78. /** Shorthand to dispatch a fake event on a specified node. */
  79. function dispatchFakeEvent(node, type, canBubble) {
  80. return dispatchEvent(node, createFakeEvent(type, canBubble));
  81. }
  82. /** Shorthand to dispatch a keyboard event with a specified key code. */
  83. function dispatchKeyboardEvent(node, type, keyCode, target) {
  84. return dispatchEvent(node, createKeyboardEvent(type, keyCode, target));
  85. }
  86. /** Shorthand to dispatch a mouse event on the specified coordinates. */
  87. function dispatchMouseEvent(node, type, x = 0, y = 0, event = createMouseEvent(type, x, y)) {
  88. return dispatchEvent(node, event);
  89. }
  90. /** Shorthand to dispatch a touch event on the specified coordinates. */
  91. function dispatchTouchEvent(node, type, x = 0, y = 0) {
  92. return dispatchEvent(node, createTouchEvent(type, x, y));
  93. }
  94. /**
  95. * Use of this source code is governed by an MIT-style license that can be
  96. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  97. */
  98. /**
  99. * Focuses an input, sets its value and dispatches
  100. * the `input` event, simulating the user typing.
  101. *
  102. * @param value Value to be set on the input.
  103. * @param element Element onto which to set the value.
  104. */
  105. function typeInElement(value, element) {
  106. element.focus();
  107. element.value = value;
  108. dispatchFakeEvent(element, 'input');
  109. }
  110. /**
  111. * Use of this source code is governed by an MIT-style license that can be
  112. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  113. */
  114. /**
  115. * Gets a RegExp used to detect an angular wrapped error message.
  116. * See https://github.com/angular/angular/issues/8348
  117. *
  118. * @internal
  119. * @deprecated Internal use only, do not use directly. Will be removed in v20
  120. */
  121. function wrappedErrorMessage(e) {
  122. const escapedMessage = e.message.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
  123. return new RegExp(escapedMessage);
  124. }
  125. /**
  126. * Use of this source code is governed by an MIT-style license that can be
  127. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  128. */
  129. /**
  130. * @internal
  131. * @deprecated Internal use only, do not use directly. Will be removed in v20
  132. */
  133. class FakeViewportRuler {
  134. getViewportRect() {
  135. return {
  136. left: 0,
  137. top: 0,
  138. width: 1014,
  139. height: 686,
  140. bottom: 686,
  141. right: 1014
  142. };
  143. }
  144. getViewportSize() {
  145. return { width: 1014, height: 686 };
  146. }
  147. getViewportScrollPosition() {
  148. return { top: 0, left: 0 };
  149. }
  150. }
  151. /**
  152. * Use of this source code is governed by an MIT-style license that can be
  153. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  154. */
  155. /**
  156. * Mock synchronous NgZone implementation that can be used
  157. * to flush out `onStable` subscriptions in tests.
  158. *
  159. * via: https://github.com/angular/angular/blob/master/packages/core/testing/src/ng_zone_mock.ts
  160. *
  161. * @docs-private
  162. */
  163. class MockNgZone extends NgZone {
  164. onStable = new EventEmitter(false);
  165. constructor() {
  166. super({ enableLongStackTrace: false });
  167. }
  168. run(fn) {
  169. return fn();
  170. }
  171. runOutsideAngular(fn) {
  172. return fn();
  173. }
  174. simulateZoneExit() {
  175. this.onStable.emit(null);
  176. }
  177. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: MockNgZone, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  178. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: MockNgZone });
  179. }
  180. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: MockNgZone, decorators: [{
  181. type: Injectable
  182. }], ctorParameters: () => [] });
  183. /**
  184. * Use of this source code is governed by an MIT-style license that can be
  185. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  186. */
  187. /**
  188. * @internal
  189. * @deprecated Internal use only, do not use directly. Will be removed in v20
  190. */
  191. function createComponentBed(component, options = {
  192. providers: [],
  193. declarations: [],
  194. imports: []
  195. }) {
  196. const { imports, declarations, providers } = options;
  197. const config = {
  198. imports: [NoopAnimationsModule, CommonModule, ...(imports || [])],
  199. declarations: [component, ...(declarations || [])],
  200. schemas: [NO_ERRORS_SCHEMA],
  201. providers: providers || []
  202. };
  203. const bed = TestBed.configureTestingModule(config);
  204. const fixture = TestBed.createComponent(component);
  205. fixture.detectChanges();
  206. return {
  207. bed,
  208. fixture,
  209. nativeElement: fixture.nativeElement,
  210. debugElement: fixture.debugElement,
  211. component: fixture.componentInstance
  212. };
  213. }
  214. /**
  215. * Use of this source code is governed by an MIT-style license that can be
  216. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  217. */
  218. /**
  219. * Generated bundle index. Do not edit.
  220. */
  221. export { FakeViewportRuler, MockNgZone, createFakeEvent, createKeyboardEvent, createMouseEvent, createTouchEvent, dispatchEvent, dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent, typeInElement, wrappedErrorMessage, createComponentBed as ɵcreateComponentBed };
  222. //# sourceMappingURL=ng-zorro-antd-core-testing.mjs.map