diff options
Diffstat (limited to 'server/sonar-docs/src/utils/testUtils.ts')
-rw-r--r-- | server/sonar-docs/src/utils/testUtils.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/server/sonar-docs/src/utils/testUtils.ts b/server/sonar-docs/src/utils/testUtils.ts new file mode 100644 index 00000000000..2f1ab920583 --- /dev/null +++ b/server/sonar-docs/src/utils/testUtils.ts @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { ReactWrapper, ShallowWrapper } from 'enzyme'; + +export function click(element: ShallowWrapper | ReactWrapper, event = {}): void { + // `type()` returns a component constructor for a composite element and string for DOM nodes + if (typeof element.type() === 'function') { + element.prop<Function>('onClick')(); + // TODO find out if `root` is a public api + // https://github.com/airbnb/enzyme/blob/master/packages/enzyme/src/ReactWrapper.js#L109 + (element as any).root().update(); + } else { + element.simulate('click', mockEvent(event)); + } +} + +export function mockEvent(overrides = {}) { + return { + target: { blur() {} }, + currentTarget: { blur() {} }, + preventDefault() {}, + stopPropagation() {}, + ...overrides + } as any; +} + +export async function waitAndUpdate(wrapper: ShallowWrapper<any, any> | ReactWrapper<any, any>) { + await new Promise(setImmediate); + wrapper.update(); +} |