Skip to content

Commit f5005b8

Browse files
committed
feat(examples): Add context examples
1 parent e8fbef5 commit f5005b8

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

examples/__tests__/react-context.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react'
2+
import {render} from '../../src'
3+
import {NameContext, NameProvider, NameConsumer} from '../react-context'
4+
5+
/**
6+
* Test default values by rendering a context consumer without a
7+
* matching provider
8+
*/
9+
test('NameConsumer shows default value', () => {
10+
const {getByText} = render(<NameConsumer />)
11+
expect(getByText('My Name Is:').textContent).toBe('My Name Is: Unknown')
12+
})
13+
14+
/**
15+
* To test a component tree that uses a context consumer but not the provider,
16+
* wrap the tree with a matching provider
17+
*/
18+
test('NameConsumer shows value from provider', () => {
19+
const tree = (
20+
<NameContext.Provider value={'C3P0'}>
21+
<NameConsumer />
22+
</NameContext.Provider>
23+
)
24+
const {getByText} = render(tree)
25+
expect(getByText('My Name Is:').textContent).toBe('My Name Is: C3P0')
26+
})
27+
28+
/**
29+
* To test a component that provides a context value, render a matching
30+
* consumer as the child
31+
*/
32+
test('NameProvider composes full name from first, last', () => {
33+
const tree = (
34+
<NameProvider first="Boba" last="Fett">
35+
<NameContext.Consumer>
36+
{value => <span>Received: {value}</span>}
37+
</NameContext.Consumer>
38+
</NameProvider>
39+
)
40+
const {getByText} = render(tree)
41+
expect(getByText('Received: ').textContent).toBe('Received: Boba Fett')
42+
})
43+
44+
/**
45+
* A tree containing both a providers and consumer can be rendered normally
46+
*/
47+
test('NameProvider/Consumer shows name of character', () => {
48+
const tree = (
49+
<NameProvider first="Leia" last="Organa">
50+
<NameConsumer />
51+
</NameProvider>
52+
)
53+
const {getByText} = render(tree)
54+
expect(getByText('My Name Is: ').textContent).toBe('My Name Is: Leia Organa')
55+
})

examples/jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const jestConfig = require('kcd-scripts/jest')
2+
3+
module.exports = Object.assign(jestConfig, {
4+
testEnvironment: 'jest-environment-jsdom',
5+
roots: ['./'],
6+
})

examples/react-context.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
3+
const NameContext = React.createContext('Unknown')
4+
5+
const NameProvider = ({children, first, last}) => {
6+
const fullName = `${first} ${last}`
7+
return (
8+
<NameContext.Provider value={fullName}>{children}</NameContext.Provider>
9+
)
10+
}
11+
12+
const NameConsumer = () => (
13+
<NameContext.Consumer>
14+
{value => <div>My Name Is: {value}</div>}
15+
</NameContext.Consumer>
16+
)
17+
18+
export {NameContext, NameConsumer, NameProvider}

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const jestConfig = require('kcd-scripts/jest')
2+
3+
module.exports = Object.assign(jestConfig, {
4+
testEnvironment: 'jest-environment-jsdom',
5+
projects: ['./examples', './src'],
6+
})

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
"add-contributor": "kcd-scripts contributors add",
1212
"build": "kcd-scripts build",
1313
"lint": "kcd-scripts lint",
14-
"test": "kcd-scripts test",
15-
"test:update": "npm test -- --updateSnapshot --coverage",
14+
"test": "kcd-scripts test --projects=src",
15+
"test:update": "npm test -- --updateSnapshot --coverage --projects=src",
16+
"test:examples": "kcd-scripts test --projects=examples",
1617
"validate": "kcd-scripts validate",
1718
"setup": "npm install && npm run validate -s",
1819
"precommit": "kcd-scripts precommit"

0 commit comments

Comments
 (0)