Skip to content

Commit 5a38347

Browse files
authored
Merge pull request #426 from vmnavarro94/depr-p1
test: deprecate react-unit-test-utils part-1
2 parents fc7370c + cc4b1c8 commit 5a38347

File tree

10 files changed

+103
-338
lines changed

10 files changed

+103
-338
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
2-
1+
import { render } from '@testing-library/react';
32
import { ConfirmModal } from './ConfirmModal';
43

4+
jest.unmock('@openedx/paragon');
5+
jest.unmock('react');
6+
57
describe('ConfirmModal', () => {
68
const props = {
79
isOpen: false,
@@ -12,10 +14,12 @@ describe('ConfirmModal', () => {
1214
onCancel: jest.fn().mockName('this.props.onCancel'),
1315
onConfirm: jest.fn().mockName('this.props.onConfirm'),
1416
};
15-
test('snapshot: closed', () => {
16-
expect(shallow(<ConfirmModal {...props} />).snapshot).toMatchSnapshot();
17+
it('should not render content when modal is closed', () => {
18+
const { queryByText } = render(<ConfirmModal {...props} />);
19+
expect(queryByText(props.content)).toBeNull();
1720
});
18-
test('snapshot: open', () => {
19-
expect(shallow(<ConfirmModal {...props} isOpen />).snapshot).toMatchSnapshot();
21+
it('should display content when modal is open', () => {
22+
const { getByText } = render(<ConfirmModal {...props} isOpen />);
23+
expect(getByText(props.content)).toBeInTheDocument();
2024
});
2125
});

src/components/DemoAlert/__snapshots__/index.test.jsx.snap

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
import React from 'react';
2-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render, fireEvent } from '@testing-library/react';
32

43
import { formatMessage } from 'testUtils';
4+
import messages from './messages';
55
import { DemoAlert } from '.';
66

7+
jest.unmock('@openedx/paragon');
8+
jest.unmock('react');
9+
710
describe('DemoAlert component', () => {
8-
test('snapshot', () => {
9-
const props = {
10-
intl: { formatMessage },
11-
isOpen: true,
12-
onClose: jest.fn().mockName('props.onClose'),
13-
};
14-
expect(shallow(<DemoAlert {...props} />).snapshot).toMatchSnapshot();
11+
const props = {
12+
intl: { formatMessage },
13+
isOpen: true,
14+
onClose: jest.fn().mockName('props.onClose'),
15+
};
16+
17+
it('does not render when isOpen is false', () => {
18+
const { queryByText } = render(<DemoAlert {...props} isOpen={false} />);
19+
expect(queryByText(formatMessage(messages.title))).toBeNull();
20+
});
21+
22+
it('renders with correct title and message when isOpen is true', () => {
23+
const { getByText } = render(<DemoAlert {...props} />);
24+
expect(getByText(formatMessage(messages.title))).toBeInTheDocument();
25+
expect(getByText(formatMessage(messages.warningMessage))).toBeInTheDocument();
26+
});
27+
28+
it('calls onClose when confirmation button is clicked', () => {
29+
const { getByText } = render(<DemoAlert {...props} />);
30+
const confirmButton = getByText(formatMessage(messages.confirm));
31+
fireEvent.click(confirmButton);
32+
expect(props.onClose).toHaveBeenCalled();
1533
});
1634
});

src/components/FilePopoverContent/__snapshots__/index.test.jsx.snap

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/components/FilePopoverContent/index.test.jsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React from 'react';
2-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render } from '@testing-library/react';
32

43
import filesize from 'filesize';
54
import FilePopoverContent from '.';
65

76
jest.mock('filesize', () => (size) => `filesize(${size})`);
7+
jest.unmock('@openedx/paragon');
8+
jest.unmock('react');
89

910
describe('FilePopoverContent', () => {
1011
describe('component', () => {
@@ -14,24 +15,26 @@ describe('FilePopoverContent', () => {
1415
downloadURL: 'this-url-is.working',
1516
size: 6000,
1617
};
17-
let el;
18-
beforeEach(() => {
19-
el = shallow(<FilePopoverContent {...props} />);
20-
});
21-
describe('snapshot', () => {
22-
test('default', () => expect(el.snapshot).toMatchSnapshot());
23-
test('invalid size', () => {
24-
el = shallow(<FilePopoverContent {...props} size={null} />);
25-
expect(el.snapshot).toMatchSnapshot();
26-
});
27-
});
2818

2919
describe('behavior', () => {
30-
test('content', () => {
31-
const childElements = el.instance.children;
32-
expect(childElements[0].children[2].el).toContain(props.name);
33-
expect(childElements[1].children[2].el).toContain(props.description);
34-
expect(childElements[2].children[2].el).toContain(filesize(props.size));
20+
it('renders file name correctly', () => {
21+
const { getByText } = render(<FilePopoverContent {...props} />);
22+
expect(getByText(props.name)).toBeInTheDocument();
23+
});
24+
25+
it('renders file description correctly', () => {
26+
const { getByText } = render(<FilePopoverContent {...props} />);
27+
expect(getByText(props.description)).toBeInTheDocument();
28+
});
29+
30+
it('renders file size correctly', () => {
31+
const { getByText } = render(<FilePopoverContent {...props} />);
32+
expect(getByText(filesize(props.size))).toBeInTheDocument();
33+
});
34+
35+
it('renders "Unknown" when size is null', () => {
36+
const { getByText } = render(<FilePopoverContent {...props} size={null} />);
37+
expect(getByText('Unknown')).toBeInTheDocument();
3538
});
3639
});
3740
});
Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import React from 'react';
2-
import { shallow } from '@edx/react-unit-test-utils';
3-
4-
import { Collapsible } from '@openedx/paragon';
5-
6-
import FilePopoverContent from 'components/FilePopoverContent';
7-
import FileInfo from './FileInfo';
1+
import { render, screen } from '@testing-library/react';
82
import FileCard from './FileCard';
93

104
jest.mock('components/FilePopoverContent', () => 'FilePopoverContent');
115
jest.mock('./FileInfo', () => 'FileInfo');
6+
jest.unmock('@openedx/paragon');
7+
jest.unmock('react');
128

139
describe('File Preview Card component', () => {
1410
const props = {
@@ -19,24 +15,27 @@ describe('File Preview Card component', () => {
1915
},
2016
};
2117
const children = (<h1>some children</h1>);
22-
let el;
23-
beforeEach(() => {
24-
el = shallow(<FileCard {...props}>{children}</FileCard>);
25-
});
26-
test('snapshot', () => {
27-
expect(el.snapshot).toMatchSnapshot();
28-
});
18+
2919
describe('Component', () => {
30-
test('collapsible title is name header', () => {
31-
const { title } = el.instance.findByType(Collapsible)[0].props;
32-
expect(title).toEqual(<h3 className="file-card-title">{props.file.name}</h3>);
20+
it('renders with the file name in the title', () => {
21+
render(<FileCard {...props}>{children}</FileCard>);
22+
expect(screen.getByText(props.file.name)).toBeInTheDocument();
23+
expect(screen.getByText(props.file.name)).toHaveClass('file-card-title');
24+
});
25+
26+
it('renders the preview panel with file info', () => {
27+
render(<FileCard {...props}>{children}</FileCard>);
28+
const previewPanel = screen.getByTestId('preview-panel');
29+
expect(previewPanel).toBeInTheDocument();
30+
expect(document.querySelector('FileInfo')).toBeInTheDocument();
31+
expect(document.querySelector('FilePopoverContent')).toBeInTheDocument();
3332
});
34-
test('forwards children into preview-panel', () => {
35-
const previewPanelChildren = el.instance.findByTestId('preview-panel')[0].children;
36-
expect(previewPanelChildren[0].matches(
37-
<FileInfo><FilePopoverContent file={props.file} /></FileInfo>,
38-
));
39-
expect(previewPanelChildren[1].matches(shallow(children))).toEqual(true);
33+
34+
it('renders children in the preview panel', () => {
35+
render(<FileCard {...props}>{children}</FileCard>);
36+
const previewPanel = screen.getByTestId('preview-panel');
37+
expect(previewPanel).toBeInTheDocument();
38+
expect(screen.getByText('some children')).toBeInTheDocument();
4039
});
4140
});
4241
});

src/components/FilePreview/__snapshots__/FileCard.test.jsx.snap

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)