Skip to content

Commit

Permalink
test: ensured use-state and use-wire-value is in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi committed Jan 29, 2022
1 parent b520957 commit c0261ae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/selector/use-selector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { renderHook } from '@testing-library/react-hooks';
import { act, renderHook } from '@testing-library/react-hooks';
import { useEffect, useState } from 'react';
import { useWireValue } from '../state-wire/use-wire-value';
import { useWire } from '../wire/use-wire';
import { useSelector } from './use-selector';

Expand All @@ -12,4 +14,20 @@ describe('use-selector', () => {

expect(result.current.selector.getValue()).toBe(6);
});
it('should updated in sync with react useState', function () {
const { result } = renderHook(() => {
const wire = useWire(null, 0);
const selector = useSelector({ get: ({ get }) => get(wire) * 2 });
const selectorValue = useWireValue(selector);
const [value, setValue] = useState(0);
useEffect(() => {
expect(selectorValue).toBe(value * 2);
});
return { wire, setValue };
});
act(() => {
result.current.setValue(1);
result.current.wire.setValue(1);
});
});
});
17 changes: 17 additions & 0 deletions src/state-wire/use-wire-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { useEffect, useState } from 'react';
import { useWire } from '../wire/use-wire';
import { useStateWire } from './use-state-wire';
import { useWireValue } from './use-wire-value';

Expand Down Expand Up @@ -148,4 +150,19 @@ describe('useWireValue', () => {
expect(result.current.wire2.getValue()).toBe(6);
});
});
it('should updated in sync with react useState', function () {
const { result } = renderHook(() => {
const wire = useWire(null, 0);
const wireValue = useWireValue(wire);
const [value, setValue] = useState(0);
useEffect(() => {
expect(wireValue).toBe(value);
});
return { wire, setValue };
});
act(() => {
result.current.setValue(1);
result.current.wire.setValue(1);
});
});
});

0 comments on commit c0261ae

Please sign in to comment.