|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { describe, test, expect } from 'vitest'; |
| 4 | +import { generateComponentFinders } from '../generate-component-finders'; |
| 5 | +import { ComponentWrapperMetadata } from '../interfaces'; |
| 6 | + |
| 7 | +const mockComponents: ComponentWrapperMetadata[] = [ |
| 8 | + { |
| 9 | + name: 'Alert', |
| 10 | + pluralName: 'Alerts', |
| 11 | + wrapperName: 'AlertWrapper', |
| 12 | + wrapperImportPath: './test-utils/AlertWrapper', |
| 13 | + testUtilsFolderName: '../test-utils', |
| 14 | + }, |
| 15 | + { |
| 16 | + name: 'Status', |
| 17 | + pluralName: 'Status', // The plural name is deliberately the same as singular |
| 18 | + wrapperName: 'StatusWrapper', |
| 19 | + wrapperImportPath: './test-utils/StatusWrapper', |
| 20 | + testUtilsFolderName: '../test-utils', |
| 21 | + }, |
| 22 | +]; |
| 23 | + |
| 24 | +describe(`${generateComponentFinders.name}`, () => { |
| 25 | + const testUtilTypes = ['dom', 'selectors'] as const; |
| 26 | + |
| 27 | + describe.each(testUtilTypes)('%s', testUtilType => { |
| 28 | + const sourceFileContent = generateComponentFinders({ components: mockComponents, testUtilType }); |
| 29 | + |
| 30 | + test('it re-exports element wrapper', () => { |
| 31 | + expect(sourceFileContent).toMatch('export { ElementWrapper }'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('it export component wrappers', () => { |
| 35 | + expect(sourceFileContent).toMatch('export { AlertWrapper };'); |
| 36 | + expect(sourceFileContent).toMatch('export { StatusWrapper };'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('it exports interfaces', () => { |
| 40 | + expect(sourceFileContent).toMatch(`declare module '@cloudscape-design/test-utils-core/dist/${testUtilType}'`); |
| 41 | + |
| 42 | + if (testUtilType === 'dom') { |
| 43 | + expect(sourceFileContent).toMatch(`findAlert(selector?: string): AlertWrapper | null`); |
| 44 | + expect(sourceFileContent).toMatch(`findAllAlerts(selector?: string): Array<AlertWrapper>`); |
| 45 | + expect(sourceFileContent).toMatch(`findStatus(selector?: string): StatusWrapper | null`); |
| 46 | + expect(sourceFileContent).toMatch(`findAllStatus(selector?: string): Array<StatusWrapper>`); |
| 47 | + } else { |
| 48 | + expect(sourceFileContent).toMatch(`findAlert(selector?: string): AlertWrapper`); |
| 49 | + expect(sourceFileContent).toMatch(`findAllAlerts(selector?: string): MultiElementWrapper<AlertWrapper>`); |
| 50 | + expect(sourceFileContent).toMatch(`findStatus(selector?: string): StatusWrapper`); |
| 51 | + expect(sourceFileContent).toMatch(`findAllStatus(selector?: string): MultiElementWrapper<StatusWrapper>`); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + test('it adds finder implementations to the ElementWrapper', () => { |
| 56 | + expect(sourceFileContent).toMatch('ElementWrapper.prototype.findAlert = function(selector)'); |
| 57 | + expect(sourceFileContent).toMatch('ElementWrapper.prototype.findAllAlerts = function(selector)'); |
| 58 | + expect(sourceFileContent).toMatch('ElementWrapper.prototype.findStatus = function(selector)'); |
| 59 | + expect(sourceFileContent).toMatch('ElementWrapper.prototype.findAllStatus = function(selector)'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('it exports the component metadata helper', () => { |
| 63 | + expect(sourceFileContent).toMatch(`export function getComponentMetadata(componentName: string)`); |
| 64 | + }); |
| 65 | + |
| 66 | + test('it exports the wrapper creator', () => { |
| 67 | + if (testUtilType === 'dom') { |
| 68 | + expect(sourceFileContent).toMatch('export default function wrapper(root: Element = document.body)'); |
| 69 | + } else { |
| 70 | + expect(sourceFileContent).toMatch(`export default function wrapper(root: string = 'body')`); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments