|
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'; |
4 | 3 | import { downloadAllLimit, downloadSingleLimit } from 'data/constants/files'; |
5 | | - |
6 | | -import { formatMessage } from 'testUtils'; |
7 | 4 | import { SubmissionFiles } from './SubmissionFiles'; |
8 | | -import messages from './messages'; |
9 | 5 |
|
10 | | -jest.mock('./components/FileNameCell', () => jest.fn().mockName('FileNameCell')); |
11 | | -jest.mock('./components/FileExtensionCell', () => jest.fn().mockName('FileExtensionCell')); |
12 | | -jest.mock('./components/FilePopoverCell', () => jest.fn().mockName('FilePopoverCell')); |
13 | | -jest.mock('./FileDownload', () => 'FileDownload'); |
| 6 | +jest.unmock('@openedx/paragon'); |
| 7 | +jest.unmock('react'); |
| 8 | +jest.unmock('@edx/frontend-platform/i18n'); |
| 9 | + |
| 10 | +jest.mock('./components/FileNameCell', () => jest.fn(({ value }) => <div>Name: {value}</div>)); |
| 11 | +jest.mock('./components/FileExtensionCell', () => jest.fn(({ value }) => <div>Extension: {value}</div>)); |
| 12 | +jest.mock('./components/FilePopoverCell', () => jest.fn(() => <div>Popover</div>)); |
| 13 | +jest.mock('./FileDownload', () => jest.fn(({ files }) => <div data-testid="file-download">Download {files.length} files</div>)); |
14 | 14 |
|
15 | 15 | describe('SubmissionFiles', () => { |
16 | | - describe('component', () => { |
17 | | - const props = { |
18 | | - files: [ |
19 | | - { |
20 | | - name: 'some file name.jpg', |
21 | | - description: 'description for the file', |
22 | | - downloadURL: '/valid-url-wink-wink', |
23 | | - size: 0, |
24 | | - }, |
25 | | - { |
26 | | - name: 'file number 2.jpg', |
27 | | - description: 'description for this file', |
28 | | - downloadURL: '/url-2', |
29 | | - size: 0, |
30 | | - }, |
31 | | - ], |
32 | | - }; |
33 | | - let el; |
34 | | - beforeEach(() => { |
35 | | - el = shallow(<SubmissionFiles {...props} />); |
36 | | - }); |
| 16 | + const defaultProps = { |
| 17 | + files: [ |
| 18 | + { |
| 19 | + name: 'some file name.jpg', |
| 20 | + description: 'description for the file', |
| 21 | + downloadURL: '/valid-url-wink-wink', |
| 22 | + size: 100, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'file number 2.jpg', |
| 26 | + description: 'description for this file', |
| 27 | + downloadURL: '/url-2', |
| 28 | + size: 200, |
| 29 | + }, |
| 30 | + ], |
| 31 | + }; |
37 | 32 |
|
38 | | - describe('snapshot', () => { |
39 | | - test('files existed for props', () => { |
40 | | - expect(el.snapshot).toMatchSnapshot(); |
41 | | - }); |
| 33 | + const renderWithIntl = (component) => render( |
| 34 | + <IntlProvider locale="en" messages={{}}> |
| 35 | + {component} |
| 36 | + </IntlProvider>, |
| 37 | + ); |
42 | 38 |
|
43 | | - test('files does not exist', () => { |
44 | | - el = shallow(<SubmissionFiles {...props} files={[]} />); |
| 39 | + beforeEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + }); |
45 | 42 |
|
46 | | - expect(el.snapshot).toMatchSnapshot(); |
47 | | - }); |
48 | | - test('files size exceed', () => { |
49 | | - const files = props.files.map(file => ({ ...file, size: downloadSingleLimit + 1 })); |
50 | | - el = shallow(<SubmissionFiles {...props} files={files} />); |
51 | | - expect(el.snapshot).toMatchSnapshot(); |
52 | | - }); |
| 43 | + describe('behavior', () => { |
| 44 | + it('displays submission files title with file count', () => { |
| 45 | + renderWithIntl(<SubmissionFiles {...defaultProps} />); |
| 46 | + const title = screen.getByTestId('submission-files-title'); |
| 47 | + expect(title).toBeInTheDocument(); |
| 48 | + expect(title).toHaveTextContent(`Submission Files (${defaultProps.files.length})`); |
53 | 49 | }); |
54 | 50 |
|
55 | | - describe('behavior', () => { |
56 | | - test('title', () => { |
57 | | - const titleEl = el.instance.findByTestId('submission-files-title')[0].children[0]; |
58 | | - expect(titleEl.el).toEqual( |
59 | | - `${formatMessage(messages.submissionFiles)} (${props.files.length})`, |
60 | | - ); |
61 | | - }); |
62 | | - |
63 | | - describe('canDownload', () => { |
64 | | - test('normal file size', () => { |
65 | | - expect(el.instance.findByTestId('file-download')).toHaveLength(1); |
66 | | - }); |
| 51 | + it('renders file download component when files can be downloaded', () => { |
| 52 | + renderWithIntl(<SubmissionFiles {...defaultProps} />); |
| 53 | + const downloadComponent = screen.getByTestId('file-download'); |
| 54 | + expect(downloadComponent).toBeInTheDocument(); |
| 55 | + expect(downloadComponent).toHaveTextContent('Download 2 files'); |
| 56 | + }); |
67 | 57 |
|
68 | | - test('one of the file exceed the limit', () => { |
69 | | - const oneFileExceed = [{ ...props.files[0], size: downloadSingleLimit + 1 }, props.files[1]]; |
| 58 | + it('displays warning when individual file exceeds size limit', () => { |
| 59 | + const largeFileProps = { |
| 60 | + ...defaultProps, |
| 61 | + files: [ |
| 62 | + { ...defaultProps.files[0], size: downloadSingleLimit + 1 }, |
| 63 | + defaultProps.files[1], |
| 64 | + ], |
| 65 | + }; |
| 66 | + renderWithIntl(<SubmissionFiles {...largeFileProps} />); |
70 | 67 |
|
71 | | - oneFileExceed.forEach(file => expect(file.size < downloadAllLimit).toEqual(true)); |
| 68 | + expect(screen.queryByTestId('file-download')).not.toBeInTheDocument(); |
| 69 | + const warningText = screen.getByTestId('exceed-download-text'); |
| 70 | + expect(warningText).toBeInTheDocument(); |
| 71 | + expect(warningText).toHaveTextContent('Exceeded the allow download size'); |
| 72 | + }); |
72 | 73 |
|
73 | | - el = shallow(<SubmissionFiles {...props} files={oneFileExceed} />); |
74 | | - expect(el.instance.findByTestId('file-download')).toHaveLength(0); |
| 74 | + it('displays warning when total file size exceeds limit', () => { |
| 75 | + const largeFileSize = (downloadAllLimit + 1) / 20; |
| 76 | + const largeFilesProps = { |
| 77 | + ...defaultProps, |
| 78 | + files: Array(20).fill({ |
| 79 | + name: 'large file.jpg', |
| 80 | + description: 'large file description', |
| 81 | + downloadURL: '/large-file-url', |
| 82 | + size: largeFileSize, |
| 83 | + }), |
| 84 | + }; |
| 85 | + renderWithIntl(<SubmissionFiles {...largeFilesProps} />); |
75 | 86 |
|
76 | | - const warningEl = el.instance.findByTestId('exceed-download-text')[0]; |
77 | | - expect(warningEl.el.children[1]).toEqual(formatMessage(messages.exceedFileSize)); |
78 | | - }); |
| 87 | + expect(screen.queryByTestId('file-download')).not.toBeInTheDocument(); |
| 88 | + }); |
79 | 89 |
|
80 | | - test('total file size exceed the limit', () => { |
81 | | - const length = 20; |
82 | | - const totalFilesExceed = new Array(length).fill({ |
83 | | - name: 'some file name.jpg', |
84 | | - description: 'description for the file', |
85 | | - downloadURL: '/valid-url-wink-wink', |
86 | | - size: (downloadAllLimit + 1) / length, |
87 | | - }); |
88 | | - totalFilesExceed.forEach(file => { |
89 | | - expect(file.size < downloadAllLimit).toEqual(true); |
90 | | - expect(file.size < downloadSingleLimit).toEqual(true); |
91 | | - }); |
| 90 | + it('displays title only when no files are provided', () => { |
| 91 | + const { container } = renderWithIntl(<SubmissionFiles {...defaultProps} files={[]} />); |
| 92 | + const title = container.querySelector('.submission-files-title h3'); |
| 93 | + expect(title).toBeInTheDocument(); |
| 94 | + expect(title).toHaveTextContent('Submission Files (0)'); |
| 95 | + expect(screen.queryByTestId('file-download')).not.toBeInTheDocument(); |
| 96 | + }); |
92 | 97 |
|
93 | | - el = shallow(<SubmissionFiles {...props} files={totalFilesExceed} />); |
94 | | - expect(el.instance.findByTestId('file-download')).toHaveLength(0); |
95 | | - }); |
96 | | - }); |
| 98 | + it('renders data table with correct file information', () => { |
| 99 | + const { container } = renderWithIntl(<SubmissionFiles {...defaultProps} />); |
| 100 | + const dataTable = container.querySelector('.submission-files-table'); |
| 101 | + expect(dataTable).toBeInTheDocument(); |
97 | 102 | }); |
98 | 103 | }); |
99 | 104 | }); |
0 commit comments