diff --git a/.gitignore b/.gitignore
index e505689..442e7a9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@ node_modules
.rts2_cache_esm
.rts2_cache_umd
dist
-todo.txt
\ No newline at end of file
+todo.txt
+coverage
\ No newline at end of file
diff --git a/example/App.tsx b/example/App.tsx
index 58f937d..db427c3 100644
--- a/example/App.tsx
+++ b/example/App.tsx
@@ -35,7 +35,7 @@ const App = () => {
-
+
diff --git a/src/pager.tsx b/src/pager.tsx
index 32dabd1..758d745 100644
--- a/src/pager.tsx
+++ b/src/pager.tsx
@@ -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;
@@ -456,8 +456,7 @@ function Pager({
);
- })}{' '}
- }
+ })}
diff --git a/test/pager.test.tsx b/test/pager.test.tsx
index b1f4b9a..1a81983 100644
--- a/test/pager.test.tsx
+++ b/test/pager.test.tsx
@@ -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(
-
+function TestPager(props: iPager) {
+ // style prop will render children without waiting for layout events
+ return ;
+}
+
+test('render() works', () => {
+ render(
+
1
2
-
+
);
+});
+
+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 (
+
+ Active Index: {activeIndex}
+
+ );
+ }
+
+ const spy = jest.fn();
+ const { getByText } = render();
- 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 (
+
+ {({ onChange }) => (
+
+ Active Index: {activeIndex}
+
+ )}
+
+ );
+ }
+
+ const spy = jest.fn();
+ const { getByText } = render();
+
+ 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