From 98b36f61ac256ff2de79c4cd00e57e5dcdc06a66 Mon Sep 17 00:00:00 2001 From: Amir Alami Date: Mon, 2 Dec 2024 11:40:09 +0100 Subject: [PATCH] chore: Enhances the usage of converter --- src/converter/generate-test-utils.ts | 35 +++++++++++-------- src/converter/interfaces.ts | 6 ++++ .../test/generate-test-utils.test.tsx | 8 +++-- .../dom/test-component-b/child-wrapper.ts | 11 ++++++ .../dom/test-component-b/index.ts | 5 +-- 5 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 src/converter/test/mock-test-utils/dom/test-component-b/child-wrapper.ts diff --git a/src/converter/generate-test-utils.ts b/src/converter/generate-test-utils.ts index 3ef2e9c..e883003 100644 --- a/src/converter/generate-test-utils.ts +++ b/src/converter/generate-test-utils.ts @@ -5,32 +5,38 @@ import fs from 'fs'; import { generateComponentFinders } from './generate-component-finders'; import { ComponentWrapperMetadata, GenerateTestUtilsParams, TestUtilType } from './interfaces'; import { writeSourceFile } from './utils'; -import lodash from 'lodash'; +import { kebabCase } from 'lodash'; import { convertToSelectorUtil } from './convert-to-selectors'; +import glob from 'glob'; interface GenerateIndexFilesParams extends GenerateTestUtilsParams { testUtilType: TestUtilType; } function generateIndexFile({ testUtilsPath, components, testUtilType }: GenerateIndexFilesParams) { - const componenWrappersMetadata: ComponentWrapperMetadata[] = components.map(({ name, pluralName }) => ({ - name, - pluralName, - wrapperName: `${name}Wrapper`, - wrapperImportPath: `./${lodash.kebabCase(name)}/index`, - })); + const componenWrappersMetadata: ComponentWrapperMetadata[] = components.map( + ({ name, pluralName, testUtilsFolderName }) => ({ + name, + pluralName, + wrapperName: `${name}Wrapper`, + wrapperImportPath: `./${testUtilsFolderName ?? kebabCase(name)}/index`, + }) + ); const content = generateComponentFinders({ testUtilType, components: componenWrappersMetadata }); const indexFilePath = path.join(testUtilsPath, testUtilType, 'index.ts'); writeSourceFile(indexFilePath, content); } -function generateSelectorUtils({ components, testUtilsPath }: GenerateTestUtilsParams) { - for (const component of components) { - const componentNameKebabCase = lodash.kebabCase(component.name); - const domFilePath = path.join(testUtilsPath, `dom/${componentNameKebabCase}/index.ts`); +function generateSelectorUtils(testUtilsPath: string) { + const domFolderPath = path.join(testUtilsPath, 'dom'); + const selectorsFolderPath = path.join(testUtilsPath, 'selectors'); + const conversionTargetRelativePaths = glob.sync(`**/*.{ts,tsx}`, { cwd: domFolderPath }); + + for (const fileRelativePath of conversionTargetRelativePaths) { + const domFilePath = path.join(domFolderPath, fileRelativePath); const domFileContent = fs.readFileSync(domFilePath, 'utf-8'); - const selectorsFilePath = path.join(testUtilsPath, `selectors/${componentNameKebabCase}/index.ts`); + const selectorsFilePath = path.join(selectorsFolderPath, fileRelativePath); const selectorsFileContent = convertToSelectorUtil(domFileContent); if (!selectorsFileContent) { @@ -41,11 +47,10 @@ function generateSelectorUtils({ components, testUtilsPath }: GenerateTestUtilsP } /** - * Generates test utils index files for dom and selector. - * Converts the test utils dom test utils to selectors. + * Generates test utils index files for dom and selector and converts the dom test utils to selectors. */ export function generateTestUtils({ components, testUtilsPath }: GenerateTestUtilsParams) { - generateSelectorUtils({ components, testUtilsPath }); + generateSelectorUtils(testUtilsPath); generateIndexFile({ components, testUtilsPath, testUtilType: 'dom' }); generateIndexFile({ components, testUtilsPath, testUtilType: 'selectors' }); } diff --git a/src/converter/interfaces.ts b/src/converter/interfaces.ts index fd7bb3e..6e1ce8f 100644 --- a/src/converter/interfaces.ts +++ b/src/converter/interfaces.ts @@ -15,6 +15,12 @@ export interface ComponentMetadata { * Examples: Buttons, Alerts, ButtonDropdowns */ pluralName: string; + + /** + * Folder name of the component test utils. + * If not specified, the kebab case of the component name will be used by default. + */ + testUtilsFolderName?: string; } export interface GenerateTestUtilsParams { diff --git a/src/converter/test/generate-test-utils.test.tsx b/src/converter/test/generate-test-utils.test.tsx index 1907cea..ae36a6f 100644 --- a/src/converter/test/generate-test-utils.test.tsx +++ b/src/converter/test/generate-test-utils.test.tsx @@ -12,13 +12,17 @@ function renderTestNode() {

First Component A

-

First Component B

+
+

First Component B

+

Second Component A

-

Second Component B

+
+

Second Component B

+
); diff --git a/src/converter/test/mock-test-utils/dom/test-component-b/child-wrapper.ts b/src/converter/test/mock-test-utils/dom/test-component-b/child-wrapper.ts new file mode 100644 index 0000000..9f72569 --- /dev/null +++ b/src/converter/test/mock-test-utils/dom/test-component-b/child-wrapper.ts @@ -0,0 +1,11 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { ComponentWrapper, createWrapper } from '@cloudscape-design/test-utils-core/dom'; + +export class ChildWrapper extends ComponentWrapper { + static rootSelector = 'test-component-b-child'; + + findContent() { + return createWrapper().find('.test-component-b-child-content'); + } +} diff --git a/src/converter/test/mock-test-utils/dom/test-component-b/index.ts b/src/converter/test/mock-test-utils/dom/test-component-b/index.ts index 52b068c..4fecf06 100644 --- a/src/converter/test/mock-test-utils/dom/test-component-b/index.ts +++ b/src/converter/test/mock-test-utils/dom/test-component-b/index.ts @@ -1,11 +1,12 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { ComponentWrapper, createWrapper } from '@cloudscape-design/test-utils-core/dom'; +import { ChildWrapper } from './child-wrapper'; export default class TestComponentBWrapper extends ComponentWrapper { static rootSelector = 'test-component-b-root'; - findChild() { - return createWrapper().find('.test-component-b-child'); + findChild(): ChildWrapper { + return createWrapper().find(`.${ChildWrapper.rootSelector}`); } }