Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ellipticalROI): give default values for cachedStats values to avoid undefined error #1537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } = {},
md-prog marked this conversation as resolved.
Show resolved Hide resolved
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 } = {},
md-prog marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
});
});