This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
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
andy
committed
Oct 30, 2019
1 parent
01cde7d
commit 59063d1
Showing
4 changed files
with
123 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ node_modules | |
.rts2_cache_esm | ||
.rts2_cache_umd | ||
dist | ||
todo.txt | ||
todo.txt | ||
coverage |
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,15 +1,124 @@ | ||
import React from 'react'; | ||
import { render } from './test-utils'; | ||
import { Pager } from '../src'; | ||
import { Text } from 'react-native'; | ||
import { render, fireEvent } from './test-utils'; | ||
import { Pager, iPager, PagerProvider, usePager } from '../src'; | ||
import { Text, Button } from 'react-native'; | ||
|
||
test('render()', () => { | ||
const { debug } = render( | ||
<Pager style={{ width: 200, height: 200 }}> | ||
function TestPager(props: iPager) { | ||
// style prop will render children without waiting for layout events | ||
return <Pager {...props} style={{ width: 100, height: 100 }} />; | ||
} | ||
|
||
test('render() works', () => { | ||
render( | ||
<TestPager> | ||
<Text>1</Text> | ||
<Text>2</Text> | ||
</Pager> | ||
</TestPager> | ||
); | ||
}); | ||
|
||
test('activeIndex and onChange props update pager', () => { | ||
function Container({ spy }) { | ||
const [activeIndex, onChange] = React.useState(0); | ||
|
||
function handleChange(nextIndex: number) { | ||
spy(nextIndex); | ||
onChange(nextIndex); | ||
} | ||
|
||
return ( | ||
<TestPager activeIndex={activeIndex} onChange={onChange}> | ||
<Text>Active Index: {activeIndex}</Text> | ||
<Button | ||
title={'change'} | ||
onPress={() => handleChange(activeIndex + 1)} | ||
/> | ||
</TestPager> | ||
); | ||
} | ||
|
||
const spy = jest.fn(); | ||
const { getByText } = render(<Container spy={spy} />); | ||
|
||
debug(); | ||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 1'); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(1); | ||
|
||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 2'); | ||
}); | ||
|
||
test('provider injects props into pager', () => { | ||
function Provider({ spy }) { | ||
const [activeIndex, onChange] = React.useState(0); | ||
|
||
function handleChange(nextIndex: number) { | ||
spy(nextIndex); | ||
onChange(nextIndex); | ||
} | ||
|
||
return ( | ||
<PagerProvider activeIndex={activeIndex} onChange={handleChange}> | ||
{({ onChange }) => ( | ||
<TestPager> | ||
<Text>Active Index: {activeIndex}</Text> | ||
<Button | ||
title={'change'} | ||
onPress={() => onChange(activeIndex + 1)} | ||
/> | ||
</TestPager> | ||
)} | ||
</PagerProvider> | ||
); | ||
} | ||
|
||
const spy = jest.fn(); | ||
const { getByText } = render(<Provider spy={spy} />); | ||
|
||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 1'); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(1); | ||
|
||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 2'); | ||
}); | ||
|
||
test('consumers of provider are able to update pager', () => { | ||
function Consumer() { | ||
const [activeIndex, onChange] = usePager(); | ||
return <Button title="change" onPress={() => onChange(activeIndex + 1)} />; | ||
} | ||
|
||
function Provider({ spy }) { | ||
const [activeIndex, onChange] = React.useState(0); | ||
|
||
function handleChange(nextIndex: number) { | ||
spy(nextIndex); | ||
onChange(nextIndex); | ||
} | ||
|
||
return ( | ||
<PagerProvider activeIndex={activeIndex} onChange={handleChange}> | ||
<TestPager> | ||
<Text>Active Index: {activeIndex}</Text> | ||
<Consumer /> | ||
</TestPager> | ||
</PagerProvider> | ||
); | ||
} | ||
|
||
const spy = jest.fn(); | ||
const { getByText } = render(<Provider spy={spy} />); | ||
|
||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 1'); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(1); | ||
|
||
fireEvent.press(getByText(/change/i)); | ||
getByText('Active Index: 2'); | ||
}); | ||
|
||
test.todo('consumers are able to use animatedIndex'); |