Skip to content

Commit

Permalink
added new tests to increaes coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
md-prog committed Mar 27, 2023
1 parent 26484c6 commit 3e5b110
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
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);
});
});
});
});
81 changes: 81 additions & 0 deletions src/tools/annotation/RectangleRoiTool.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
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('../../drawing/drawRect', () => ({
__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-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 +235,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 +262,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 3e5b110

Please sign in to comment.