use-scan-detection
Advanced tools
Comparing version 0.1.4 to 0.2.0
{ | ||
"name": "use-scan-detection", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "A react hook for detecting barcode scanner input.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,6 +9,2 @@ import { renderHook, act } from '@testing-library/react-hooks' | ||
beforeEach(() => { | ||
jest.spyOn(performance, 'now').mockImplementation(() => Math.random() * 50) | ||
}) | ||
afterEach(() => { | ||
jest.clearAllTimers() | ||
@@ -27,2 +23,6 @@ }) | ||
{ | ||
keyCode: 51, | ||
key: "3" | ||
}, | ||
{ | ||
keyCode: 13, | ||
@@ -33,3 +33,3 @@ key: "Enter" | ||
it("should call onComplete on a complete scan", () => { | ||
it("should call onComplete on a complete code", () => { | ||
const config = { | ||
@@ -51,6 +51,27 @@ onComplete: jest.fn() | ||
expect(config.onComplete) | ||
.toBeCalledWith("12") | ||
.toBeCalledWith("123") | ||
}) | ||
it("should call onError on a partial scan", () => { | ||
it('should evaluate after timeToEvaluate has passed from the last character', () => { | ||
const config = { | ||
onComplete: jest.fn() | ||
} | ||
const result = renderHook(() => useScanDetection(config)) | ||
events.slice(0, -1).forEach(event => { | ||
document.dispatchEvent(event) | ||
act(() => { | ||
result.rerender() | ||
}) | ||
}) | ||
jest.advanceTimersToNextTimer() | ||
expect(config.onComplete) | ||
.toHaveBeenCalled() | ||
expect(config.onComplete) | ||
.toBeCalledWith("123") | ||
}) | ||
it("should call onError on an incomplete code", () => { | ||
const config = { | ||
onComplete: jest.fn(), | ||
@@ -73,3 +94,3 @@ onError: jest.fn(), | ||
}) | ||
it("should be incomplete if keys are too far apart", () => { | ||
it("should not call onComplete or onError if keypresses are too far apart", () => { | ||
const config = { | ||
@@ -80,7 +101,6 @@ onError: jest.fn(), | ||
jest.spyOn(performance, 'now').mockImplementation(() => 100) | ||
const result = renderHook(() => useScanDetection(config)) | ||
events.forEach(event => { | ||
events.forEach((event, k) => { | ||
jest.spyOn(performance, "now").mockImplementationOnce(() => 250 * k) | ||
document.dispatchEvent(event) | ||
@@ -90,6 +110,6 @@ act(() => { | ||
}) | ||
jest.runAllTimers() | ||
}) | ||
expect(config.onError) | ||
.not | ||
.toHaveBeenCalled() | ||
@@ -100,3 +120,3 @@ expect(config.onComplete) | ||
}) | ||
it("should wait for startCharacter to be inputted", () => { | ||
it("should wait for startCharacter to be inputted before buffering", () => { | ||
const config = { | ||
@@ -116,7 +136,8 @@ onComplete: jest.fn(), | ||
expect(config.onComplete) | ||
.toBeCalledWith("2") | ||
.toBeCalledWith("23") | ||
}) | ||
it("should ignore input on ignoreIfFocusOn when focused", () => { | ||
it("should ignore keypress events when element in ignoreIfFocusOn is focused", () => { | ||
const config = { | ||
onComplete: jest.fn(), | ||
onError: jest.fn(), | ||
ignoreIfFocusOn: document | ||
@@ -136,5 +157,8 @@ } | ||
.toBeCalled() | ||
expect(config.onError) | ||
.not | ||
.toBeCalled() | ||
}) | ||
it("should cleanup any timers", () => { | ||
it("should cleanup any timers on unmount", () => { | ||
const config = { | ||
@@ -172,7 +196,7 @@ onComplete: jest.fn() | ||
const mockPreventDefault = jest.spyOn(event, "preventDefault") | ||
document.dispatchEvent(event) | ||
expect(mockPreventDefault) | ||
.toHaveBeenCalled() | ||
.toHaveBeenCalled() | ||
}) | ||
@@ -190,3 +214,3 @@ it('should respect stopPropagation', () => { | ||
const mockStopPropagation = jest.spyOn(event, "stopPropagation") | ||
document.dispatchEvent(event) | ||
@@ -193,0 +217,0 @@ |
@@ -65,8 +65,9 @@ import { | ||
} | ||
const evaluateBuffer = () => { | ||
clearTimeout() | ||
clearTimeout(timeout.current) | ||
const sum = buffer.current | ||
.reduce((result, { time }) => result + time, 0) | ||
const avg = sum / buffer.current.length | ||
.map(({ time }, k, arr) => k > 0 ? time - arr[k - 1].time : 0) | ||
.slice(1) | ||
.reduce((total, delta) => total + delta, 0) | ||
const avg = sum / (buffer.current.length - 1) | ||
@@ -77,10 +78,10 @@ const code = buffer.current | ||
.join("") | ||
if ( | ||
avg <= averageWaitTime | ||
&& buffer.current.length >= minLength | ||
&& buffer.current.slice(startCharacter.length > 0 ? 1 : 0).length >= minLength | ||
) { | ||
onComplete(code) | ||
} else { | ||
!!onError && onError(code) | ||
avg <= averageWaitTime && !!onError && onError(code) | ||
} | ||
@@ -87,0 +88,0 @@ clearBuffer() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19086
407