-
Notifications
You must be signed in to change notification settings - Fork 337
fix(umd): batch requests when using plugins via UMD #902
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 10 commits
7795aa2
d2ef158
dd8c522
e0251b2
1af5ce1
23d92ba
fd94c0c
9c00a16
20940e8
470aac7
2c08991
bbaf5f8
57ef125
aee71d0
e011c0f
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 |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| import { | ||
| createRequester, | ||
| fetchAlgoliaResults, | ||
| } from '@algolia/autocomplete-preset-algolia'; | ||
| import { fireEvent, waitFor, within } from '@testing-library/dom'; | ||
|
|
||
| import { | ||
|
|
@@ -346,6 +350,93 @@ describe('requester', () => { | |
| }); | ||
| }); | ||
|
|
||
| test('batches calls when possible and re-dispatches results to the right sources across requester instances', async () => { | ||
|
Haroenv marked this conversation as resolved.
Outdated
|
||
| const container = document.createElement('div'); | ||
| const panelContainer = document.createElement('div'); | ||
|
|
||
| document.body.appendChild(panelContainer); | ||
|
|
||
| const searchClient = createSearchClient({ | ||
| search: jest.fn(() => | ||
| Promise.resolve( | ||
| createMultiSearchResponse<{ label: string }>( | ||
| { hits: [{ objectID: '1', label: 'Hit 1' }] }, | ||
| { hits: [{ objectID: '2', label: 'Hit 2' }] } | ||
| ) | ||
| ) | ||
| ), | ||
| }); | ||
|
|
||
| const getResults1 = (params) => | ||
| createRequester(fetchAlgoliaResults)({ | ||
| transformResponse: (response) => response.hits, | ||
| })(params); | ||
|
|
||
| const getResults2 = (params) => | ||
| createRequester(fetchAlgoliaResults)({ | ||
| transformResponse: (response) => response.hits, | ||
| })(params); | ||
|
Haroenv marked this conversation as resolved.
|
||
|
|
||
| autocomplete({ | ||
| container, | ||
| panelContainer, | ||
| getSources({ query }) { | ||
| return [ | ||
| { | ||
| sourceId: 'hits', | ||
| getItems() { | ||
| return getResults1({ | ||
| searchClient, | ||
| queries: [ | ||
| { | ||
| indexName: 'indexName', | ||
| query, | ||
| }, | ||
| ], | ||
| }); | ||
| }, | ||
| templates: { | ||
| item({ item }) { | ||
| return JSON.stringify(item); | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| sourceId: 'other-hits', | ||
| getItems() { | ||
| return getResults2({ | ||
| searchClient, | ||
| queries: [ | ||
| { | ||
| indexName: 'indexName', | ||
| query, | ||
| }, | ||
| ], | ||
| }); | ||
| }, | ||
| templates: { | ||
| item({ item }) { | ||
| return JSON.stringify(item); | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
| }, | ||
| }); | ||
|
|
||
| const input = container.querySelector<HTMLInputElement>('.aa-Input'); | ||
|
|
||
| fireEvent.input(input, { target: { value: 'a' } }); | ||
|
|
||
| await waitFor(() => { | ||
| expect( | ||
| panelContainer.querySelector<HTMLElement>('.aa-Panel') | ||
| ).toBeInTheDocument(); | ||
|
|
||
| expect(searchClient.search).toHaveBeenCalledTimes(1); | ||
|
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. nice test! Not necessary now but I'd love to see one more test which I think would have prevented this issue: An E2E test using our UMD builds and counting requests. Overall, having a test suite counting queries as we make changes would make a lot a sense, given the immediate impact on pricing.
Member
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. Agree on the end-to-end test. I believe this was initially in the PR then removed? I recall our end-to-end setup not to be optimal, but this is a good opportunity to fix it given that one underlying issue is how we expect all our builds to work similarly (and in this case, they don't).
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. there was an UMD E2E test initially indeed, but it wasn't actually easily testing for the case we were fixing. I'll recreate it in a separate PR
Member
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. For the record, FX-1228 in the backlog is about this. |
||
| }); | ||
| }); | ||
|
|
||
| test('transforms the response before forwarding it to the state', async () => { | ||
| const container = document.createElement('div'); | ||
| const panelContainer = document.createElement('div'); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.