-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add test example for context. (#60)
* Add Test and add myself to list of contributors * Change getByTestId to getByText * Chage how value is set on Provider because it is better on performance * Add test to list of examples in README
- Loading branch information
Showing
3 changed files
with
59 additions
and
4 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
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,47 @@ | ||
import React from 'react' | ||
import {render, Simulate} from '../' | ||
|
||
const StateContext = React.createContext() | ||
|
||
const MyButton = () => ( | ||
<StateContext.Consumer> | ||
{({handleToggle}) => <button onClick={handleToggle}>Push Me</button>} | ||
</StateContext.Consumer> | ||
) | ||
|
||
const SecretMessage = () => ( | ||
<StateContext.Consumer> | ||
{({toggleStatus}) => <h1>{toggleStatus ? 'Secret Message' : 'Hidden'}</h1>} | ||
</StateContext.Consumer> | ||
) | ||
|
||
class Container extends React.Component { | ||
handleToggle = () => { | ||
this.setState(prevState => ({ | ||
toggleStatus: !prevState.toggleStatus, | ||
})) | ||
} | ||
|
||
state = { | ||
toggleStatus: false, | ||
handleToggle: this.handleToggle, | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<StateContext.Provider value={this.state}> | ||
<MyButton /> | ||
<SecretMessage /> | ||
</StateContext.Provider> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
test('Component renders with the correct message then correctly changes message after clicking button', () => { | ||
const {getByText} = render(<Container />) | ||
expect(getByText('Hidden').textContent).toBe('Hidden') | ||
Simulate.click(getByText('Push Me')) | ||
expect(getByText('Secret Message').textContent).toBe('Secret Message') | ||
}) |