Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove act from selectors #64

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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,10 +29,15 @@
.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
if (ourWrappers.includes((path.node.superClass as any)?.name) && path.node.superTypeParameters) {

Check warning on line 40 in src/converter/convert-to-selectors.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
path.node.superTypeParameters = null;
}
},
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;
Loading