-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d0b9d7
commit 21bf852
Showing
6 changed files
with
112 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,85 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import axios from 'axios'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
|
||
import fetchPaginatedData from '../utils'; | ||
|
||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
...jest.requireActual('@edx/frontend-platform/auth'), | ||
getAuthenticatedHttpClient: jest.fn(), | ||
})); | ||
|
||
const axiosMock = new MockAdapter(axios); | ||
getAuthenticatedHttpClient.mockReturnValue(axios); | ||
|
||
describe('fetchPaginatedData', () => { | ||
const EXAMPLE_ENDPOINT = 'http://example.com/api/v1/data'; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('returns empty data results', async () => { | ||
axiosMock.onGet(EXAMPLE_ENDPOINT).reply(200, { | ||
count: 0, | ||
prev: null, | ||
next: null, | ||
num_pages: 0, | ||
results: [], | ||
}); | ||
const result = await fetchPaginatedData(EXAMPLE_ENDPOINT); | ||
expect(result).toEqual({ | ||
results: [], | ||
response: { | ||
count: 0, | ||
prev: null, | ||
next: null, | ||
numPages: 0, | ||
results: [], | ||
}, | ||
}); | ||
}); | ||
|
||
it('traverses pagination', async () => { | ||
const urlFirstPage = `${EXAMPLE_ENDPOINT}?page=1`; | ||
const urlSecondPage = `${EXAMPLE_ENDPOINT}?page=2`; | ||
const mockResult = { | ||
uuid: uuidv4(), | ||
}; | ||
const mockSecondResult = { | ||
uuid: uuidv4(), | ||
}; | ||
axiosMock.onGet(urlFirstPage).reply(200, { | ||
count: 2, | ||
prev: null, | ||
next: urlSecondPage, | ||
num_pages: 2, | ||
results: [mockResult], | ||
}); | ||
axiosMock.onGet(urlSecondPage).reply(200, { | ||
count: 2, | ||
prev: null, | ||
next: null, | ||
num_pages: 2, | ||
results: [mockSecondResult], | ||
enterprise_features: { | ||
feature_a: true, | ||
}, | ||
}); | ||
const result = await fetchPaginatedData(urlFirstPage); | ||
expect(result).toEqual({ | ||
results: [mockResult, mockSecondResult], | ||
response: { | ||
count: 2, | ||
prev: null, | ||
next: null, | ||
numPages: 2, | ||
results: [mockSecondResult], | ||
enterpriseFeatures: { | ||
featureA: true, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |