|
| 1 | +import * as React from 'react'; |
| 2 | +import FileIcon from '../../../icons/file-icon'; |
| 3 | + |
| 4 | +import { MetadataBasedItemListComponent as MetadataBasedItemList } from '../MetadataBasedItemList'; |
| 5 | + |
| 6 | +jest.mock('react-virtualized/dist/es/AutoSizer', () => () => 'AutoSizer'); |
| 7 | + |
| 8 | +describe('features/metadata-based-view/MetadataBasedItemList', () => { |
| 9 | + let wrapper; |
| 10 | + let instance; |
| 11 | + const intl = { formatMessage: jest.fn().mockReturnValue('Name') }; |
| 12 | + const currentCollection = { |
| 13 | + items: [ |
| 14 | + { |
| 15 | + metadata: { |
| 16 | + data: { type: 'bill', amount: 500 }, |
| 17 | + }, |
| 18 | + name: 'name1.pdf', |
| 19 | + }, |
| 20 | + { |
| 21 | + metadata: { |
| 22 | + data: { type: 'receipt', amount: 200 }, |
| 23 | + }, |
| 24 | + name: 'name2.mp4', |
| 25 | + }, |
| 26 | + ], |
| 27 | + nextMarker: 'abc', |
| 28 | + }; |
| 29 | + const metadataColumnsToShow = ['type', 'amount']; |
| 30 | + const pdfIcon = <FileIcon dimension={32} extension="pdf" />; |
| 31 | + const mp4Icon = <FileIcon dimension={32} extension="mp4" />; |
| 32 | + |
| 33 | + const defaultProps = { |
| 34 | + currentCollection, |
| 35 | + metadataColumnsToShow, |
| 36 | + intl, |
| 37 | + }; |
| 38 | + |
| 39 | + const getWrapper = (props = defaultProps) => mount(<MetadataBasedItemList {...props} />); |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + wrapper = getWrapper(); |
| 43 | + instance = wrapper.instance(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('getColumnWidth(columnIndex)', () => { |
| 47 | + test.each` |
| 48 | + columnIndex | columnWidth | desc |
| 49 | + ${0} | ${54} | ${'file icon'} |
| 50 | + ${1} | ${350} | ${'file name'} |
| 51 | + ${2} | ${250} | ${'metadata column'} |
| 52 | + `('getColumnWidth() for $desc', ({ columnIndex, columnWidth }) => { |
| 53 | + const availableWidth = 500; // width provided to AutoSizer Component |
| 54 | + const getWidth = instance.getColumnWidth(availableWidth); |
| 55 | + expect(getWidth({ index: columnIndex })).toEqual(columnWidth); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('getGridCellData(columnIndex, rowIndex)', () => { |
| 60 | + test.each` |
| 61 | + columnIndex | rowIndex | cellData |
| 62 | + ${0} | ${1} | ${pdfIcon} |
| 63 | + ${1} | ${1} | ${'name1.pdf'} |
| 64 | + ${2} | ${1} | ${'bill'} |
| 65 | + ${3} | ${1} | ${500} |
| 66 | + ${0} | ${2} | ${mp4Icon} |
| 67 | + ${1} | ${2} | ${'name2.mp4'} |
| 68 | + ${2} | ${2} | ${'receipt'} |
| 69 | + ${3} | ${2} | ${200} |
| 70 | + `('cellData for row: $rowIndex, column: $columnIndex', ({ columnIndex, rowIndex, cellData }) => { |
| 71 | + const data = instance.getGridCellData(columnIndex, rowIndex); |
| 72 | + expect(data).toEqual(cellData); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('getGridHeaderData(columnIndex)', () => { |
| 77 | + test.each` |
| 78 | + columnIndex | headerData |
| 79 | + ${0} | ${undefined} |
| 80 | + ${1} | ${'Name'} |
| 81 | + ${2} | ${'type'} |
| 82 | + ${3} | ${'amount'} |
| 83 | + `('headerData for column $columnIndex', ({ columnIndex, headerData }) => { |
| 84 | + const data = instance.getGridHeaderData(columnIndex); |
| 85 | + expect(data).toEqual(headerData); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('handleMouseEnter()', () => { |
| 90 | + test('should handle mouse over event by setting state accordingly', () => { |
| 91 | + instance.handleMouseEnter(5); |
| 92 | + expect(instance.state.hoveredRowIndex).toEqual(5); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('handleMouseLeave()', () => { |
| 97 | + test('should handle mouse leave event by setting state accordingly', () => { |
| 98 | + instance.handleMouseLeave(); |
| 99 | + expect(instance.state.hoveredRowIndex).toEqual(-1); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('cellRenderer()', () => { |
| 104 | + test.each([ |
| 105 | + [{ columnIndex: 0, rowIndex: 2, key: 'key', style: {} }, true, false], |
| 106 | + [{ columnIndex: 1, rowIndex: 2, key: 'key', style: {} }, false, true], |
| 107 | + ])('should have correct class names', (arg, hasFileIconClass, hasFileNameClass) => { |
| 108 | + const cell = shallow(instance.cellRenderer(arg)); |
| 109 | + expect(cell.hasClass('bdl-MetadataBasedItemList-cell')).toEqual(true); |
| 110 | + expect(cell.hasClass('bdl-MetadataBasedItemList-cell--fileIcon')).toEqual(hasFileIconClass); |
| 111 | + expect(cell.hasClass('bdl-MetadataBasedItemList-cell--filename')).toEqual(hasFileNameClass); |
| 112 | + }); |
| 113 | + |
| 114 | + test('should have hovered class for adding background color on row hover', () => { |
| 115 | + const hoverCellIndex = 1; |
| 116 | + instance.handleMouseEnter(hoverCellIndex); // Hover over row |
| 117 | + |
| 118 | + const cell = shallow( |
| 119 | + instance.cellRenderer({ columnIndex: 0, rowIndex: hoverCellIndex, key: 'key', style: {} }), |
| 120 | + ); |
| 121 | + expect(cell.hasClass('bdl-MetadataBasedItemList-cell--hover')).toEqual(true); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('render()', () => { |
| 126 | + test('should render a default component correctly', () => { |
| 127 | + expect(wrapper.find('AutoSizer')).toBeTruthy(); |
| 128 | + expect(wrapper).toMatchSnapshot(); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments