Skip to content
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
15 changes: 10 additions & 5 deletions src/drawing/DrawingCreator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function DrawingCreator({ className, onStart, onStop, stroke = de
const drawingSVGRef = React.useRef<DrawingSVGRef>(null);
const renderHandleRef = React.useRef<number | null>(null);

const getPoints = (): Array<Position> => {
const getPoints = React.useCallback((): Array<Position> => {
const { current: creatorEl } = creatorElRef;
const { current: points } = capturedPointsRef;

Expand All @@ -39,11 +39,16 @@ export default function DrawingCreator({ className, onStart, onStop, stroke = de
}

const { height, width } = creatorEl.getBoundingClientRect();
const { size: minSize } = stroke;
const MAX_X = width - minSize;
const MAX_Y = height - minSize;

return points.map(({ x, y }) => ({
x: (x / width) * 100,
y: (y / height) * 100,
x: (Math.min(MAX_X, Math.max(minSize, x)) / width) * 100,
y: (Math.min(MAX_Y, Math.max(minSize, y)) / height) * 100,
}));
};
}, [stroke]);

const getPosition = (x: number, y: number): [number, number] => {
const { current: creatorEl } = creatorElRef;

Expand Down Expand Up @@ -84,7 +89,7 @@ export default function DrawingCreator({ className, onStart, onStop, stroke = de
paths: [{ points: adjustedPoints }],
stroke,
});
}, [onStop, setDrawingStatus, stroke]);
}, [getPoints, onStop, setDrawingStatus, stroke]);

const updateDraw = React.useCallback(
(x: number, y: number): void => {
Expand Down
94 changes: 77 additions & 17 deletions src/drawing/__tests__/DrawingCreator-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,28 @@ describe('DrawingCreator', () => {
jest.spyOn(Element.prototype, 'setAttribute');
});

const simulateDrawStart = (wrapper: ReactWrapper<PointerCaptureProps, {}>, clientX = 10, clientY = 10): void =>
const simulateDrawStart = (wrapper: ReactWrapper<PointerCaptureProps, {}>, clientX = 10, clientY = 10): void => {
act(() => {
wrapper.prop('onDrawStart')(clientX, clientY);
});
const simulateDrawMove = (wrapper: ReactWrapper<PointerCaptureProps, {}>, clientX = 10, clientY = 10): void =>

wrapper.update();
};
const simulateDrawMove = (wrapper: ReactWrapper<PointerCaptureProps, {}>, clientX = 10, clientY = 10): void => {
act(() => {
wrapper.prop('onDrawUpdate')(clientX, clientY);
});
const simulateDrawStop = (wrapper: ReactWrapper<PointerCaptureProps, {}>): void =>

wrapper.update();
};
const simulateDrawStop = (wrapper: ReactWrapper<PointerCaptureProps, {}>): void => {
act(() => {
wrapper.prop('onDrawStop')();
});

wrapper.update();
};

describe('render', () => {
test('should render PointerCapture', () => {
const wrapper = getWrapper();
Expand All @@ -60,8 +69,6 @@ describe('DrawingCreator', () => {

simulateDrawStart(wrapper.find(PointerCapture));

wrapper.update();

expect(wrapper.find(PointerCapture).exists()).toBe(true);
expect(wrapper.find(DrawingSVG).exists()).toBe(true);
});
Expand All @@ -71,8 +78,6 @@ describe('DrawingCreator', () => {

simulateDrawMove(wrapper.find(PointerCapture));

wrapper.update();

expect(wrapper.find(PointerCapture).exists()).toBe(true);
expect(wrapper.find(DrawingSVG).exists()).toBe(true);
});
Expand All @@ -85,8 +90,6 @@ describe('DrawingCreator', () => {

simulateDrawStart(wrapper.find(PointerCapture));

wrapper.update();

expect(onStart).not.toHaveBeenCalled();
});

Expand All @@ -96,8 +99,6 @@ describe('DrawingCreator', () => {

simulateDrawMove(wrapper.find(PointerCapture));

wrapper.update();

expect(onStart).toHaveBeenCalled();
});
});
Expand All @@ -108,15 +109,15 @@ describe('DrawingCreator', () => {
const wrapper = getWrapper({ onStop });

simulateDrawStart(wrapper.find(PointerCapture));
wrapper.update();

expect(onStop).not.toHaveBeenCalled();

simulateDrawMove(wrapper.find(PointerCapture), 15, 15);
wrapper.update();

expect(onStop).not.toHaveBeenCalled();

simulateDrawStop(wrapper.find(PointerCapture));
wrapper.update();

expect(onStop).toHaveBeenCalledWith({
paths: [
{
Expand All @@ -129,6 +130,68 @@ describe('DrawingCreator', () => {
stroke: defaultStroke,
});
});

test.each`
x | y | expectedX | expectedY
${-1} | ${-1} | ${4} | ${4}
${110} | ${110} | ${96} | ${96}
`(
'should bound the drawn points ($x, $y) by the boundary of the PointerCapture element',
({ x, y, expectedX, expectedY }) => {
const onStop = jest.fn();
const wrapper = getWrapper({ onStop });

simulateDrawStart(wrapper.find(PointerCapture));

expect(onStop).not.toHaveBeenCalled();

simulateDrawMove(wrapper.find(PointerCapture), x, y);

expect(onStop).not.toHaveBeenCalled();

simulateDrawStop(wrapper.find(PointerCapture));

expect(onStop).toHaveBeenCalledWith({
paths: [
{
points: [
{ x: 10, y: 10 },
{ x: expectedX, y: expectedY },
],
},
],
stroke: defaultStroke,
});
},
);

test('should bound the drawn points by the boundary of the PointerCapture element based on the provided stroke size', () => {
const customStroke = { color: '#000', size: 10 };
const onStop = jest.fn();
const wrapper = getWrapper({ onStop, stroke: customStroke });

simulateDrawStart(wrapper.find(PointerCapture));

expect(onStop).not.toHaveBeenCalled();

simulateDrawMove(wrapper.find(PointerCapture), -1, -1);

expect(onStop).not.toHaveBeenCalled();

simulateDrawStop(wrapper.find(PointerCapture));

expect(onStop).toHaveBeenCalledWith({
paths: [
{
points: [
{ x: 10, y: 10 },
{ x: 10, y: 10 },
],
},
],
stroke: customStroke,
});
});
});

describe('svg path rendering', () => {
Expand All @@ -142,7 +205,6 @@ describe('DrawingCreator', () => {
const wrapper = getWrapper();

simulateDrawStart(wrapper.find(PointerCapture));
wrapper.update();
jest.advanceTimersByTime(100);

expect(window.requestAnimationFrame).toHaveBeenCalled();
Expand All @@ -162,8 +224,6 @@ describe('DrawingCreator', () => {
simulateDrawMove(wrapper.find(PointerCapture), 15, 15);
simulateDrawMove(wrapper.find(PointerCapture), 20, 20);

wrapper.update();

jest.advanceTimersByTime(100);

expect(window.requestAnimationFrame).toHaveBeenCalled();
Expand Down