|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import FilesListMenu from './index'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | + |
| 6 | +// Mock images data |
| 7 | +const mockAllImages = [ |
| 8 | + { name: 'Image1', processed: false }, |
| 9 | + { name: 'Image2', processed: true }, |
| 10 | + { name: 'Image3', processed: false } |
| 11 | +]; |
| 12 | + |
| 13 | +// Mock the useTranslation hook |
| 14 | +jest.mock('react-i18next', () => ({ |
| 15 | + useTranslation: () => ({ |
| 16 | + t: key => ({ |
| 17 | + "menu.images": "Images", |
| 18 | + }[key]), |
| 19 | + }), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('FilesListMenu', () => { |
| 23 | + const state = { |
| 24 | + "annotationType": "image", |
| 25 | + "showTags": true, |
| 26 | + "selectedTool": "create-polygon", |
| 27 | + "mode": null, |
| 28 | + "taskDescription": "dgdg", |
| 29 | + "settings": { |
| 30 | + "taskDescription": "dgdg", |
| 31 | + "taskChoice": "image_segmentation", |
| 32 | + "images": [ |
| 33 | + { |
| 34 | + "src": "http://127.0.0.1:5000/uploads/clothes.jpeg", |
| 35 | + "name": "clothes", |
| 36 | + "selectedClsList": "", |
| 37 | + "comment": "", |
| 38 | + "processed": false |
| 39 | + } |
| 40 | + ], |
| 41 | + "dataTask": null, |
| 42 | + "configuration": { |
| 43 | + "labels": [ |
| 44 | + { |
| 45 | + "id": "dfgd" |
| 46 | + }, |
| 47 | + { |
| 48 | + "id": "dfg" |
| 49 | + } |
| 50 | + ], |
| 51 | + "multipleRegions": true, |
| 52 | + "multipleRegionLabels": true, |
| 53 | + "regionTypesAllowed": [ |
| 54 | + "polygon", |
| 55 | + "bounding-box", |
| 56 | + "circle" |
| 57 | + ] |
| 58 | + } |
| 59 | + }, |
| 60 | + "labelImages": false, |
| 61 | + "regionClsList": [ |
| 62 | + "dfgd", |
| 63 | + "dfg" |
| 64 | + ], |
| 65 | + "regionColorList": [], |
| 66 | + "preselectCls": null, |
| 67 | + "regionTagList": [], |
| 68 | + "imageClsList": [], |
| 69 | + "imageTagList": [], |
| 70 | + "currentVideoTime": 0, |
| 71 | + "enabledTools": [ |
| 72 | + "create-box", |
| 73 | + "create-polygon", |
| 74 | + "create-circle" |
| 75 | + ], |
| 76 | + "history": [], |
| 77 | + "enabledRegionProps": [ |
| 78 | + "class", |
| 79 | + "comment" |
| 80 | + ], |
| 81 | + "selectedImage": 0, |
| 82 | + "images": [ |
| 83 | + { |
| 84 | + "src": "http://127.0.0.1:5000/uploads/clothes.jpeg", |
| 85 | + "name": "clothes", |
| 86 | + "selectedClsList": [ |
| 87 | + "dfgd" |
| 88 | + ], |
| 89 | + "comment": "", |
| 90 | + "processed": false |
| 91 | + } |
| 92 | + ], |
| 93 | + "lastAction": { |
| 94 | + "type": "SELECT_IMAGE", |
| 95 | + "imageIndex": 0, |
| 96 | + "image": { |
| 97 | + "src": "http://127.0.0.1:5000/uploads/clothes.jpeg", |
| 98 | + "name": "clothes", |
| 99 | + "selectedClsList": [ |
| 100 | + "dfgd" |
| 101 | + ], |
| 102 | + "comment": "", |
| 103 | + "processed": false |
| 104 | + } |
| 105 | + }, |
| 106 | + "selectedCls": "dfgd", |
| 107 | + "lastMouseMoveCall": 1718142223586 |
| 108 | +} |
| 109 | + it('renders correctly', () => { |
| 110 | + const onSelectJumpMock = jest.fn(); |
| 111 | + const saveActiveImageMock = jest.fn(); |
| 112 | + const onClickMock = jest.fn(); |
| 113 | + |
| 114 | + const { getByText, getAllByTestId } = render( |
| 115 | + <FilesListMenu |
| 116 | + state={state} |
| 117 | + selectedImage={'image1'} |
| 118 | + allImages={mockAllImages} |
| 119 | + onSelectJump={onSelectJumpMock} |
| 120 | + saveActiveImage={saveActiveImageMock} |
| 121 | + onClick={onClickMock} |
| 122 | + /> |
| 123 | + ); |
| 124 | + |
| 125 | + // Check if the component title is rendered |
| 126 | + expect(getByText(/Images \[3\]/)).toBeInTheDocument(); |
| 127 | + |
| 128 | + // Check if each image name is rendered |
| 129 | + mockAllImages.forEach(image => { |
| 130 | + expect(getByText(image.name)).toBeInTheDocument(); |
| 131 | + }); |
| 132 | + |
| 133 | + // Check if checkboxes are rendered for each image |
| 134 | + const checkboxes = getAllByTestId('checkbox'); |
| 135 | + expect(checkboxes).toHaveLength(mockAllImages.length); |
| 136 | + |
| 137 | + // Simulate click on an image |
| 138 | + fireEvent.click(getByText('Image1')); |
| 139 | + |
| 140 | + // Check if the onClick callback is called with the correct arguments |
| 141 | + expect(onClickMock).toHaveBeenCalledWith({"activeImage": {"comment": "", "name": "clothes", "processed": false, "selectedClsList": ["dfgd"], "src": "http://127.0.0.1:5000/uploads/clothes.jpeg"}, "currentImageIndex": 0, "pathToActiveImage": ["images", 0]}); |
| 142 | + |
| 143 | + // Check if the onSelectJump and saveActiveImage callbacks are called with the correct arguments |
| 144 | + expect(onSelectJumpMock).toHaveBeenCalledWith('Image1'); |
| 145 | + expect(saveActiveImageMock).toHaveBeenCalledWith({"comment": "", "name": "clothes", "processed": false, "selectedClsList": ["dfgd"], "src": "http://127.0.0.1:5000/uploads/clothes.jpeg"}); |
| 146 | + }); |
| 147 | +}); |
0 commit comments