|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2022-2023 The Pybricks Authors |
| 3 | + |
| 4 | +import { waitFor } from '@testing-library/dom'; |
| 5 | +import React from 'react'; |
| 6 | +import { testRender } from '../../../test'; |
| 7 | +import RenameImportDialog from './ReplaceImportDialog'; |
| 8 | +import { |
| 9 | + ReplaceImportDialogAction, |
| 10 | + replaceImportDialogDidAccept, |
| 11 | + replaceImportDialogDidCancel, |
| 12 | +} from './actions'; |
| 13 | + |
| 14 | +describe('replace button', () => { |
| 15 | + it.each([ |
| 16 | + [/skip/i, ReplaceImportDialogAction.Skip, false], |
| 17 | + [/skip/i, ReplaceImportDialogAction.Skip, true], |
| 18 | + [/replace/i, ReplaceImportDialogAction.Replace, false], |
| 19 | + [/replace/i, ReplaceImportDialogAction.Replace, true], |
| 20 | + [/rename/i, ReplaceImportDialogAction.Rename, false], |
| 21 | + [/rename/i, ReplaceImportDialogAction.Rename, true], |
| 22 | + ])( |
| 23 | + 'should accept when %c%s button is clicked and remember checkbox is %s', |
| 24 | + async (buttonName, action, remember) => { |
| 25 | + const [user, dialog, dispatch] = testRender(<RenameImportDialog />, { |
| 26 | + explorer: { |
| 27 | + replaceImportDialog: { isOpen: true, fileName: 'old.file' }, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + if (remember) { |
| 32 | + const rememberCheckBox = dialog.getByRole('checkbox', { |
| 33 | + name: /remember/i, |
| 34 | + }); |
| 35 | + await user.click(rememberCheckBox); |
| 36 | + } |
| 37 | + |
| 38 | + const button = dialog.getByRole('button', { name: buttonName }); |
| 39 | + await user.click(button); |
| 40 | + |
| 41 | + expect(dispatch).toHaveBeenCalledWith( |
| 42 | + replaceImportDialogDidAccept(action, remember), |
| 43 | + ); |
| 44 | + }, |
| 45 | + ); |
| 46 | + |
| 47 | + it('should cancel when close button is clicked', async () => { |
| 48 | + const [user, dialog, dispatch] = testRender(<RenameImportDialog />, { |
| 49 | + explorer: { replaceImportDialog: { isOpen: true } }, |
| 50 | + }); |
| 51 | + |
| 52 | + const button = dialog.getByRole('button', { name: 'Close' }); |
| 53 | + |
| 54 | + await waitFor(() => expect(button).toBeVisible()); |
| 55 | + |
| 56 | + await user.click(button); |
| 57 | + expect(dispatch).toHaveBeenCalledWith(replaceImportDialogDidCancel()); |
| 58 | + }); |
| 59 | +}); |
0 commit comments