Skip to content

Commit

Permalink
fix: Remove act from selectors (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgylobko authored Aug 1, 2024
1 parent c19106e commit 574619a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/converter/convert-to-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<types.ClassDeclaration>) {
// our wrapper classes have generic parameters only in DOM version
Expand Down
16 changes: 16 additions & 0 deletions src/converter/test/__snapshots__/converter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions src/converter/test/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
@@ -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());

Check warning on line 10 in src/converter/test/inputs/converter-no-typecheck/strip-external-imports.ts

View workflow job for this annotation

GitHub Actions / build / build

Forbidden non-null assertion
}

@usesDom
findDomElement(): ComponentWrapper {
return new ComponentWrapper(this.find('.awsui-child')!.getElement());

Check warning on line 15 in src/converter/test/inputs/converter-no-typecheck/strip-external-imports.ts

View workflow job for this annotation

GitHub Actions / build / build

Forbidden non-null assertion
}

@usesDom
openDropdown(): void {
act(() => {
this.findElement().click();
});
}

findSomething(): number {
return KeyCode.enter;
}
}
9 changes: 9 additions & 0 deletions src/core/utils-dom.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 574619a

Please sign in to comment.