Skip to content

Commit 1220dfd

Browse files
authored
Add test (#81)
1 parent 97d343c commit 1220dfd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/getStore.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {render, screen} from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3+
import {act} from 'react-dom/test-utils';
34

45
import '@babel/polyfill';
56

@@ -109,4 +110,29 @@ describe('getStore', () => {
109110
expect(screen.getByTestId('price').textContent).toContain('0');
110111
expect(screen.getByTestId('other').textContent).toContain('ARAL 10');
111112
});
113+
114+
it('should be possible to create methods to use the setter from getStore', () => {
115+
const {useStore, getStore} = createStore({cart: {items: []}});
116+
117+
function push(proxy, value) {
118+
const [val, set] = proxy(getStore);
119+
set([...val, value]);
120+
}
121+
122+
function Cart() {
123+
const [cart] = useStore.cart();
124+
return <div data-testid="items">{cart.items.join(',')}</div>;
125+
}
126+
127+
const items = (proxy) => proxy.cart.items();
128+
129+
render(<Cart />);
130+
131+
expect(screen.getByTestId('items').textContent).toContain('');
132+
133+
act(() => push(items, 'firstElement'));
134+
expect(screen.getByTestId('items').textContent).toContain('firstElement');
135+
act(() => push(items, 'secondElement'));
136+
expect(screen.getByTestId('items').textContent).toContain('firstElement,secondElement');
137+
})
112138
});

0 commit comments

Comments
 (0)