Skip to content

Commit

Permalink
Add failing test (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclay committed Jul 21, 2022
1 parent b27abf2 commit 75dd135
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/getStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ describe('getStore', () => {
expect(screen.getByTestId('items').textContent).toContain('firstElement');
act(() => push(items, 'secondElement'));
expect(screen.getByTestId('items').textContent).toContain('firstElement,secondElement');
})
});
});
65 changes: 65 additions & 0 deletions tests/issue88.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {render} from '@testing-library/react';
import {useEffect, useState} from 'react';

import '@babel/polyfill';

import createStore from '../package/index';

describe('Issue 88', () => {
it('should subscribe even if initially not rendered', () => {
const track = jest.fn();

const initialStore = {
number: 0,
};
const {useStore} = createStore(initialStore);

const AA = () => {
const [number] = useStore.number();
track(`AA number: ${number}`);

return number;
};

const CC = () => {
const [number, setNumber] = useStore.number();
track(`CC number: ${number}`);
if (!number) {
setNumber((prev) => prev + 1);
}

return 'c';
};

function App() {
const [show, setShow] = useState(false);
track(`App show: ${show}`);
useEffect(() => {
setShow(true);
}, []);

return (
<div>
{show && (
<>
<AA />
<CC />
</>
)}
</div>
);
}

render(<App />);

console.log(track.mock.calls);
expect(track.mock.calls).toContain([
[ 'App show: false' ],
[ 'App show: true' ],
[ 'AA number: 0' ],
[ 'CC number: 0' ],
[ 'AA number: 1' ],
[ 'CC number: 1' ],
]);
});
});

0 comments on commit 75dd135

Please sign in to comment.