Skip to content

Commit

Permalink
fix(ellipticalROI): give default values for cachedStats values to avo…
Browse files Browse the repository at this point in the history
…id undefined error (#1537)

* [bugfix] give default values for cachedStats values to avoid undefined error

* coding rule fix

* added new tests to increaes coverage

* test refactor
  • Loading branch information
md-prog authored Mar 27, 2023
1 parent 925a3dd commit 79d0ee1
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/tools/annotation/EllipticalRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function _getUnit(modality, showHounsfieldUnits) {
function _createTextBoxContent(
context,
isColorImage,
{ area, mean, stdDev, min, max, meanStdDevSUV } = {},
{ area = 0, mean = 0, stdDev = 0, min = 0, max = 0, meanStdDevSUV = 0 } = {},
modality,
hasPixelSpacing,
options = {}
Expand Down
81 changes: 81 additions & 0 deletions src/tools/annotation/EllipticalRoiTool.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import EllipticalRoiTool from './EllipticalRoiTool.js';
import { getToolState } from './../../stateManagement/toolState.js';
import { getLogger } from '../../util/logger.js';
import getNewContext from '../../drawing/getNewContext.js';
import drawEllipse from '../../drawing/drawEllipse.js';

/* ~ Setup
* To mock properly, Jest needs jest.mock('moduleName') to be in the
* same scope as the require/import statement.
*/
import external from '../../externalModules.js';

jest.mock('../../util/logger.js');
jest.mock('./../../stateManagement/toolState.js', () => ({
getToolState: jest.fn(),
}));
jest.mock('../../drawing/drawEllipse', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock('../../drawing/getNewContext', () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock('./../../importInternal.js', () => ({
default: jest.fn(),
Expand All @@ -19,6 +35,7 @@ jest.mock('./../../externalModules.js', () => ({
/* eslint-disable prettier/prettier */
getPixels: () => [100, 100, 100, 100, 4, 5, 100, 3, 6],
/* eslint-enable prettier/prettier */
pixelToCanvas: jest.fn(),
},
}));

Expand Down Expand Up @@ -215,6 +232,20 @@ describe('EllipticalRoiTool.js', () => {
});

describe('renderToolData', () => {
beforeAll(() => {
getNewContext.mockReturnValue({
save: jest.fn(),
restore: jest.fn(),
beginPath: jest.fn(),
arc: jest.fn(),
stroke: jest.fn(),
fillRect: jest.fn(),
fillText: jest.fn(),
measureText: jest.fn(() => ({ width: 1 })),
});
external.cornerstone.pixelToCanvas.mockImplementation((comp, val) => val);
});

it('returns undefined when no toolData exists for the tool', () => {
const instantiatedTool = new EllipticalRoiTool();
const mockEvent = {
Expand All @@ -228,5 +259,55 @@ describe('EllipticalRoiTool.js', () => {

expect(renderResult).toBe(undefined);
});

describe('draw ellipse with color', () => {
const defaulColor = 'white';
const mockEvent = {
detail: {
element: {},
canvasContext: {
canvas: {},
},
image: {},
viewport: {},
},
};
const instantiatedTool = new EllipticalRoiTool({
configuration: {},
});

const toolState = {
data: [
{
visible: true,
active: false,
handles: {
start: {
x: 0,
y: 0,
},
end: {
x: 3,
y: 3,
},
textBox: {},
},
},
],
};

const expectDraw = color => {
expect(drawEllipse.mock.calls.length).toBe(1);
};

it('should draw an ellipse with the inactive color', () => {
toolState.data[0].active = false;
getToolState.mockReturnValue(toolState);

instantiatedTool.renderToolData(mockEvent);

expectDraw(defaulColor);
});
});
});
});
2 changes: 1 addition & 1 deletion src/tools/annotation/RectangleRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function _getUnit(modality, showHounsfieldUnits) {
function _createTextBoxContent(
context,
isColorImage,
{ area, mean, stdDev, min, max, meanStdDevSUV },
{ area = 0, mean = 0, stdDev = 0, min = 0, max = 0, meanStdDevSUV = 0 } = {},
modality,
hasPixelSpacing,
options = {}
Expand Down
81 changes: 79 additions & 2 deletions src/tools/annotation/RectangleRoiTool.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import RectangleRoiTool from './RectangleRoiTool.js';
import { getToolState } from './../../stateManagement/toolState.js';
import { getLogger } from '../../util/logger.js';
import getNewContext from '../../drawing/getNewContext.js';
import drawRect from '../../drawing/drawRect.js';

/* ~ Setup
* To mock properly, Jest needs jest.mock('moduleName') to be in the
* same scope as the require/import statement.
*/
import external from '../../externalModules.js';

jest.mock('../../util/logger.js');
jest.mock('./../../stateManagement/toolState.js', () => ({
getToolState: jest.fn(),
}));

jest.mock('./../../importInternal.js', () => ({
jest.mock('../../drawing/drawRect', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock('../../drawing/getNewContext', () => ({
__esModule: true,
default: jest.fn(),
}));

Expand All @@ -19,6 +31,7 @@ jest.mock('./../../externalModules.js', () => ({
/* eslint-enable prettier/prettier */
getPixels: () => [100, 100, 100, 100, 4, 5, 100, 3, 6],
/* eslint-enable prettier/prettier */
pixelToCanvas: jest.fn(),
},
}));

Expand Down Expand Up @@ -218,6 +231,20 @@ describe('RectangleRoiTool.js', () => {
});

describe('renderToolData', () => {
beforeAll(() => {
getNewContext.mockReturnValue({
save: jest.fn(),
restore: jest.fn(),
beginPath: jest.fn(),
arc: jest.fn(),
stroke: jest.fn(),
fillRect: jest.fn(),
fillText: jest.fn(),
measureText: jest.fn(() => ({ width: 1 })),
});
external.cornerstone.pixelToCanvas.mockImplementation((comp, val) => val);
});

it('returns undefined when no toolData exists for the tool', () => {
const instantiatedTool = new RectangleRoiTool();
const mockEvent = {
Expand All @@ -231,5 +258,55 @@ describe('RectangleRoiTool.js', () => {

expect(renderResult).toBe(undefined);
});

describe('draw rectangle with color', () => {
const defaulColor = 'white';
const mockEvent = {
detail: {
element: {},
canvasContext: {
canvas: {},
},
image: {},
viewport: {},
},
};
const instantiatedTool = new RectangleRoiTool({
configuration: {},
});

const toolState = {
data: [
{
visible: true,
active: false,
handles: {
start: {
x: 0,
y: 0,
},
end: {
x: 3,
y: 3,
},
textBox: {},
},
},
],
};

const expectDraw = color => {
expect(drawRect.mock.calls.length).toBe(1);
};

it('should draw a rectangle with the inactive color', () => {
toolState.data[0].active = false;
getToolState.mockReturnValue(toolState);

instantiatedTool.renderToolData(mockEvent);

expectDraw(defaulColor);
});
});
});
});

0 comments on commit 79d0ee1

Please sign in to comment.