-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor notices provider to useQuery (#992)
* feat: refactor notices provider to useQuery * chore: merge fix * chore: cleanup * fix: redirects without momentary render * fix: debugging * chore: Testing and cleanup * chore: PR feedback
- Loading branch information
1 parent
d0b1f41
commit cffd0b4
Showing
13 changed files
with
143 additions
and
129 deletions.
There are no files selected for viewing
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,12 @@ | ||
import { queries } from '../../../../../utils/queryKeyFactory'; | ||
|
||
/** | ||
* Helper function to assist querying with useQuery package | ||
* | ||
* @returns {Types.QueryObject} - The query object for notices. | ||
* @property {[string]} QueryObject.queryKey - The query key for the object | ||
* @property {func} QueryObject.queryFn - The asynchronous API request "fetchNotices" | ||
*/ | ||
export default function queryNotices() { | ||
return queries.user.notices; | ||
} |
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,76 @@ | ||
/* eslint-disable react/jsx-filename-extension */ | ||
import { render, waitFor, screen } from '@testing-library/react'; | ||
import { useState } from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import axios from 'axios'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { Button } from '@openedx/paragon'; | ||
import { fetchNotices } from '../services'; | ||
|
||
const APP_CONFIG = { | ||
USE_API_CACHE: true, | ||
DISCOVERY_API_BASE_URL: 'http://localhost:18381', | ||
LMS_BASE_URL: 'http://localhost:18000', | ||
}; | ||
jest.mock('@edx/frontend-platform/config', () => ({ | ||
...jest.requireActual('@edx/frontend-platform/config'), | ||
getConfig: jest.fn(() => APP_CONFIG), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedUser: jest.fn(() => ({ id: 12345 })), | ||
getAuthenticatedHttpClient: jest.fn(), | ||
})); | ||
|
||
const axiosMock = new MockAdapter(axios); | ||
getAuthenticatedHttpClient.mockReturnValue(axios); | ||
|
||
describe('fetchNotices', () => { | ||
const NOTICES_ENDPOINT = `${APP_CONFIG.LMS_BASE_URL }/notices/api/v1/unacknowledged`; | ||
const ComponentWithNotices = () => { | ||
const [output, setOuput] = useState(null); | ||
const onClickHandler = async () => { | ||
const apiOutput = await fetchNotices(); | ||
if (apiOutput?.results.length > 0) { | ||
setOuput(apiOutput.results[0]); | ||
return; | ||
} | ||
setOuput('No Results'); | ||
}; | ||
return ( | ||
<Button data-testid="fetchNotices" onClick={onClickHandler}>{output || 'hi'}</Button> | ||
); | ||
}; | ||
|
||
// Preserves original window location, and swaps it back after test is completed | ||
const currentLocation = window.location; | ||
beforeAll(() => { | ||
delete window.location; | ||
window.location = { ...currentLocation, assign: jest.fn() }; | ||
}); | ||
afterAll(() => { | ||
window.location = currentLocation; | ||
}); | ||
it('returns empty data results and does not assign the window location', async () => { | ||
axiosMock.onGet(NOTICES_ENDPOINT).reply(200, { results: [] }); | ||
render(<ComponentWithNotices />); | ||
userEvent.click(screen.getByTestId('fetchNotices')); | ||
await waitFor(() => expect(window.location.assign).not.toHaveBeenCalled()); | ||
}); | ||
it('returns logInfo on 404', async () => { | ||
axiosMock.onGet(NOTICES_ENDPOINT).reply(404, {}); | ||
render(<ComponentWithNotices />); | ||
userEvent.click(screen.getByTestId('fetchNotices')); | ||
await waitFor(() => expect(window.location.assign).not.toHaveBeenCalled()); | ||
}); | ||
it('assigns the window location on successful API response', async () => { | ||
const currentHref = window.location.href; | ||
axiosMock.onGet(NOTICES_ENDPOINT).reply(200, { results: [APP_CONFIG.LMS_BASE_URL] }); | ||
render(<ComponentWithNotices />); | ||
userEvent.click(screen.getByTestId('fetchNotices')); | ||
await waitFor(() => expect(window.location.assign).toHaveBeenCalledWith( | ||
`${APP_CONFIG.LMS_BASE_URL }?next=${currentHref}`, | ||
)); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/components/app/routes/data/utils.test.js → ...nents/app/routes/data/tests/utils.test.js
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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