diff --git a/src/converter/convert-to-selectors.ts b/src/converter/convert-to-selectors.ts index ce53485..a1c0201 100644 --- a/src/converter/convert-to-selectors.ts +++ b/src/converter/convert-to-selectors.ts @@ -4,6 +4,7 @@ import { transformSync, types, PluginObj, NodePath } from '@babel/core'; const runtimeSelectorsPath = '@cloudscape-design/test-utils-core/selectors'; const ourWrappers = ['ElementWrapper', 'ComponentWrapper']; +const domUtilsPath = '@cloudscape-design/test-utils-core/utils-dom'; interface PluginArguments { types: typeof types; @@ -28,6 +29,11 @@ function selectorUtilsGenerator({ types: t }: PluginArguments): PluginObj { .filter(spec => spec.node.local.name === 'usesDom') .forEach(spec => spec.remove()); } + + // Remove dom utils + if (source.node.value === domUtilsPath) { + path.remove(); + } }, ClassDeclaration(path: NodePath) { // our wrapper classes have generic parameters only in DOM version diff --git a/src/converter/test/__snapshots__/converter.test.ts.snap b/src/converter/test/__snapshots__/converter.test.ts.snap index d9e11da..fd6901a 100644 --- a/src/converter/test/__snapshots__/converter.test.ts.snap +++ b/src/converter/test/__snapshots__/converter.test.ts.snap @@ -45,6 +45,22 @@ export default class DummyWrapper extends ComponentWrapper { }" `; +exports[`strip-external-imports 1`] = ` +"// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/* eslint-env browser */ +import { ComponentWrapper } from "@cloudscape-design/test-utils-core/selectors"; +import { KeyCode } from "@cloudscape-design/test-utils-core/utils"; +export default class DummyWrapper extends ComponentWrapper { + findElement() { + return new ComponentWrapper(this.find('.awsui-child')!.getElement()); + } + findSomething() { + return KeyCode.enter; + } +}" +`; + exports[`strip-imports 1`] = ` "// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 diff --git a/src/converter/test/converter.test.ts b/src/converter/test/converter.test.ts index 5fd5fee..05e243d 100644 --- a/src/converter/test/converter.test.ts +++ b/src/converter/test/converter.test.ts @@ -5,12 +5,17 @@ import path from 'path'; import { test, expect } from 'vitest'; import convertToSelectorUtil from '../index'; -const inputDir = path.join(__dirname, 'inputs', 'converter'); +const runTestsInFolder = (folder: string) => { + const inputDir = path.join(__dirname, 'inputs', folder); -fs.readdirSync(inputDir).forEach(name => { - test(path.basename(name, '.ts'), () => { - const source = fs.readFileSync(path.join(inputDir, name), 'utf-8').trim(); - const result = convertToSelectorUtil(source); - expect(result).toMatchSnapshot(); + fs.readdirSync(inputDir).forEach(name => { + test(path.basename(name, '.ts'), () => { + const source = fs.readFileSync(path.join(inputDir, name), 'utf-8').trim(); + const result = convertToSelectorUtil(source); + expect(result).toMatchSnapshot(); + }); }); -}); +}; + +runTestsInFolder('converter'); +runTestsInFolder('converter-no-typecheck'); diff --git a/src/converter/test/inputs/converter-no-typecheck/strip-external-imports.ts b/src/converter/test/inputs/converter-no-typecheck/strip-external-imports.ts new file mode 100644 index 0000000..e0769d9 --- /dev/null +++ b/src/converter/test/inputs/converter-no-typecheck/strip-external-imports.ts @@ -0,0 +1,28 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +/* eslint-env browser */ +import { ComponentWrapper, usesDom } from '@cloudscape-design/test-utils-core/dom'; +import { KeyCode } from '@cloudscape-design/test-utils-core/utils'; +import { act } from '@cloudscape-design/test-utils-core/utils-dom'; + +export default class DummyWrapper extends ComponentWrapper { + findElement(): ComponentWrapper { + return new ComponentWrapper(this.find('.awsui-child')!.getElement()); + } + + @usesDom + findDomElement(): ComponentWrapper { + return new ComponentWrapper(this.find('.awsui-child')!.getElement()); + } + + @usesDom + openDropdown(): void { + act(() => { + this.findElement().click(); + }); + } + + findSomething(): number { + return KeyCode.enter; + } +} diff --git a/src/core/utils-dom.ts b/src/core/utils-dom.ts new file mode 100644 index 0000000..e1a5ba5 --- /dev/null +++ b/src/core/utils-dom.ts @@ -0,0 +1,9 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Use this file for any utilities specific to React/ReactDOM APIs or those that depend on them. + +import * as React from 'react'; +import { act as reactDomAct } from 'react-dom/test-utils'; + +export const act = ('act' in React ? React.act : reactDomAct) as typeof reactDomAct;