Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set value on adding setter in createGlobalState #2564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/factory/createGlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createGlobalState<S>(initialState?: S) {
useIsomorphicLayoutEffect(() => {
if (!store.setters.includes(stateSetter)) {
store.setters.push(stateSetter);
storeSetter(store.state);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, renderHook } from '@testing-library/react-hooks';
import createGlobalState from '../src/factory/createGlobalState';
import { act, renderHook, render } from '@testing-library/react';
import React from 'react';
import createGlobalState from 'src/hooks/createGlobalState';

describe('useGlobalState', () => {
it('should be defined', () => {
Expand Down Expand Up @@ -71,6 +72,23 @@ describe('useGlobalState', () => {
});
});

it('should set ref correctly', async () => {
const useGlobalValue = createGlobalState<Element>();
const CheckComponent = ({ stateValue1 }) => {
const [stateValue2] = useGlobalValue();
return <>{String(stateValue2 !== undefined && stateValue2 === stateValue1)}</>;
};

const WrapperComponent = () => {
const [stateValue, setStateValue] = useGlobalValue();
return <div ref={setStateValue}>
<CheckComponent stateValue1={stateValue} />
</div>;
};
const { findByText } = render(<WrapperComponent />);
expect(await findByText('true')).toBeDefined();
});

it('initializes with function', () => {
const useGlobalValue = createGlobalState(() => 0);
const { result: result1 } = renderHook(() => useGlobalValue());
Expand Down