Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
// since there are none matched. In withOnyx() we wait for all connected keys to return a value before rendering the child
// component. This null value will be filtered out so that the connected component can utilize defaultProps.
if (matchingKeys.length === 0) {
if (mapping.key && !isCollectionKey(mapping.key)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kacper-mikolajczak I think we should still keep the mapping.key check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've traced down what addNullishStorageKey method does and it only action it does is adding a key to nullishStorageKeys which is a Set primitive.

Do you think we should not be able to add undefined or null into nullishStorageKeys?

If yes, what do you think of moving this check into addNullishStorageKey, as it can decide on its own what to accept?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should not be able to add undefined or null into nullishStorageKeys?

nullishStorageKeys is to save the key that its value is nullish. In case the key is nullish and I think we should not add it to nullishStorageKeys

If yes, what do you think of moving this check into addNullishStorageKey, as it can decide on its own what to accept?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep it as before (keeping mapping.key check). It will be safer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅

if (mapping.key) {
cache.addNullishStorageKey(mapping.key);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/useOnyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ describe('useOnyx', () => {
});

describe('misc', () => {
it('should initially return loading state while loading non-existent key, and then return `undefined` and loaded state', async () => {
const {result} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY));

expect(result.current[0]).toBeUndefined();
expect(result.current[1].status).toEqual('loading');

await act(async () => waitForPromisesToResolve());

expect(result.current[0]).toBeUndefined();
expect(result.current[1].status).toEqual('loaded');
});

it('should initially return loading state while loading non-existent collection key, and then return `undefined` and loaded state', async () => {
const {result} = renderHook(() => useOnyx(ONYXKEYS.COLLECTION.TEST_KEY));

expect(result.current[0]).toBeUndefined();
expect(result.current[1].status).toEqual('loading');

await act(async () => waitForPromisesToResolve());

expect(result.current[0]).toBeUndefined();
expect(result.current[1].status).toEqual('loaded');
});

it('should return value and loaded state when loading cached key', async () => {
Onyx.set(ONYXKEYS.TEST_KEY, 'test');

Expand Down
Loading