forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request airbnb#786 from hshoff/harry-cov
test(vx-brush,vx-event): add code coverage
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 })); | ||
}); | ||
}); |