Answer the question
In order to leave comments, you need to log in
How to test a private function?
there is such a function
private createMark(marginFromBegin: number, settings: ISettings): HTMLElement {
const mark = createElement('span', 'range-slider__scale-mark');
if (settings.isVertical) {
mark.className += ' range-slider__scale-mark_vertical';
mark.style.marginTop = `${marginFromBegin}px`;
} else {
mark.style.marginLeft = `${marginFromBegin}px`;
}
return mark;
}
import { ISettings } from '../RangeSlider/types';
import Scale from './Scale';
let settings: ISettings;
beforeEach(() => {
settings = {
min: 0,
max: 1500,
isTwoRunners: true,
isScaleVisible: true,
isVertical: false,
isTooltipsVisible: true,
valueFrom: 1000,
valueTo: 1490,
step: 10,
};
});
function getPrivateMethod(methodName: string, marginFromBegin: number, settings: ISettings) {
const scaleProto = Scale.prototype as any;
const instance = Object.create(scaleProto);
return () => scaleProto.constructor[methodName].call(instance, marginFromBegin, settings);
}
describe('private createMark method', () => {
test('should return html element', () => {
settings.isVertical = true;
const result = getPrivateMethod('createMark', 100, settings);
console.log(result);
expect(result).not.toBeNull();
});
});
Answer the question
In order to leave comments, you need to log in
scaleProto.constructor[methodName]is the method static?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question