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

Add tests showing collection callback is called with undefined when setting null on a collection key #597

Closed
wants to merge 3 commits into from
Closed
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
31 changes: 31 additions & 0 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,37 @@ describe('Onyx', () => {
);
});

it('should not call the callback with undefined to a collection subscriber using waitForCollectionCallback when deleting one of the collection keys', () => {
const mockCallback = jest.fn();

// Given an Onyx.connect call with waitForCollectionCallback=true
connection = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
waitForCollectionCallback: true,
callback: mockCallback,
});
Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, {someData: 1});
return (
waitForPromisesToResolve()
// When mergeCollection is called with an updated collection
.then(() => Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, null))
.then(() => {
// TODO: not sure why 3 times, I see it gets called with:
// - { testPolicy_1: { someData: 1 } } testPolicy_ (expected)
// - { testPolicy_1: { someData: 1 } } undefined (not expected)
// - { testPolicy_1: undefined } testPolicy_ (this is showing what I think is a problem)
expect(mockCallback).toHaveBeenCalledTimes(3);

// We should not call the callback with {testPolicy_1: undefined}
expect(mockCallback).toHaveBeenLastCalledWith({}, ONYX_KEYS.COLLECTION.TEST_POLICY);

// The call above should fail, but for some reason it is not, seems like jest is treating
// {testPolicy_1: undefined} the same as {} so adding this check that shows the failure
expect(Object.keys(mockCallback.mock.calls[mockCallback.mock.calls.length - 1][0])).toBe([]);
})
);
});

it('should return a promise when set() called with the same value and there is no change', () => {
const promiseOne = Onyx.set('test', 'pizza');
expect(promiseOne).toBeInstanceOf(Promise);
Expand Down
Loading