Skip to content

Commit b10aa63

Browse files
test: deprecate react-unit-test-utils part-5 (#439)
* test: deprecate react-unit-test-utils part-5 * test: fixes due to rebase * test: change fireEvent to userEvent * test: improve tests and address feedback --------- Co-authored-by: diana-villalvazo-wgu <diana.villalvazo@wgu.edu>
1 parent 377bb6b commit b10aa63

File tree

11 files changed

+276
-870
lines changed

11 files changed

+276
-870
lines changed

src/containers/ListView/ListViewBreadcrumb.test.jsx

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import React from 'react';
2-
import { shallow } from '@edx/react-unit-test-utils';
3-
4-
import { Hyperlink } from '@openedx/paragon';
5-
6-
import * as constants from 'data/constants/app';
7-
import urls from 'data/services/lms/urls';
1+
import { render } from '@testing-library/react';
2+
import { IntlProvider } from '@edx/frontend-platform/i18n';
83
import { selectors } from 'data/redux';
9-
104
import {
115
ListViewBreadcrumb,
126
mapStateToProps,
137
} from './ListViewBreadcrumb';
148

9+
jest.unmock('@openedx/paragon');
10+
jest.unmock('react');
11+
jest.unmock('@edx/frontend-platform/i18n');
12+
1513
jest.mock('data/redux', () => ({
1614
selectors: {
1715
app: {
18-
courseId: (...args) => ({ courseId: args }),
16+
courseId: jest.fn((state) => state.courseId || 'test-course-id'),
1917
ora: {
20-
name: (...args) => ({ oraName: args }),
18+
name: jest.fn((state) => state.oraName || 'test-ora-name'),
2119
},
2220
},
2321
},
@@ -28,41 +26,64 @@ jest.mock('data/services/lms/urls', () => ({
2826
ora: (courseId, locationId) => `oraUrl(${courseId}, ${locationId})`,
2927
}));
3028

31-
let el;
29+
jest.mock('data/constants/app', () => ({
30+
locationId: () => 'test-location-id',
31+
}));
3232

3333
describe('ListViewBreadcrumb component', () => {
34-
describe('component', () => {
35-
const props = {
36-
courseId: 'test-course-id',
37-
oraName: 'fake-ora-name',
38-
};
39-
beforeEach(() => {
40-
el = shallow(<ListViewBreadcrumb {...props} />);
34+
const props = {
35+
courseId: 'test-course-id',
36+
oraName: 'fake-ora-name',
37+
};
38+
39+
const renderWithIntl = (component) => render(
40+
<IntlProvider locale="en" messages={{}}>
41+
{component}
42+
</IntlProvider>,
43+
);
44+
45+
beforeEach(() => {
46+
jest.clearAllMocks();
47+
});
48+
49+
describe('behavior', () => {
50+
it('renders back to responses link with correct destination', () => {
51+
const { container } = renderWithIntl(<ListViewBreadcrumb {...props} />);
52+
const backLink = container.querySelector('a[href*="openResponseUrl"]');
53+
expect(backLink).toBeInTheDocument();
54+
expect(backLink).toHaveAttribute('href', `openResponseUrl(${props.courseId})`);
4155
});
42-
test('snapshot: empty (no list data)', () => {
43-
expect(el.snapshot).toMatchSnapshot();
56+
57+
it('displays ORA name in heading', () => {
58+
const { getByText } = renderWithIntl(<ListViewBreadcrumb {...props} />);
59+
const heading = getByText(props.oraName);
60+
expect(heading).toBeInTheDocument();
61+
expect(heading).toHaveClass('h3');
4462
});
45-
test('openResponse destination', () => {
46-
expect(
47-
el.instance.findByType(Hyperlink)[0].props.destination,
48-
).toEqual(urls.openResponse(props.courseId));
63+
64+
it('renders ORA link with correct destination', () => {
65+
const { container } = renderWithIntl(<ListViewBreadcrumb {...props} />);
66+
const oraLink = container.querySelector('a[href*="oraUrl"]');
67+
expect(oraLink).toBeInTheDocument();
68+
expect(oraLink).toHaveAttribute('href', `oraUrl(${props.courseId}, test-location-id)`);
4969
});
50-
test('ora destination', () => {
51-
expect(
52-
el.instance.findByType(Hyperlink)[1].props.destination,
53-
).toEqual(urls.ora(props.courseId, constants.locationId()));
70+
71+
it('displays back to responses text', () => {
72+
const { getByText } = renderWithIntl(<ListViewBreadcrumb {...props} />);
73+
expect(getByText('Back to all open responses')).toBeInTheDocument();
5474
});
5575
});
76+
5677
describe('mapStateToProps', () => {
57-
let mapped;
5878
const testState = { some: 'test-state' };
59-
beforeEach(() => {
60-
mapped = mapStateToProps(testState);
61-
});
62-
test('courseId loads from app.courseId', () => {
79+
80+
it('maps courseId from app.courseId selector', () => {
81+
const mapped = mapStateToProps(testState);
6382
expect(mapped.courseId).toEqual(selectors.app.courseId(testState));
6483
});
65-
test('oraName loads from app.ora.name', () => {
84+
85+
it('maps oraName from app.ora.name selector', () => {
86+
const mapped = mapStateToProps(testState);
6687
expect(mapped.oraName).toEqual(selectors.app.ora.name(testState));
6788
});
6889
});
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
import React from 'react';
2-
import { shallow } from '@edx/react-unit-test-utils';
3-
1+
import { render, screen } from '@testing-library/react';
2+
import { IntlProvider } from '@edx/frontend-platform/i18n';
43
import { SelectedBulkAction } from './SelectedBulkAction';
54

5+
jest.unmock('@openedx/paragon');
6+
jest.unmock('react');
7+
jest.unmock('@edx/frontend-platform/i18n');
8+
69
describe('SelectedBulkAction component', () => {
710
const props = {
811
selectedFlatRows: [{ id: 1 }, { id: 2 }],
9-
handleClick: jest.fn(),
12+
handleClick: jest.fn(() => () => {}),
1013
};
11-
test('snapshots', () => {
12-
const el = shallow(<SelectedBulkAction {...props} handleClick={() => jest.fn()} />);
13-
expect(el.snapshot).toMatchSnapshot();
14+
15+
beforeEach(() => {
16+
jest.clearAllMocks();
17+
});
18+
19+
it('renders button with correct text and selected count', () => {
20+
render(<IntlProvider locale="en" messages={{}}><SelectedBulkAction {...props} /></IntlProvider>);
21+
const button = screen.getByRole('button');
22+
expect(button).toBeInTheDocument();
23+
expect(button).toHaveTextContent(`View selected responses (${props.selectedFlatRows.length})`);
24+
});
25+
26+
it('applies correct CSS class to button', () => {
27+
render(<IntlProvider locale="en" messages={{}}><SelectedBulkAction {...props} /></IntlProvider>);
28+
const button = screen.getByRole('button');
29+
expect(button).toHaveClass('view-selected-responses-btn');
30+
expect(button).toHaveClass('btn-primary');
1431
});
1532

16-
test('handleClick', () => {
17-
shallow(<SelectedBulkAction {...props} />);
33+
it('calls handleClick with selectedFlatRows on render', () => {
34+
render(<IntlProvider locale="en" messages={{}}><SelectedBulkAction {...props} /></IntlProvider>);
1835
expect(props.handleClick).toHaveBeenCalledWith(props.selectedFlatRows);
1936
});
2037
});

0 commit comments

Comments
 (0)