Skip to content

Commit

Permalink
Fix table regression
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed Nov 30, 2023
1 parent 94502a9 commit 45535fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
15 changes: 13 additions & 2 deletions packages/core/src/jotai-factory/createAtomProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AtomRecord, JotaiStore } from './createAtomStore';
import { useHydrateStore, useSyncStore } from './useHydrateStore';

// Global store contexts
const storeContexts = new Map<string, Context<JotaiStore | undefined>>();
const storeContexts = new Map<string, Context<JotaiStore>>();

const GLOBAL_STORE_SCOPE = 'global';
const GLOBAL_SCOPE = 'global';
Expand All @@ -29,7 +29,18 @@ export const getContext = (
) => {
const fullyQualifiedScope = getFullyQualifiedScope(storeScope, scope);
if (createIfNotExists && !storeContexts.has(fullyQualifiedScope)) {
storeContexts.set(fullyQualifiedScope, createContext(undefined as any));
/**
* In some circumstances, when used without a store, jotai presents
* multiple different versions of the same atom depending on where the atom
* is accessed from. (See https://github.com/pmndrs/jotai/discussions/2044)
*
* To avoid this case, we return a default store for use in the absence of
* a provider.
*
* This is not covered by any test; when editing this code, please manually
* verify that table cell selection and column resizing are not broken.
*/
storeContexts.set(fullyQualifiedScope, createContext(createStore()));
}
return storeContexts.get(fullyQualifiedScope);
};
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/jotai-factory/createAtomStore.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { ReactNode, useState } from 'react';
import { act, render, renderHook } from '@testing-library/react';
import { useSetAtom } from 'jotai';

import { createAtomStore } from './createAtomStore';

Expand All @@ -19,8 +18,10 @@ describe('createAtomStore', () => {
age: INITIAL_AGE,
};

const { useMyTestStoreStore, MyTestStoreProvider, myTestStoreStore } =
createAtomStore(initialTestStoreValue, { name: 'myTestStore' as const });
const { useMyTestStoreStore, MyTestStoreProvider } = createAtomStore(
initialTestStoreValue,
{ name: 'myTestStore' as const }
);

const ReadOnlyConsumer = () => {
const [name] = useMyTestStoreStore().use.name();
Expand Down Expand Up @@ -67,8 +68,8 @@ describe('createAtomStore', () => {
};

beforeEach(() => {
renderHook(() => useSetAtom(myTestStoreStore.atom.name)(INITIAL_NAME));
renderHook(() => useSetAtom(myTestStoreStore.atom.age)(INITIAL_AGE));
renderHook(() => useMyTestStoreStore().set.name()(INITIAL_NAME));
renderHook(() => useMyTestStoreStore().set.age()(INITIAL_AGE));
});

it('passes default values from provider to consumer', () => {
Expand Down Expand Up @@ -340,5 +341,15 @@ describe('createAtomStore', () => {
expect(getByText('Jane')).toBeInTheDocument();
expect(getByText('98')).toBeInTheDocument();
});

it('works without provider', () => {
const { getByText } = render(
<MyFirstTestStoreProvider name="Jane" scope="firstScope">
<SecondReadOnlyConsumer />
</MyFirstTestStoreProvider>
);

expect(getByText('72')).toBeInTheDocument();
});
});
});

0 comments on commit 45535fb

Please sign in to comment.