You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testUtils.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { ShallowWrapper, ReactWrapper } from 'enzyme';
  21. import { InjectedRouter } from 'react-router';
  22. export const mockEvent = {
  23. target: { blur() {} },
  24. currentTarget: { blur() {} },
  25. preventDefault() {},
  26. stopPropagation() {}
  27. };
  28. export function click(element: ShallowWrapper | ReactWrapper, event = {}): void {
  29. // `type()` returns a component constructor for a composite element and string for DOM nodes
  30. if (typeof element.type() === 'function') {
  31. element.prop<Function>('onClick')();
  32. // TODO find out if `root` is a public api
  33. // https://github.com/airbnb/enzyme/blob/master/packages/enzyme/src/ReactWrapper.js#L109
  34. (element as any).root().update();
  35. } else {
  36. element.simulate('click', { ...mockEvent, ...event });
  37. }
  38. }
  39. export function clickOutside(event = {}): void {
  40. const dispatchedEvent = new MouseEvent('click', event);
  41. window.dispatchEvent(dispatchedEvent);
  42. }
  43. export function submit(element: ShallowWrapper | ReactWrapper): void {
  44. element.simulate('submit', {
  45. preventDefault() {}
  46. });
  47. }
  48. export function change(element: ShallowWrapper | ReactWrapper, value: string, event = {}): void {
  49. // `type()` returns a component constructor for a composite element and string for DOM nodes
  50. if (typeof element.type() === 'function') {
  51. element.prop<Function>('onChange')(value);
  52. // TODO find out if `root` is a public api
  53. // https://github.com/airbnb/enzyme/blob/master/packages/enzyme/src/ReactWrapper.js#L109
  54. (element as any).root().update();
  55. } else {
  56. element.simulate('change', {
  57. target: { value },
  58. currentTarget: { value },
  59. ...event
  60. });
  61. }
  62. }
  63. export function keydown(keyCode: number): void {
  64. const event = new KeyboardEvent('keydown', { keyCode } as KeyboardEventInit);
  65. document.dispatchEvent(event);
  66. }
  67. export function elementKeydown(element: ShallowWrapper, keyCode: number): void {
  68. const event = {
  69. currentTarget: { element },
  70. keyCode,
  71. preventDefault() {}
  72. };
  73. if (typeof element.type() === 'string') {
  74. // `type()` is string for native dom elements
  75. element.simulate('keydown', event);
  76. } else {
  77. element.prop<Function>('onKeyDown')(event);
  78. }
  79. }
  80. export function resizeWindowTo(width?: number, height?: number) {
  81. // `document.body.clientWidth/clientHeight` are getters by default, so we need to redefine them
  82. // pass `configurable: true` to allow to redefine the properties multiple times
  83. if (width) {
  84. Object.defineProperty(document.body, 'clientWidth', { configurable: true, value: width });
  85. }
  86. if (height) {
  87. Object.defineProperty(document.body, 'clientHeight', { configurable: true, value: height });
  88. }
  89. const resizeEvent = new Event('resize');
  90. window.dispatchEvent(resizeEvent);
  91. }
  92. export function doAsync(fn?: Function): Promise<void> {
  93. return new Promise(resolve => {
  94. setImmediate(() => {
  95. if (fn) {
  96. fn();
  97. }
  98. resolve();
  99. });
  100. });
  101. }
  102. export async function waitAndUpdate(wrapper: ShallowWrapper<any, any> | ReactWrapper<any, any>) {
  103. await new Promise(setImmediate);
  104. wrapper.update();
  105. }
  106. export function mockRouter(overrides: { push?: Function; replace?: Function } = {}) {
  107. return {
  108. createHref: jest.fn(),
  109. createPath: jest.fn(),
  110. go: jest.fn(),
  111. goBack: jest.fn(),
  112. goForward: jest.fn(),
  113. isActive: jest.fn(),
  114. push: jest.fn(),
  115. replace: jest.fn(),
  116. setRouteLeaveHook: jest.fn(),
  117. ...overrides
  118. } as InjectedRouter;
  119. }