-
Notifications
You must be signed in to change notification settings - Fork 95
Add waitForCollectionCallback to keysChanged #167
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ const ONYX_KEYS = { | |
| COLLECTION: { | ||
| TEST_KEY: 'test_', | ||
| TEST_CONNECT_COLLECTION: 'test_connect_collection_', | ||
| TEST_POLICY: 'test_policy_', | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -470,11 +471,10 @@ describe('Onyx', () => { | |
| }); | ||
|
|
||
| it('should return all collection keys as a single object when waitForCollectionCallback = true', () => { | ||
| const valuesReceived = {}; | ||
| const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value); | ||
| const mockCallback = jest.fn(); | ||
|
|
||
| // GIVEN some collection data | ||
| const collectionData = { | ||
| // GIVEN some initial collection data | ||
| const initialCollectionData = { | ||
| test_connect_collection_1: { | ||
| ID: 123, | ||
| value: 'one', | ||
|
|
@@ -488,10 +488,11 @@ describe('Onyx', () => { | |
| value: 'three', | ||
| }, | ||
| }; | ||
| Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionData); | ||
|
|
||
| Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData); | ||
| return waitForPromisesToResolve() | ||
| .then(() => { | ||
| // WHEN we call Onyx.connect with the waitForCollectionCallback = true param | ||
| // WHEN we connect to that collection with waitForCollectionCallback = true | ||
| connectionID = Onyx.connect({ | ||
| key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, | ||
| waitForCollectionCallback: true, | ||
|
|
@@ -500,11 +501,40 @@ describe('Onyx', () => { | |
| return waitForPromisesToResolve(); | ||
| }) | ||
| .then(() => { | ||
| // THEN the callback should be triggered only once | ||
| // THEN we expect the callback to be called only once and the initial stored value to be initialCollectionData | ||
| expect(mockCallback.mock.calls.length).toBe(1); | ||
| expect(mockCallback.mock.calls[0][0]).toEqual(initialCollectionData); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true', () => { | ||
| const mockCallback = jest.fn(); | ||
| const collectionUpdate = { | ||
| test_policy_1: {ID: 234, value: 'one'}, | ||
| test_policy_2: {ID: 123, value: 'two'}, | ||
| }; | ||
|
|
||
| // GIVEN an Onyx.connect call with waitForCollectionCallback=true | ||
| connectionID = Onyx.connect({ | ||
| key: ONYX_KEYS.COLLECTION.TEST_POLICY, | ||
| waitForCollectionCallback: true, | ||
| callback: mockCallback, | ||
| }); | ||
| return waitForPromisesToResolve() | ||
| .then(() => { | ||
| // WHEN mergeCollection is called with an updated collection | ||
| Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate); | ||
| return waitForPromisesToResolve(); | ||
| }) | ||
| .then(() => { | ||
| // THEN we expect the callback to have called twice, once for the initial connect call + once for the collection update | ||
| expect(mockCallback.mock.calls.length).toBe(2); | ||
|
|
||
| // AND the value for the first call should be null since the collection was not initialized at that point | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it seem weird to anyone else that:
? Is it possible for us to remove that first call? What is the value in having the call triggered with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is expected since we want to initialize that key's value to This happens here before we run any callback subscriber logic. I think it would make sense to keep the behavior consistent.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to what @luacmartins said :), I would also prefer to avoid exceptions and let the subscriber handle that. What if
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, those are good points. I think it would be a change in behavior that could make other bugs in the process, so this was more of an exploratory question. Let's leave it as-is! |
||
| expect(mockCallback.mock.calls[0][0]).toBe(null); | ||
|
|
||
| // AND all the collection data should be returned as a single object | ||
| expect(mockCallback.mock.calls[0][0]).toEqual(collectionData); | ||
| // AND the value for the second call should be collectionUpdate since the collection was updated | ||
| expect(mockCallback.mock.calls[1][0]).toEqual(collectionUpdate); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.