Repository navigation
(fix) Load every page of locations in the location data source #866
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
Open
mherman22
wants to merge
6
commits into
openmrs:main
Choose a base branch
from
mherman22:fix/location-data-source-paging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
123480e
(fix) Load every page of locations in the location data source
mherman22 f4e4dc5
Pin the protocol swap in the location paging test
mherman22 63c2189
Send every next page to the page's own origin, port included
mherman22 60b9d70
Shorten the next-link comment
mherman22 4eb5177
Merge branch 'main' into fix/location-data-source-paging
mherman22 07720b3
(refactor) Use openmrsFetchAll for location lists
mherman22 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
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,50 @@ | ||
| import { vi, it, describe, beforeEach, expect } from 'vitest'; | ||
| import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; | ||
| import { LocationDataSource } from './location-data-source'; | ||
|
|
||
| const mockOpenmrsFetch = vi.mocked(openmrsFetch); | ||
|
|
||
| const locations = (from: number, count: number) => | ||
| Array.from({ length: count }, (_, i) => ({ uuid: `uuid-${from + i}`, display: `Clinic ${from + i}` })); | ||
|
|
||
| const backendNextUri = 'https://backend:8443/openmrs/ws/rest/v1/location?tag=Visit+Location&startIndex=50'; | ||
| const proxiedNextUri = `${window.location.protocol}//${window.location.host}/openmrs/ws/rest/v1/location?tag=Visit+Location&startIndex=50`; | ||
|
|
||
| describe('LocationDataSource', () => { | ||
| let ds: LocationDataSource; | ||
|
|
||
| beforeEach(() => { | ||
| mockOpenmrsFetch.mockReset(); | ||
| ds = new LocationDataSource(); | ||
| }); | ||
|
|
||
| describe('fetchData', () => { | ||
| it('returns every tagged location when the server pages the results', async () => { | ||
| mockOpenmrsFetch | ||
| .mockResolvedValueOnce({ | ||
| data: { results: locations(0, 50), links: [{ rel: 'next', uri: backendNextUri }] }, | ||
| } as any) | ||
| .mockResolvedValueOnce({ data: { results: locations(50, 7), links: [{ rel: 'prev', uri: '' }] } } as any); | ||
|
|
||
| const result = await ds.fetchData(null, { tag: 'Visit Location' }); | ||
|
|
||
| expect(result).toEqual(locations(0, 57)); | ||
| expect(mockOpenmrsFetch).toHaveBeenCalledTimes(2); | ||
| expect(mockOpenmrsFetch).toHaveBeenNthCalledWith( | ||
| 1, | ||
| `${restBaseUrl}/location?tag=Visit Location&v=custom:(uuid,display)`, | ||
| ); | ||
| expect(mockOpenmrsFetch).toHaveBeenNthCalledWith(2, proxiedNextUri); | ||
| }); | ||
|
|
||
| it('returns a single page as it is when there is no next link', async () => { | ||
| mockOpenmrsFetch.mockResolvedValueOnce({ data: { results: locations(0, 3) } } as any); | ||
|
|
||
| const result = await ds.fetchData('Clinic'); | ||
|
|
||
| expect(result).toEqual(locations(0, 3)); | ||
| expect(mockOpenmrsFetch).toHaveBeenCalledTimes(1); | ||
| expect(mockOpenmrsFetch).toHaveBeenCalledWith(`${restBaseUrl}/location?v=custom:(uuid,display)&q=Clinic`); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
url.hostdoes not clear the old port whenwindow.location.hosthas no port. For a page athttps://clinic.example, the next link used in the test becomeshttps://clinic.example:8443/..., not the browser's origin. The second request bypasses the intended proxy and can fail, leaving the dropdown empty. Explicitly copywindow.location.port, including its empty value, after changing the protocol.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks. Fixed in 63c2189: the next link's path and query are now resolved against
window.location.origin, so the backend's host, port and scheme are all dropped, and a new test on a port-lesshttps://clinic.examplepage covers it.