|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { readFileSync, writeFileSync } from 'fs'; |
| 4 | +import { describe, test, expect, beforeAll, beforeEach, vi } from 'vitest'; |
| 5 | +import { convertToSelectorUtil } from '../convert-to-selectors'; |
| 6 | +import { generateTestUtils } from '../generate-test-utils'; |
| 7 | +import { ComponentMetadata } from '../interfaces'; |
| 8 | + |
| 9 | +vi.mock('fs'); |
| 10 | +vi.mock('../convert-to-selectors'); |
| 11 | + |
| 12 | +const mockComponents: ComponentMetadata[] = [ |
| 13 | + { |
| 14 | + name: 'Alert', |
| 15 | + pluralName: 'Alerts', |
| 16 | + testUtilsFolderName: '../test-utils', |
| 17 | + }, |
| 18 | +]; |
| 19 | + |
| 20 | +describe(`${generateTestUtils.name}`, () => { |
| 21 | + beforeAll(() => { |
| 22 | + vi.mocked(readFileSync).mockReturnValue('file content'); |
| 23 | + vi.mocked(convertToSelectorUtil).mockImplementation(input => input); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + test('generates the selector utils for the given dom utils', () => { |
| 31 | + generateTestUtils({ |
| 32 | + components: mockComponents, |
| 33 | + testUtilsPath: './test/mock-test-utils', |
| 34 | + }); |
| 35 | + |
| 36 | + expect(convertToSelectorUtil).toHaveBeenCalledWith('file content'); |
| 37 | + }); |
| 38 | + |
| 39 | + const testUtilsType = ['dom', 'selectors'] as const; |
| 40 | + test.each(testUtilsType)('generates the index file for %s test utils', testUtilType => { |
| 41 | + generateTestUtils({ |
| 42 | + components: mockComponents, |
| 43 | + testUtilsPath: './test/mock-test-utils', |
| 44 | + }); |
| 45 | + |
| 46 | + const testUtilsFilePartialContent = 'ElementWrapper.prototype.findAlert'; |
| 47 | + |
| 48 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 49 | + `test/mock-test-utils/${testUtilType}/index.ts`, |
| 50 | + expect.stringMatching(testUtilsFilePartialContent) |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
0 commit comments