|
| 1 | +import Tool from './CobbAngleTool.js'; |
| 2 | +import { getToolState } from '../../stateManagement/toolState.js'; |
| 3 | + |
| 4 | +jest.mock('./../../stateManagement/toolState.js', () => ({ |
| 5 | + getToolState: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +jest.mock('./../../importInternal.js', () => ({ |
| 9 | + default: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('./../../externalModules.js', () => ({ |
| 13 | + cornerstone: { |
| 14 | + metaData: { |
| 15 | + get: jest.fn(), |
| 16 | + }, |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +const goodMouseEventData = { |
| 21 | + currentPoints: { |
| 22 | + image: { |
| 23 | + x: 0, |
| 24 | + y: 0, |
| 25 | + }, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +const image = { |
| 30 | + rowPixelSpacing: 0.8984375, |
| 31 | + columnPixelSpacing: 0.8984375, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('CobbAngleTool.js', () => { |
| 35 | + describe('default values', () => { |
| 36 | + it('has a default name of "CobbAngle"', () => { |
| 37 | + const defaultName = 'CobbAngle'; |
| 38 | + const instantiatedTool = new Tool(); |
| 39 | + |
| 40 | + expect(instantiatedTool.name).toEqual(defaultName); |
| 41 | + }); |
| 42 | + |
| 43 | + it('can be created with a custom tool name', () => { |
| 44 | + const customToolName = { name: 'customToolName' }; |
| 45 | + const instantiatedTool = new Tool(customToolName); |
| 46 | + |
| 47 | + expect(instantiatedTool.name).toEqual(customToolName.name); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('createNewMeasurement', () => { |
| 52 | + it('returns aa new measurement object', () => { |
| 53 | + const instantiatedTool = new Tool('CobbAngle'); |
| 54 | + |
| 55 | + const toolMeasurement = instantiatedTool.createNewMeasurement( |
| 56 | + goodMouseEventData |
| 57 | + ); |
| 58 | + |
| 59 | + expect(typeof toolMeasurement).toBe(typeof {}); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns a measurement with a start, end, start2 and end2 handles at the eventData's x and y", () => { |
| 63 | + const instantiatedTool = new Tool('toolName'); |
| 64 | + |
| 65 | + const toolMeasurement = instantiatedTool.createNewMeasurement( |
| 66 | + goodMouseEventData |
| 67 | + ); |
| 68 | + const startHandle = { |
| 69 | + x: toolMeasurement.handles.start.x, |
| 70 | + y: toolMeasurement.handles.start.y, |
| 71 | + }; |
| 72 | + const endHandle = { |
| 73 | + x: toolMeasurement.handles.end.x, |
| 74 | + y: toolMeasurement.handles.end.y, |
| 75 | + }; |
| 76 | + const start2Handle = { |
| 77 | + x: toolMeasurement.handles.start2.x, |
| 78 | + y: toolMeasurement.handles.start2.y, |
| 79 | + }; |
| 80 | + const end2Handle = { |
| 81 | + x: toolMeasurement.handles.end2.x, |
| 82 | + y: toolMeasurement.handles.end2.y, |
| 83 | + }; |
| 84 | + |
| 85 | + expect(startHandle.x).toBe(goodMouseEventData.currentPoints.image.x); |
| 86 | + expect(startHandle.y).toBe(goodMouseEventData.currentPoints.image.y); |
| 87 | + expect(start2Handle.x).toBe(goodMouseEventData.currentPoints.image.x); |
| 88 | + expect(start2Handle.y).toBe(goodMouseEventData.currentPoints.image.y); |
| 89 | + expect(endHandle.x).toBe(goodMouseEventData.currentPoints.image.x); |
| 90 | + expect(endHandle.y).toBe(goodMouseEventData.currentPoints.image.y); |
| 91 | + expect(end2Handle.x).toBe(goodMouseEventData.currentPoints.image.x + 1); |
| 92 | + expect(end2Handle.y).toBe(goodMouseEventData.currentPoints.image.y); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns a measurement with a textBox handle', () => { |
| 96 | + const instantiatedTool = new Tool('toolName'); |
| 97 | + |
| 98 | + const toolMeasurement = instantiatedTool.createNewMeasurement( |
| 99 | + goodMouseEventData |
| 100 | + ); |
| 101 | + |
| 102 | + expect(typeof toolMeasurement.handles.textBox).toBe(typeof {}); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('pointNearTool', () => { |
| 107 | + let element, coords; |
| 108 | + |
| 109 | + beforeEach(() => { |
| 110 | + element = jest.fn(); |
| 111 | + coords = jest.fn(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns false when measurement data is not visible', () => { |
| 115 | + const instantiatedTool = new Tool('AngleTool'); |
| 116 | + const notVisibleMeasurementData = { |
| 117 | + visible: false, |
| 118 | + }; |
| 119 | + |
| 120 | + const isPointNearTool = instantiatedTool.pointNearTool( |
| 121 | + element, |
| 122 | + notVisibleMeasurementData, |
| 123 | + coords |
| 124 | + ); |
| 125 | + |
| 126 | + expect(isPointNearTool).toBe(false); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('updateCachedStats', () => { |
| 131 | + let element; |
| 132 | + |
| 133 | + beforeEach(() => { |
| 134 | + element = jest.fn(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should calculate and update annotation value', () => { |
| 138 | + const instantiatedTool = new Tool('AngleTool'); |
| 139 | + |
| 140 | + const data = { |
| 141 | + handles: { |
| 142 | + start: { |
| 143 | + x: 166, |
| 144 | + y: 90, |
| 145 | + }, |
| 146 | + end: { |
| 147 | + x: 120, |
| 148 | + y: 113, |
| 149 | + }, |
| 150 | + start2: { |
| 151 | + x: 120, |
| 152 | + y: 113, |
| 153 | + }, |
| 154 | + end2: { |
| 155 | + x: 145, |
| 156 | + y: 143, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }; |
| 160 | + |
| 161 | + instantiatedTool.updateCachedStats(image, element, data); |
| 162 | + expect(data.rAngle).toBe(76.76); |
| 163 | + expect(data.invalidated).toBe(false); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('renderToolData', () => { |
| 168 | + it('returns undefined when no toolData exists for the tool', () => { |
| 169 | + const instantiatedTool = new Tool('AngleTool'); |
| 170 | + const mockEvent = { |
| 171 | + detail: { |
| 172 | + enabledElement: undefined, |
| 173 | + }, |
| 174 | + }; |
| 175 | + |
| 176 | + getToolState.mockReturnValueOnce(undefined); |
| 177 | + |
| 178 | + const renderResult = instantiatedTool.renderToolData(mockEvent); |
| 179 | + |
| 180 | + expect(renderResult).toBe(undefined); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments