- ALM used: GitHub
- CI system used: GHA
- Scan
- TypeScript
False positive issue for rule typescript:S2187 when using it.each which is IMO a pretty good practice and elegant way to test several cases.
The file in question:
import { useDispatch } from 'react-redux';
import { describe, it, expect, Mock } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { KeysCombinations, onKeyPress } from '@hmi/core-features-events';
import { useKeys } from '../useKeys';
vi.mock('react-redux', () => ({
useDispatch: vi.fn(),
}));
describe('useKeys', () => {
let dispatchMock: Mock;
beforeEach(() => {
dispatchMock = vi.fn();
(useDispatch as unknown as Mock).mockReturnValue(dispatchMock);
});
it.each`
key | expectedAction
${'{Alt>}a{/Alt}'} | ${onKeyPress(KeysCombinations.altA)}
${'{Control>}a{/Control}'} | ${onKeyPress(KeysCombinations.ctrlA)}
`(
'should dispatch onKeyPress action when $key is pressed',
async ({ key, expectedAction }) => {
renderHook(() => useKeys());
// Simulate key press
await act(async () => {
await userEvent.keyboard(key);
});
// Verify that the correct action has been dispatched
expect(dispatchMock).toHaveBeenCalledWith(expectedAction);
},
);
});