Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andy committed Oct 30, 2019
1 parent 01cde7d commit 59063d1
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
.rts2_cache_esm
.rts2_cache_umd
dist
todo.txt
todo.txt
coverage
2 changes: 1 addition & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const App = () => {
<View
style={{flex: 1, backgroundColor: 'white', justifyContent: 'center'}}>
<PagerProvider activeIndex={activeIndex} onChange={onChange}>
<MyPager />
<KilterCards />
</PagerProvider>
</View>
</SafeAreaView>
Expand Down
7 changes: 3 additions & 4 deletions src/pager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ function Pager({

// set the initial position - priority to direct prop over context, and context over uncontrolled
const _position = memoize(new Value(activeIndex));
const position = isControlled
? animatedValue || _position
const position = animatedValue
? animatedValue
: context
? context[2]
: _position;
Expand Down Expand Up @@ -456,8 +456,7 @@ function Pager({
</FocusProvider>
</IndexProvider>
);
})}{' '}
}
})}
</Animated.View>
</Animated.View>
</Animated.View>
Expand Down
125 changes: 117 additions & 8 deletions test/pager.test.tsx
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');

0 comments on commit 59063d1

Please sign in to comment.