-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Tests] add hooks tests #2008
Merged
Merged
[Tests] add hooks tests #2008
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34a7359
[Tests] add `useState`, more `useEffect`, custom hooks
39676a0
[Tests] `useReducer`
839005c
[Tests] `useContext`
48cc351
[Tests] more `useState`
chenesan 9abded2
[Tests] fix custom form input hook test
chenesan d9fa3f0
[Tests] `useEffect`: Fix set document title test
chenesan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ import { | |
|
||
import { | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useLayoutEffect, | ||
useMemo, | ||
|
150 changes: 150 additions & 0 deletions
150
packages/enzyme-test-suite/test/shared/hooks/custom.jsx
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,150 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon-sandbox'; | ||
|
||
import { | ||
describeIf, | ||
} from '../../_helpers'; | ||
|
||
import { | ||
useEffect, | ||
useState, | ||
} from '../../_helpers/react-compat'; | ||
|
||
export default function describeCustomHooks({ | ||
hasHooks, | ||
Wrap, | ||
isShallow, | ||
}) { | ||
describeIf(hasHooks, 'hooks: custom', () => { | ||
describe('custom hook : useCounter', () => { | ||
function useCounter({ initialCount = 0, step = 1 } = {}) { | ||
const [count, setCount] = useState(initialCount); | ||
const increment = () => setCount(c => c + step); | ||
const decrement = () => setCount(c => c - step); | ||
return { count, increment, decrement }; | ||
} | ||
// testing custom hooks with renderProps | ||
// may be we can think of adding in utils | ||
// will be repeated | ||
const Counter = ({ children, ...rest }) => children(useCounter(rest)); | ||
|
||
function setup(props) { | ||
const returnVal = {}; | ||
Wrap( | ||
<Counter {...props}> | ||
{(val) => { | ||
Object.assign(returnVal, val); | ||
return null; | ||
}} | ||
</Counter>, | ||
); | ||
return returnVal; | ||
} | ||
|
||
it('useCounter', () => { | ||
const counterData = setup(); | ||
counterData.increment(); | ||
expect(counterData).to.have.property('count', 1); | ||
counterData.decrement(); | ||
expect(counterData).to.have.property('count', 0); | ||
}); | ||
|
||
it('useCounter with initialCount', () => { | ||
const counterData = setup({ initialCount: 2 }); | ||
counterData.increment(); | ||
expect(counterData).to.have.property('count', 3); | ||
counterData.decrement(); | ||
expect(counterData).to.have.property('count', 2); | ||
}); | ||
|
||
it('useCounter with step', () => { | ||
const counterData = setup({ step: 2 }); | ||
counterData.increment(); | ||
expect(counterData).to.have.property('count', 2); | ||
counterData.decrement(); | ||
expect(counterData).to.have.property('count', 0); | ||
}); | ||
|
||
it('useCounter with step and initialCount', () => { | ||
const counterData = setup({ step: 2, initialCount: 5 }); | ||
counterData.increment(); | ||
expect(counterData).to.have.property('count', 7); | ||
counterData.decrement(); | ||
expect(counterData).to.have.property('count', 5); | ||
}); | ||
}); | ||
|
||
// todo: enable shallow when useEffect works in the shallow renderer. see https://github.com/facebook/react/issues/15275 | ||
describeIf(!isShallow, 'custom hook: formInput invoke props', () => { | ||
function useFormInput(initialValue = '') { | ||
const [value, setValue] = useState(initialValue); | ||
|
||
return { | ||
value, | ||
onChange(e) { | ||
setValue(e.target.value); | ||
}, | ||
}; | ||
} | ||
|
||
function Input(props) { | ||
return ( | ||
<div> | ||
<input {...props} /> | ||
</div> | ||
); | ||
} | ||
|
||
function ControlledInputWithEnhancedInput({ searchSomething }) { | ||
const search = useFormInput(); | ||
|
||
useEffect( | ||
() => { | ||
searchSomething(search.value); | ||
}, | ||
[search.value], | ||
); | ||
|
||
return <Input {...search} />; | ||
} | ||
|
||
function ControlledInputWithNativeInput({ searchSomething }) { | ||
const search = useFormInput(); | ||
|
||
useEffect( | ||
() => { | ||
searchSomething(search.value); | ||
}, | ||
[search.value], | ||
); | ||
|
||
return <input {...search} />; | ||
} | ||
|
||
it('work with native input', () => { | ||
const spy = sinon.spy(); | ||
const wrapper = Wrap(<ControlledInputWithNativeInput searchSomething={spy} />); | ||
wrapper.find('input').invoke('onChange')({ target: { value: 'foo' } }); | ||
|
||
expect(spy.withArgs('foo')).to.have.property('callCount', 1); | ||
}); | ||
|
||
it('work with custom wrapped Input', () => { | ||
const spy = sinon.spy(); | ||
const wrapper = Wrap(<ControlledInputWithEnhancedInput searchSomething={spy} />); | ||
const input = wrapper.find('Input'); | ||
input.invoke('onChange')({ target: { value: 'foo' } }); | ||
expect(spy.withArgs('foo')).to.have.property('callCount', 1); | ||
}); | ||
|
||
it('work with custom wrapped input', () => { | ||
const spy = sinon.spy(); | ||
const wrapper = Wrap(<ControlledInputWithEnhancedInput searchSomething={spy} />); | ||
const input = wrapper.find('input'); | ||
input.invoke('onChange')({ target: { value: 'foo' } }); | ||
expect(spy.withArgs('foo')).to.have.property('callCount', 1); | ||
}); | ||
}); | ||
}); | ||
} |
122 changes: 122 additions & 0 deletions
122
packages/enzyme-test-suite/test/shared/hooks/useContext.jsx
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,122 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
|
||
import { | ||
describeIf, | ||
itIf, | ||
} from '../../_helpers'; | ||
|
||
import { | ||
useContext, | ||
useState, | ||
createContext, | ||
} from '../../_helpers/react-compat'; | ||
|
||
export default function describeUseContext({ | ||
hasHooks, | ||
Wrap, | ||
isShallow, | ||
}) { | ||
describeIf(hasHooks, 'hooks: useContext', () => { | ||
describe('simple example', () => { | ||
const initialTitle = 'initialTitle'; | ||
const TitleContext = createContext && createContext(initialTitle); | ||
|
||
function UiComponent() { | ||
const title = useContext(TitleContext); | ||
return ( | ||
<div> | ||
{title} | ||
</div> | ||
); | ||
} | ||
|
||
const customTitle = 'CustomTitle'; | ||
|
||
function App() { | ||
return ( | ||
<TitleContext.Provider value={customTitle}> | ||
<UiComponent /> | ||
</TitleContext.Provider> | ||
); | ||
} | ||
|
||
it('render ui component with initial context value', () => { | ||
const wrapper = Wrap(<UiComponent />); | ||
expect(wrapper.text()).to.equal(initialTitle); | ||
}); | ||
|
||
// TODO: useContext: enable when shallow dive supports createContext | ||
itIf(!isShallow, 'render ui component with value from outer provider', () => { | ||
const wrapper = Wrap(<App />); | ||
const subWrapper = isShallow ? wrapper.dive().dive() : wrapper; | ||
expect(subWrapper.text()).to.equal(customTitle); | ||
}); | ||
}); | ||
|
||
// TODO: useContext: enable when shallow dive supports createContext | ||
describeIf(!isShallow, 'useContext: with Setting', () => { | ||
const initialState = 10; | ||
const context = createContext && createContext(null); | ||
|
||
function MyGrandChild() { | ||
const myContextVal = useContext(context); | ||
|
||
const increment = () => { | ||
myContextVal.setState(myContextVal.state + 1); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button type="button" onClick={increment}>increment</button> | ||
<span className="grandChildState"> | ||
{myContextVal.state} | ||
</span> | ||
</div> | ||
); | ||
} | ||
|
||
function MyChild() { | ||
return ( | ||
<div> | ||
<MyGrandChild /> | ||
</div> | ||
); | ||
} | ||
|
||
function App() { | ||
const [state, setState] = useState(initialState); | ||
|
||
return ( | ||
<context.Provider value={{ state, setState }}> | ||
<div> | ||
<MyChild /> | ||
</div> | ||
</context.Provider> | ||
); | ||
} | ||
|
||
it('test render, get and set context value ', () => { | ||
const wrapper = Wrap(<App />); | ||
|
||
function getChild() { | ||
const child = wrapper.find(MyChild); | ||
return isShallow ? child.dive() : child; | ||
} | ||
function getGrandChild() { | ||
const grandchild = getChild().find(MyGrandChild); | ||
return isShallow ? grandchild.dive() : grandchild; | ||
} | ||
expect(getGrandChild().find('.grandChildState').debug()).to.equal(`<span className="grandChildState"> | ||
${String(initialState)} | ||
</span>`); | ||
|
||
getGrandChild().find('button').props().onClick(); | ||
wrapper.update(); | ||
expect(getGrandChild().find('.grandChildState').debug()).to.equal(`<span className="grandChildState"> | ||
${String(initialState + 1)} | ||
</span>`); | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and also add the link here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2008 (comment)