Skip to content

Commit d827f03

Browse files
authored
chore(storage-browser): Update control unit tests to be more isolated (#6194)
1 parent 0e666ef commit d827f03

36 files changed

+541
-569
lines changed
Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import { ActionCancelControl } from '../ActionCancelControl';
4-
import * as useActionCancelModule from '../hooks/useActionCancel';
4+
import { useActionCancel } from '../hooks/useActionCancel';
5+
import { useResolvedComposable } from '../hooks/useResolvedComposable';
6+
7+
jest.mock('../hooks/useActionCancel');
8+
jest.mock('../hooks/useResolvedComposable');
9+
jest.mock('../../composables/ActionCancel', () => ({
10+
ActionCancel: () => <div data-testid="action-cancel" />,
11+
}));
512

613
describe('ActionCancelControl', () => {
7-
const useActionCancelSpy = jest.spyOn(
8-
useActionCancelModule,
9-
'useActionCancel'
10-
);
14+
const mockUseActionCancel = jest.mocked(useActionCancel);
15+
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
1116

12-
beforeEach(() => {
13-
useActionCancelSpy.mockClear();
17+
beforeAll(() => {
18+
mockUseResolvedComposable.mockImplementation(
19+
(component) => component as () => React.JSX.Element
20+
);
1421
});
1522

16-
it('renders', () => {
17-
useActionCancelSpy.mockReturnValue({
18-
isDisabled: false,
19-
onCancel: jest.fn(),
20-
label: 'Cancel',
21-
});
22-
render(<ActionCancelControl />);
23-
24-
const button = screen.getByRole('button', {
25-
name: 'Cancel',
26-
});
27-
28-
expect(button).toBeInTheDocument();
23+
afterEach(() => {
24+
mockUseActionCancel.mockClear();
2925
});
3026

31-
it('disables button', () => {
32-
useActionCancelSpy.mockReturnValue({
33-
isDisabled: true,
34-
onCancel: jest.fn(),
35-
label: 'Cancel',
36-
});
27+
it('renders', () => {
3728
render(<ActionCancelControl />);
3829

39-
const button = screen.getByRole('button', {
40-
name: 'Cancel',
41-
});
30+
const actionCancel = screen.getByTestId('action-cancel');
4231

43-
expect(button).toBeDisabled();
32+
expect(actionCancel).toBeInTheDocument();
4433
});
4534
});

packages/react-storage/src/components/StorageBrowser/controls/__tests__/ActionDestinationControl.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('ActionDestinationControl', () => {
2727
it('renders', () => {
2828
render(<ActionDestinationControl />);
2929

30-
const ActionDestination = screen.getByTestId('action-destination');
30+
const actionDestination = screen.getByTestId('action-destination');
3131

32-
expect(ActionDestination).toBeInTheDocument();
32+
expect(actionDestination).toBeInTheDocument();
3333
});
3434
});
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import { ActionExitControl } from '../ActionExitControl';
4-
import * as UseActionExitModule from '../hooks/useActionExit';
4+
import { useActionExit } from '../hooks/useActionExit';
5+
import { useResolvedComposable } from '../hooks/useResolvedComposable';
6+
7+
jest.mock('../hooks/useActionExit');
8+
jest.mock('../hooks/useResolvedComposable');
9+
jest.mock('../../composables/ActionExit', () => ({
10+
ActionExit: () => <div data-testid="action-exit" />,
11+
}));
512

613
describe('ActionExitControl', () => {
7-
const useActionExitSpy = jest.spyOn(UseActionExitModule, 'useActionExit');
14+
const mockUseActionExit = jest.mocked(useActionExit);
15+
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
816

9-
beforeEach(() => {
10-
useActionExitSpy.mockClear();
17+
beforeAll(() => {
18+
mockUseResolvedComposable.mockImplementation(
19+
(component) => component as () => React.JSX.Element
20+
);
1121
});
1222

13-
it('renders', () => {
14-
useActionExitSpy.mockReturnValue({
15-
isDisabled: false,
16-
onExit: jest.fn(),
17-
label: 'Exit',
18-
});
19-
render(<ActionExitControl />);
20-
21-
const button = screen.getByRole('button', { name: 'Exit' });
22-
23-
expect(button).toBeInTheDocument();
23+
afterEach(() => {
24+
mockUseActionExit.mockClear();
2425
});
2526

26-
it('disables button', () => {
27-
useActionExitSpy.mockReturnValue({
28-
isDisabled: true,
29-
onExit: jest.fn(),
30-
label: 'Exit',
31-
});
27+
it('renders', () => {
3228
render(<ActionExitControl />);
3329

34-
const button = screen.getByRole('button', { name: 'Exit' });
30+
const actionExit = screen.getByTestId('action-exit');
3531

36-
expect(button).toBeDisabled();
32+
expect(actionExit).toBeInTheDocument();
3733
});
3834
});
Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,34 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import { ActionStartControl } from '../ActionStartControl';
4-
import * as useActionStartModule from '../hooks/useActionStart';
4+
import { useActionStart } from '../hooks/useActionStart';
5+
import { useResolvedComposable } from '../hooks/useResolvedComposable';
56

6-
describe('ActionStartControl', () => {
7-
const useActionStartSpy = jest.spyOn(useActionStartModule, 'useActionStart');
8-
9-
beforeEach(() => {
10-
useActionStartSpy.mockClear();
11-
});
12-
13-
it('renders', () => {
14-
useActionStartSpy.mockReturnValue({
15-
isDisabled: false,
16-
onStart: jest.fn(),
17-
label: 'Start',
18-
});
19-
render(<ActionStartControl />);
7+
jest.mock('../hooks/useActionStart');
8+
jest.mock('../hooks/useResolvedComposable');
9+
jest.mock('../../composables/ActionStart', () => ({
10+
ActionStart: () => <div data-testid="action-start" />,
11+
}));
2012

21-
const button = screen.getByRole('button', {
22-
name: 'Start',
23-
});
13+
describe('ActionStartControl', () => {
14+
const mockUseActionStart = jest.mocked(useActionStart);
15+
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
2416

25-
expect(button).toBeInTheDocument();
17+
beforeAll(() => {
18+
mockUseResolvedComposable.mockImplementation(
19+
(component) => component as () => React.JSX.Element
20+
);
2621
});
2722

28-
it('renders with custom label', () => {
29-
useActionStartSpy.mockReturnValue({
30-
isDisabled: false,
31-
onStart: jest.fn(),
32-
label: 'Custom Label',
33-
});
34-
render(<ActionStartControl />);
35-
36-
const button = screen.getByRole('button', {
37-
name: 'Custom Label',
38-
});
39-
40-
expect(button).toBeInTheDocument();
23+
afterEach(() => {
24+
mockUseActionStart.mockClear();
4125
});
4226

43-
it('calls onStart when button is clicked', () => {
44-
const mockOnStart = jest.fn();
45-
useActionStartSpy.mockReturnValue({
46-
isDisabled: false,
47-
onStart: mockOnStart,
48-
label: 'Start',
49-
});
27+
it('renders', () => {
5028
render(<ActionStartControl />);
5129

52-
const button = screen.getByRole('button', {
53-
name: 'Start',
54-
});
55-
56-
button.click();
30+
const actionStart = screen.getByTestId('action-start');
5731

58-
expect(mockOnStart).toHaveBeenCalled();
32+
expect(actionStart).toBeInTheDocument();
5933
});
6034
});

packages/react-storage/src/components/StorageBrowser/controls/__tests__/ActionsListControl.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('ActionsListControl', () => {
2727
it('renders', () => {
2828
render(<ActionsListControl />);
2929

30-
const ActionsList = screen.getByTestId('actions-list');
30+
const actionsList = screen.getByTestId('actions-list');
3131

32-
expect(ActionsList).toBeInTheDocument();
32+
expect(actionsList).toBeInTheDocument();
3333
});
3434
});

packages/react-storage/src/components/StorageBrowser/controls/__tests__/AddFilesControl.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jest.mock('../../composables/AddFiles', () => ({
1111
}));
1212

1313
describe('AddFilesControl', () => {
14-
// assert mocks
1514
const mockUseAddFiles = jest.mocked(useAddFiles);
1615
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
1716

packages/react-storage/src/components/StorageBrowser/controls/__tests__/AddFolderControl.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jest.mock('../../composables/AddFolder', () => ({
1111
}));
1212

1313
describe('AddFolderControl', () => {
14-
// assert mocks
1514
const mockUseAddFolder = jest.mocked(useAddFolder);
1615
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
1716

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
import React from 'react';
2-
import { render, screen, fireEvent } from '@testing-library/react';
2+
import { render, screen } from '@testing-library/react';
33
import { DataRefreshControl } from '../DataRefreshControl';
4+
import { useDataRefresh } from '../hooks/useDataRefresh';
5+
import { useResolvedComposable } from '../hooks/useResolvedComposable';
46

5-
import * as useDataRefreshModule from '../hooks/useDataRefresh';
7+
jest.mock('../hooks/useDataRefresh');
8+
jest.mock('../hooks/useResolvedComposable');
9+
jest.mock('../../composables/DataRefresh', () => ({
10+
DataRefresh: () => <div data-testid="data-refresh" />,
11+
}));
612

713
describe('DataRefreshControl', () => {
8-
const useDataRefreshSpy = jest.spyOn(useDataRefreshModule, 'useDataRefresh');
14+
const mockUseDataRefresh = jest.mocked(useDataRefresh);
15+
const mockUseResolvedComposable = jest.mocked(useResolvedComposable);
916

10-
afterEach(() => {
11-
useDataRefreshSpy.mockReset();
17+
beforeAll(() => {
18+
mockUseResolvedComposable.mockImplementation(
19+
(component) => component as () => React.JSX.Element
20+
);
1221
});
1322

14-
it('renders with button enabled', () => {
15-
const onRefreshMock = jest.fn();
16-
useDataRefreshSpy.mockReturnValue({
17-
isDisabled: false,
18-
onRefresh: onRefreshMock,
19-
});
20-
21-
render(<DataRefreshControl />);
22-
23-
const button = screen.getByRole('button', {
24-
name: 'Refresh data',
25-
});
26-
27-
const icon = button.querySelector('svg');
28-
29-
expect(button).toBeInTheDocument();
30-
expect(button).not.toHaveAttribute('disabled');
31-
expect(icon).toBeInTheDocument();
32-
expect(icon).toHaveAttribute('aria-hidden', 'true');
33-
34-
fireEvent.click(button);
35-
expect(onRefreshMock).toHaveBeenCalled();
23+
afterEach(() => {
24+
mockUseDataRefresh.mockClear();
3625
});
3726

38-
it('renders with button disabled', () => {
39-
useDataRefreshSpy.mockReturnValue({
40-
isDisabled: true,
41-
onRefresh: jest.fn(),
42-
});
43-
27+
it('renders', () => {
4428
render(<DataRefreshControl />);
4529

46-
const button = screen.getByRole('button', {
47-
name: 'Refresh data',
48-
});
49-
50-
const icon = button.querySelector('svg');
30+
const dataRefresh = screen.getByTestId('data-refresh');
5131

52-
expect(button).toBeInTheDocument();
53-
expect(button).toHaveAttribute('disabled');
54-
expect(icon).toBeInTheDocument();
55-
expect(icon).toHaveAttribute('aria-hidden', 'true');
32+
expect(dataRefresh).toBeInTheDocument();
5633
});
5734
});

0 commit comments

Comments
 (0)