diff --git a/packages/vx-brush/package.json b/packages/vx-brush/package.json index f8e6c1a4b..5a4dadb21 100644 --- a/packages/vx-brush/package.json +++ b/packages/vx-brush/package.json @@ -38,5 +38,8 @@ "@vx/shape": "0.0.198", "classnames": "^2.2.5", "prop-types": "^15.6.1" + }, + "devDependencies": { + "@vx/scale": "^0.0.198" } } diff --git a/packages/vx-brush/test/utils.test.ts b/packages/vx-brush/test/utils.test.ts new file mode 100644 index 000000000..e7328264f --- /dev/null +++ b/packages/vx-brush/test/utils.test.ts @@ -0,0 +1,75 @@ +import { createScale } from '@vx/scale'; +import { getDomainFromExtent, scaleInvert } from '../src/utils'; + +describe('getDomainFromExtent()', () => { + test('it should return { start, end } if scale.invert', () => { + const scale = createScale({ domain: [0, 10], range: [2, 4] }); + const start = 0; + const end = 1; + const tolerentDelta = 0.5; + const result = getDomainFromExtent(scale, start, end, tolerentDelta); + expect(result.start).toBeDefined(); + expect(result.end).toBeDefined(); + expect(result.start).toEqual(scale.invert(start - tolerentDelta)); + expect(result.end).toEqual(scale.invert(end + tolerentDelta)); + }); + + test('it should handle start > end', () => { + const scale = createScale({ domain: [0, 10], range: [2, 4] }); + const start = 1; + const end = 0; + const tolerentDelta = 0.5; + const result = getDomainFromExtent(scale, start, end, tolerentDelta); + expect(result.start).toEqual(scale.invert(end - tolerentDelta)); + expect(result.end).toEqual(scale.invert(start + tolerentDelta)); + }); + + test('it should return { values } for band scales', () => { + const scale = createScale({ + type: 'band', + domain: ['a', 'b', 'c'], + range: [1.1, 3.5], + round: false, + }); + const domain = scale.domain(); + const start = 0; + const end = 1; + const tolerentDelta = 0.5; + const result = getDomainFromExtent(scale, start, end, tolerentDelta); + expect(result.values).toBeDefined(); + expect(result.values).toEqual([domain[0]]); + }); +}); + +describe('scaleInvert()', () => { + test('it should return scale.invert(value) if scale.invert', () => { + const scale = createScale({ domain: [0, 10], range: [2, 4] }); + const value = 3; + const result = scaleInvert(scale, value); + expect(result).toEqual(scale.invert(value)); + }); + + test('it should return the index of domain item for scales without invert (like band)', () => { + const scale = createScale({ + type: 'band', + domain: ['a', 'b', 'c'], + range: [1.1, 3.5], + round: false, + }); + const value = 3; + const result = scaleInvert(scale, value); + expect(result).toEqual(2); + }); + + test('it should handle band scales where end < start', () => { + const scale = createScale({ + type: 'band', + domain: ['a', 'b', 'c'], + range: [20, 1], + round: false, + }); + const value = 3; + const result = scaleInvert(scale, value); + expect(result).toEqual(2); + }); +}); diff --git a/packages/vx-event/test/getXandYFromEvent.test.ts b/packages/vx-event/test/getXandYFromEvent.test.ts new file mode 100644 index 000000000..9c04539e5 --- /dev/null +++ b/packages/vx-event/test/getXandYFromEvent.test.ts @@ -0,0 +1,31 @@ +import getXAndYFromEvent from '../src/getXAndYFromEvent'; + +describe('getXAndYFromEvent()', () => { + test('it should return { x: 0, y: 0 } if no event argument', () => { + const result = getXAndYFromEvent(); + // @ts-ignore + const result2 = getXAndYFromEvent(null); + expect(result).toEqual({ x: 0, y: 0 }); + expect(result2).toEqual({ x: 0, y: 0 }); + }); + + test('it should return { x, y } for mouse events', () => { + const e = { clientX: 0, clientY: 0 }; + const result = getXAndYFromEvent(e as MouseEvent); + expect(result).toEqual({ x: e.clientX, y: e.clientY }); + }); + + test('it should return { x, y } for touch events with changedTouches', () => { + const touch0 = { clientX: 0, clientY: 0 }; + const touch1 = { clientX: 1, clientY: 1 }; + const e = { changedTouches: [touch0, touch1] }; + const result = getXAndYFromEvent((e as unknown) as TouchEvent); + expect(result).toEqual({ x: touch0.clientX, y: touch0.clientY }); + }); + + test('it should return { x: 0, y: 0 } for touch events with no changedTouches', () => { + const e = { changedTouches: [] }; + const result = getXAndYFromEvent((e as unknown) as TouchEvent); + expect(result).toEqual({ x: 0, y: 0 }); + }); +}); diff --git a/packages/vx-event/test/localPoint.test.ts b/packages/vx-event/test/localPoint.test.ts index b12f6e9ba..554da7bb0 100644 --- a/packages/vx-event/test/localPoint.test.ts +++ b/packages/vx-event/test/localPoint.test.ts @@ -1,7 +1,50 @@ +import { Point } from '@vx/point'; import { localPoint } from '../src'; +import localPointGeneric from '../src/localPointGeneric'; describe('localPoint', () => { test('it should be defined', () => { expect(localPoint).toBeDefined(); }); + + test('it should return null if called with no arguments', () => { + // @ts-ignore + expect(localPoint()).toBeNull(); + // @ts-ignore + expect(localPointGeneric(document.createElement('div'))).toBeNull(); + }); + + test('it should handle localPoint(event) and get node from event.target', () => { + const e = new MouseEvent('test', { + clientX: 10, + clientY: 10, + }); + Object.defineProperty(e, 'target', { + writable: false, + value: { + clientLeft: 0, + clientTop: 0, + getBoundingClientRect: () => ({ left: 0, top: 0 }), + }, + }); + // @ts-ignore + const result = localPoint(e); + expect(result).toEqual(new Point({ x: 10, y: 10 })); + }); + + test('it should handle localPoint(node, event)', () => { + const e = new MouseEvent('test', { + clientX: 10, + clientY: 10, + }); + const node = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + // @ts-ignore + svg.createSVGPoint = () => ({ matrixTransform: () => ({ x: 10, y: 10 }) }); + // @ts-ignore + svg.getScreenCTM = () => ({ inverse: () => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }); + svg.appendChild(node); + const result = localPoint(node, e); + expect(result).toEqual(new Point({ x: 10, y: 10 })); + }); });