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 2 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
18 changes: 17 additions & 1 deletion scripts/verify-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@

// This file converts example input into selector utils and checks that the result compiles by Typescript
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const { execSync } = require('child_process');
const convertToSelectorUtil = require(path.join(__dirname, '../lib/converter/dist')).default;

// These test files include utils represented as external dependencies to properly test their conversion to selectors.
// We need to skip them here because they are not expected to compile correctly.
const skip = ['strip-external-imports.ts'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change and everything else in this file seems like too much work to work around this issue.

Alternative options

  1. Can we make that code compile using paths or typeRoots options from tsconfig.
  2. If fixing the typecheck is hard, can we put them into a separate converter-no-typecheck folder (seems easier than writing that much code to skip, right?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option 2 looks like a way to go. Updated


const inputDir = path.join(__dirname, '../src/converter/test/inputs/converter');
const outputDir = path.join(__dirname, '../src/converter/test/outputs/converter');

fs.rmSync(outputDir, { recursive: true, force: true });
fs.mkdirSync(outputDir, { recursive: true });
fs.readdirSync(inputDir).forEach(name => {
if (skip.includes(name)) {
return;
}

const source = fs.readFileSync(path.join(inputDir, name), 'utf-8').trim();
const result = convertToSelectorUtil(source);
fs.writeFileSync(path.join(outputDir, name), result);
});

const includedInputFiles = glob.sync('src/converter/test/inputs/converter/*.ts').filter(file => {
const fileName = file.split('/').pop();
return !skip.includes(fileName);
});

// Command-line API does not allow to use tsconfig when compiling only selected files
// https://github.com/microsoft/TypeScript/issues/27379
execSync(
`tsc --noEmit --strict --experimentalDecorators --target es5 src/converter/test/inputs/converter/*.ts src/converter/test/outputs/converter/*.ts`,
`tsc --noEmit --strict --experimentalDecorators --target es5 ${includedInputFiles.join(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no built-in flag provided by tsc to easily exclude these files.

' '
)} src/converter/test/outputs/converter/*.ts`,
Fixed Show fixed Hide fixed
{
stdio: 'inherit',
}
Expand Down
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
17 changes: 17 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,23 @@ 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";
import ChildWrapper from './simple';
export default class DummyWrapper extends ComponentWrapper {
findElement() {
return new ChildWrapper(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
30 changes: 30 additions & 0 deletions src/converter/test/inputs/converter/strip-external-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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';

import ChildWrapper from './simple';

export default class DummyWrapper extends ComponentWrapper {
findElement(): ChildWrapper {
return new ChildWrapper(this.find('.awsui-child')!.getElement());

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

View workflow job for this annotation

GitHub Actions / build / build

Forbidden non-null assertion
}

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

Check warning on line 17 in src/converter/test/inputs/converter/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