Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
31 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 |
---|---|---|
@@ -1,37 +1,74 @@ | ||
import * as React from 'react'; | ||
import {createRef} from "react"; | ||
import {mount} from 'enzyme'; | ||
import {createCallbackRef, useCallbackRef} from "../src"; | ||
import {createCallbackRef, mergeRefs, useCallbackRef} from "../src"; | ||
|
||
|
||
describe('Specs', () => { | ||
it('base - createRef', () => { | ||
const ref = React.createRef<HTMLDivElement>(); | ||
mount(<div ref={ref}>test</div>); | ||
expect(ref.current).not.toBe(null); | ||
}); | ||
describe('useCallbackRef', () => { | ||
it('base - createRef', () => { | ||
const ref = React.createRef<HTMLDivElement>(); | ||
mount(<div ref={ref}>test</div>); | ||
expect(ref.current).not.toBe(null); | ||
}); | ||
|
||
it('shall work as createRef', () => { | ||
const spy = jest.fn(); | ||
const ref = createCallbackRef<HTMLDivElement>(spy); | ||
mount(<div ref={ref}>test</div>); | ||
expect(spy).toBeCalledWith(ref.current, null); | ||
expect(ref.current).not.toBe(null); | ||
}); | ||
|
||
it('shall work as useRef', () => { | ||
const spy = jest.fn(); | ||
let counter = 0; | ||
const Test = () => { | ||
const x = counter++; | ||
const ref = useCallbackRef<HTMLDivElement>(null, () => spy(x)); | ||
return <div key={x < 2 ? '1' : '2'} ref={ref}>test</div>; | ||
}; | ||
const wrapper = mount(<Test/>); | ||
expect(spy).toBeCalledWith(0); | ||
|
||
wrapper.setProps({x: 42}); | ||
expect(spy).toBeCalledWith(0); | ||
|
||
it('shall work as createRef', () => { | ||
const spy = jest.fn(); | ||
const ref = createCallbackRef<HTMLDivElement>(spy); | ||
mount(<div ref={ref}>test</div>); | ||
expect(spy).toBeCalledWith(ref.current, null); | ||
expect(ref.current).not.toBe(null); | ||
wrapper.setProps({x: 24}); | ||
expect(spy).toBeCalledWith(2); | ||
}); | ||
}); | ||
|
||
it('shall work as useRef', () => { | ||
const spy = jest.fn(); | ||
let counter = 0; | ||
const Test = () => { | ||
const x = counter++; | ||
const ref = useCallbackRef<HTMLDivElement>(null, () => spy(x)); | ||
return <div key={x<2 ? '1' : '2'} ref={ref}>test</div>; | ||
}; | ||
const wrapper = mount(<Test/>); | ||
expect(spy).toBeCalledWith(0); | ||
|
||
wrapper.setProps({x: 42}); | ||
expect(spy).toBeCalledWith(0); | ||
|
||
wrapper.setProps({x: 24}); | ||
expect(spy).toBeCalledWith(2); | ||
describe('mergeRef', () => { | ||
it("merges two refs", () => { | ||
const spy1 = jest.fn(); | ||
const ref1 = createCallbackRef<HTMLDivElement>(spy1); | ||
const spy2 = jest.fn(); | ||
const ref2 = createCallbackRef<HTMLDivElement>(spy2); | ||
const ref3 = createRef() as React.MutableRefObject<any>; | ||
const ref4 = jest.fn(); | ||
|
||
const TestComponent = () => ( | ||
<div | ||
ref={mergeRefs([ | ||
ref1, | ||
ref2, | ||
ref3, | ||
ref4, | ||
])} | ||
>test</div> | ||
); | ||
|
||
mount(<TestComponent />); | ||
|
||
const ref = ref1.current; | ||
expect(ref).not.toBe(null); | ||
|
||
expect(spy1).toBeCalledWith(ref, null); | ||
expect(spy2).toBeCalledWith(ref, null); | ||
expect(ref3.current).toBe(ref); | ||
expect(ref4).toBeCalledWith(ref); | ||
}); | ||
}); | ||
|
||
}); |
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
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,2 +1,3 @@ | ||
export {useCallbackRef} from './useRef'; | ||
export {createCallbackRef} from './createRef'; | ||
export {createCallbackRef} from './createRef'; | ||
export {mergeRefs} from './mergeRef' |
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,19 @@ | ||
import * as React from 'react'; | ||
import {useCallbackRef} from "./useRef"; | ||
|
||
type ReactRef<T> = ( | ||
((newValue: T | null) => void) | | ||
React.MutableRefObject<T | null> | ||
); | ||
|
||
export function mergeRefs<T>(refs: ReactRef<T>[], defaultValue?: T): React.MutableRefObject<T | null> { | ||
return useCallbackRef<T>(defaultValue, newValue => { | ||
refs.forEach(ref => { | ||
if (typeof ref === 'function') { | ||
ref(newValue) | ||
} else if (ref != null) { | ||
ref.current = newValue | ||
} | ||
}) | ||
}); | ||
} |
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