style(prettier): format code style

This commit is contained in:
unix
2020-05-06 14:18:28 +08:00
parent cf8e277324
commit 112c826575
263 changed files with 4927 additions and 3992 deletions

View File

@@ -82,13 +82,15 @@ describe('UseBodyScroll', () => {
expect(el.style.overflow).not.toEqual('hidden')
expect(event.preventDefault).toHaveBeenCalled()
// Touch events with multiple fingers do nothing
document.dispatchEvent(new TouchEvent('touchmove', {
touches: [{}, {}, {}] as Array<Touch>
}))
document.dispatchEvent(
new TouchEvent('touchmove', {
touches: [{}, {}, {}] as Array<Touch>,
}),
)
expect(event.preventDefault).toHaveBeenCalledTimes(1)
act(() => result.current[1](false))
;(window.navigator as any).platform = ''
})
@@ -111,7 +113,6 @@ describe('UseBodyScroll', () => {
expect(el.style.overflow).toEqual('hidden')
expect(event.preventDefault).not.toHaveBeenCalled()
act(() => result.current[1](false))
;(window.navigator as any).platform = ''
})
@@ -122,23 +123,23 @@ describe('UseBodyScroll', () => {
_ref = ref
const el = ref.current as HTMLDivElement
const { result, rerender } = renderHook(() => useBodyScroll(_ref))
act(() => result.current[1](true))
expect(el.style.overflow).toEqual('hidden')
// Force tigger rerender at the same value
_ref = React.createRef<HTMLDivElement>()
rerender()
_ref = ref
rerender()
act(() => result.current[1](true))
expect(el.style.overflow).toEqual('hidden')
})
it('should set body when missing all params', () => {
const { result } = renderHook(() => useBodyScroll())
act(() => result.current[1](true))
expect(document.body.style.overflow).toEqual('hidden')
})

View File

@@ -3,11 +3,13 @@ import { renderHook } from '@testing-library/react-hooks'
import useClickAway from '../use-click-away'
const simulateNativeClick = (el: Element) => {
el.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
}))
el.dispatchEvent(
new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
}),
)
}
describe('UseClickAway', () => {
@@ -18,18 +20,18 @@ describe('UseClickAway', () => {
const el = ref.current as HTMLDivElement
document.body.appendChild(el)
renderHook(() => useClickAway(ref, handler))
simulateNativeClick(el)
expect(handler).not.toHaveBeenCalled()
simulateNativeClick(document.body)
expect(handler).toHaveBeenCalled()
})
it('should no errors when element missing', () => {
const errorSpy = jest.spyOn(console, 'error')
const ref = React.createRef<HTMLDivElement>()
renderHook(() => useClickAway(ref, () => {}))
expect(errorSpy).not.toHaveBeenCalled()
})
})

View File

@@ -3,17 +3,15 @@ import { renderHook } from '@testing-library/react-hooks'
describe('UseClipboard', () => {
beforeAll(() => {
window.getSelection = jest.fn()
.mockImplementation(() => ({
removeAllRanges: jest.fn(),
addRange: jest.fn(),
}))
document.createRange = jest.fn()
.mockImplementation(() => ({
selectNode: jest.fn(),
}))
window.getSelection = jest.fn().mockImplementation(() => ({
removeAllRanges: jest.fn(),
addRange: jest.fn(),
}))
document.createRange = jest.fn().mockImplementation(() => ({
selectNode: jest.fn(),
}))
})
it('should work correctly', () => {
document.execCommand = jest.fn()
const { result } = renderHook(() => useClipboard())
@@ -21,44 +19,37 @@ describe('UseClipboard', () => {
expect(document.execCommand).toHaveBeenCalled()
;(document.execCommand as jest.Mock).mockClear()
})
it('should capture copy erros', () => {
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {})
document.execCommand = jest.fn()
.mockImplementation(() => {
throw new Error('test')
})
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
document.execCommand = jest.fn().mockImplementation(() => {
throw new Error('test')
})
const { result } = renderHook(() => useClipboard())
result.current.copy('space speace breaks $@#')
expect(errorSpy).toHaveBeenCalled()
;(document.execCommand as jest.Mock).mockClear()
errorSpy.mockRestore()
})
it('should work correctly without text', () => {
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {})
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
document.execCommand = jest.fn()
const { result } = renderHook(() => useClipboard())
result.current.copy('')
expect(errorSpy).not.toHaveBeenCalled()
;(document.execCommand as jest.Mock).mockClear()
errorSpy.mockRestore()
})
it('should not throw errors when the browser does not support', () => {
window.getSelection = jest.fn()
.mockImplementation(() => undefined)
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {})
window.getSelection = jest.fn().mockImplementation(() => undefined)
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
document.execCommand = jest.fn()
const { result } = renderHook(() => useClipboard())
result.current.copy('test')
expect(errorSpy).not.toHaveBeenCalled()
;(document.execCommand as jest.Mock).mockClear()
;(window.getSelection as jest.Mock).mockClear()
errorSpy.mockRestore()

View File

@@ -7,21 +7,21 @@ describe('UseCurrentState', () => {
it('should work correctly', () => {
const { result } = renderHook(() => useCurrentState(''))
expect(result.current[0]).toEqual('')
act(() => result.current[1]('test'))
expect(result.current[0]).toEqual('test')
expect(result.current[2].current).toEqual('test')
})
it('functional update mode should be supported', () => {
const { result } = renderHook(() => useCurrentState(0))
expect(result.current[0]).toEqual(0)
act(() => result.current[1](pre => pre + 10))
act(() => result.current[1]((pre) => pre + 10))
expect(result.current[0]).toEqual(10)
expect(result.current[2].current).toEqual(10)
})
it('only ref should track latest value', () => {
const Mock: React.FC<{}> = () => {
const [state, setState, stateRef] = useCurrentState('')

View File

@@ -38,7 +38,7 @@ const useBodyScroll = (
...defaultOptions,
...(options || {}),
}
// don't prevent touch event when layer contain scroll
const isIosWithCustom = () => {
if (safeOptions.scrollLayer) return false
@@ -60,7 +60,7 @@ const useBodyScroll = (
})
return
}
// reset element overflow
if (!elementStack.has(elRef.current)) return
if (!isIosWithCustom()) {

View File

@@ -14,19 +14,17 @@ const defaultOptions: UseClipboardOptions = {
onError: () => useWarning('Failed to copy.', 'use-clipboard'),
}
const useClipboard = (
options: UseClipboardOptions = defaultOptions,
): UseClipboardResult => {
const useClipboard = (options: UseClipboardOptions = defaultOptions): UseClipboardResult => {
const el = usePortal('clipboard')
const copyText = (el: HTMLElement | null, text: string) => {
if (!el || !text) return
const selection = window.getSelection()
if (!selection) return
el.style.whiteSpace = 'pre'
el.textContent = text
const range = window.document.createRange()
selection.removeAllRanges()
range.selectNode(el)
@@ -36,16 +34,19 @@ const useClipboard = (
} catch (e) {
options.onError && options.onError()
}
selection.removeAllRanges()
if (el) {
el.textContent = ''
}
}
const copy = useCallback((text: string) => {
copyText(el ,text)
}, [el])
const copy = useCallback(
(text: string) => {
copyText(el, text)
},
[el],
)
return { copy }
}

View File

@@ -1,10 +1,8 @@
import { Dispatch, MutableRefObject, SetStateAction, useEffect, useRef, useState } from 'react'
export type CurrentStateType<S> = [
S, Dispatch<SetStateAction<S>>, MutableRefObject<S>
]
export type CurrentStateType<S> = [S, Dispatch<SetStateAction<S>>, MutableRefObject<S>]
const useCurrentState = <S,>(initialState: S): CurrentStateType<S> => {
const useCurrentState = <S>(initialState: S): CurrentStateType<S> => {
const [state, setState] = useState<S>(initialState as S)
const ref = useRef<S>(initialState as S)
@@ -13,7 +11,7 @@ const useCurrentState = <S,>(initialState: S): CurrentStateType<S> => {
}, [state])
const setValue = (val: SetStateAction<S>) => {
const result = typeof val === 'function' ? (val as ((prevState: S) => S))(ref.current) : val
const result = typeof val === 'function' ? (val as (prevState: S) => S)(ref.current) : val
ref.current = result
setState(result)
}