-
Notifications
You must be signed in to change notification settings - Fork 402
fix(checkout): CHECKOUT-10026 Add new Places API support for Google Autocomplete with legacy fallback #3110
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
Closed
Closed
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
588c55f
fix(checkout): CHECKOUT-10026 migrate Google Places script loader to …
bc-maxy bd75dcc
fix(checkout): CHECKOUT-10026 Migrate GoogleAutocompleteService
bc-maxy a9876e5
fix(checkout): CHECKOUT-10026 Update GoogleAutocomplete file
bc-maxy 1322a5a
fix(checkout): CHECKOUT-10026 Various quick fixes
bc-maxy f7af121
fix(checkout): CHECKOUT-10026 Resolve cursor comments
bc-maxy a5d1b4b
fix(checkout): CHECKOUT-10026 Add session token to suggestion calls
bc-maxy e02bfce
fix(checkout): CHECKOUT-10026 Various small cleanup
bc-maxy b93d093
fix(checkout): CHECKOUT-10026 Keep the old service alive
bc-maxy 491717a
chore(checkout): CHECKOUT-10026 Clean up the code
bc-maxy dd051c8
feat(checkout): CHECKOUT-10026 Fall back to new service if old one fails
bc-maxy 0e95de2
fix(checkout): CHECKOUT-10026 Adjust functions to work correctly
bc-maxy 9389b14
fix(checkout): CHECKOUT-10026 Use module scoped variable
bc-maxy 9672f94
fix(checkout): CHECKOUT-10026 Fix review comments
bc-maxy 9796c11
fix(checkout): CHECKOUT-10026 Clean up the implementation
bc-maxy acad608
fix(checkout): CHECKOUT-10026 Update test file path
bc-maxy 164211b
fix(checkout): CHECKOUT-10026 Try new service first, fall back to old
bc-maxy 34537f2
fix(checkout): CHECKOUT-10026 Clean up the implementation
bc-maxy bcc8a31
fix(checkout): CHECKOUT-10026 A small refactor
bc-maxy 974d130
chore(checkout): CHECKOUT-10026 Fix typos and unused exports
bc-maxy 45efb30
fix(checkout): CHECKOUT-10026 Fall back to legacy only for permission…
bc-maxy 9648939
fix(checkout): CHECKOUT-10026 Hide behind feature flag WIP
bc-maxy 08fc93e
Merge branch 'master' into checkout-10026
bc-maxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
packages/core/src/app/address/googleAutocomplete/GoogleAutocomplete.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import userEvent from '@testing-library/user-event'; | ||
| import React from 'react'; | ||
|
|
||
| import { render, screen, waitFor } from '@bigcommerce/checkout/test-utils'; | ||
|
|
||
| import GoogleAutocomplete, { | ||
| type GoogleAutocompleteProps, | ||
| newGooglePlacesApiState, | ||
| } from './GoogleAutocomplete'; | ||
| import LegacyGoogleAutocompleteService from './GoogleAutocompleteService'; | ||
| import { NewGooglePlacesApiService } from './newGooglePlacesApi/NewGooglePlacesApiService'; | ||
|
|
||
| jest.mock('./GoogleAutocompleteService'); | ||
| jest.mock('./newGooglePlacesApi/NewGooglePlacesApiService'); | ||
|
|
||
| const MockLegacyService = LegacyGoogleAutocompleteService as jest.MockedClass< | ||
| typeof LegacyGoogleAutocompleteService | ||
| >; | ||
| const MockPlacesApiService = NewGooglePlacesApiService as jest.MockedClass< | ||
| typeof NewGooglePlacesApiService | ||
| >; | ||
|
|
||
| const legacySuggestions = [ | ||
| { | ||
| description: '123 Legacy St, New York', | ||
| structured_formatting: { main_text: '123 Legacy St' }, | ||
| matched_substrings: [], | ||
| place_id: 'legacy-place-1', | ||
| }, | ||
| ]; | ||
|
|
||
| const legacyPlaceResult = { | ||
| name: '123 Legacy St', | ||
| address_components: [], | ||
| } as google.maps.places.PlaceResult; | ||
|
|
||
| const newApiSuggestions = [ | ||
| { id: 'new-place-1', label: '123 New API Ave', value: '123 New API Ave' }, | ||
| ]; | ||
|
|
||
| const newApiPlaceResult = { | ||
| name: '123 New API Ave', | ||
| address_components: [], | ||
| } as google.maps.places.PlaceResult; | ||
|
|
||
| describe('GoogleAutocomplete', () => { | ||
| let mockGetPlacePredictions: jest.Mock; | ||
| let mockGetDetails: jest.Mock; | ||
| let mockGetSuggestions: jest.Mock; | ||
| let mockGetPlaceDetails: jest.Mock; | ||
| let defaultProps: GoogleAutocompleteProps; | ||
|
|
||
| beforeEach(() => { | ||
| newGooglePlacesApiState.isUnavailable = false; | ||
|
|
||
| mockGetPlacePredictions = jest.fn(); | ||
| mockGetDetails = jest.fn(); | ||
| mockGetSuggestions = jest.fn().mockResolvedValue(newApiSuggestions); | ||
| mockGetPlaceDetails = jest.fn().mockResolvedValue(newApiPlaceResult); | ||
|
|
||
| MockLegacyService.mockImplementation( | ||
| () => | ||
| ({ | ||
| getAutocompleteService: jest | ||
| .fn() | ||
| .mockResolvedValue({ getPlacePredictions: mockGetPlacePredictions }), | ||
| getPlacesServices: jest.fn().mockResolvedValue({ getDetails: mockGetDetails }), | ||
| }) as unknown as LegacyGoogleAutocompleteService, | ||
| ); | ||
|
|
||
| MockPlacesApiService.mockImplementation( | ||
| () => | ||
| ({ | ||
| getSuggestions: mockGetSuggestions, | ||
| getPlaceDetails: mockGetPlaceDetails, | ||
| }) as unknown as NewGooglePlacesApiService, | ||
| ); | ||
|
|
||
| defaultProps = { | ||
| apiKey: 'test-api-key', | ||
| isAutocompleteEnabled: true, | ||
| onSelect: jest.fn(), | ||
| onChange: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('new API available (preferred path)', () => { | ||
| it('shows suggestions from the new API', async () => { | ||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| await userEvent.type(screen.getByRole('textbox'), '123'); | ||
|
|
||
| await screen.findByText('123 New API Ave'); | ||
| expect(mockGetPlacePredictions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('debounces rapid keystrokes into a single new API request', async () => { | ||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| await userEvent.type(screen.getByRole('textbox'), '123'); | ||
|
|
||
| await screen.findByText('123 New API Ave'); | ||
|
|
||
| // Three characters, one request — for the full final value. | ||
| expect(mockGetSuggestions).toHaveBeenCalledTimes(1); | ||
| expect(mockGetSuggestions).toHaveBeenCalledWith('123', undefined, undefined); | ||
| }); | ||
|
|
||
| it('calls onSelect with the new API place result when a suggestion is picked', async () => { | ||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| await userEvent.type(screen.getByRole('textbox'), '123'); | ||
| await screen.findByText('123 New API Ave'); | ||
| await userEvent.click(screen.getByText('123 New API Ave')); | ||
|
|
||
| await waitFor(() => | ||
| expect(defaultProps.onSelect).toHaveBeenCalledWith( | ||
| newApiPlaceResult, | ||
| expect.objectContaining({ id: 'new-place-1' }), | ||
| ), | ||
| ); | ||
| expect(mockGetDetails).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('new API unavailable (fall back to legacy)', () => { | ||
| beforeEach(() => { | ||
| mockGetPlacePredictions.mockImplementation((_req, cb) => cb(legacySuggestions, 'OK')); | ||
| mockGetDetails.mockImplementation((_req, cb) => cb(legacyPlaceResult, 'OK')); | ||
| }); | ||
|
|
||
| it('falls back to the legacy service for suggestions when the new API rejects', async () => { | ||
| mockGetSuggestions.mockRejectedValue(new Error('new API down')); | ||
|
|
||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| await userEvent.type(screen.getByRole('textbox'), '123'); | ||
|
|
||
| await screen.findByText('123 Legacy St, New York'); | ||
| expect(mockGetPlacePredictions).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('skips the new API on later input once it has failed', async () => { | ||
| mockGetSuggestions.mockRejectedValue(new Error('new API down')); | ||
|
|
||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| const input = screen.getByRole('textbox'); | ||
|
|
||
| // First debounced request hits the new API, fails, and flips the fallback flag. | ||
| await userEvent.type(input, '1'); | ||
| await screen.findByText('123 Legacy St, New York'); | ||
| expect(mockGetSuggestions).toHaveBeenCalledTimes(1); | ||
|
|
||
| // A later request goes straight to legacy without retrying the new API. | ||
| await userEvent.type(input, '2'); | ||
| await waitFor(() => expect(mockGetPlacePredictions).toHaveBeenCalledTimes(2)); | ||
| expect(mockGetSuggestions).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('falls back to the legacy service for place details when the new API rejects', async () => { | ||
| // Suggestions still come from the new API so the dropdown has an item to click, | ||
| // but getPlaceDetails rejects, forcing the legacy getDetails fallback. | ||
| mockGetPlaceDetails.mockRejectedValue(new Error('new API down')); | ||
|
|
||
| render(<GoogleAutocomplete {...defaultProps} />); | ||
|
|
||
| await userEvent.type(screen.getByRole('textbox'), '1'); | ||
| await screen.findByText('123 New API Ave'); | ||
| await userEvent.click(screen.getByText('123 New API Ave')); | ||
|
|
||
| await waitFor(() => | ||
| expect(defaultProps.onSelect).toHaveBeenCalledWith( | ||
| legacyPlaceResult, | ||
| expect.objectContaining({ id: 'new-place-1' }), | ||
| ), | ||
| ); | ||
| expect(mockGetDetails).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.