-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto setting active linked enterprise
- Loading branch information
1 parent
cc98d50
commit 80ebfb1
Showing
11 changed files
with
273 additions
and
4 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as useEnterpriseCuration } from './useEnterpriseCuration'; | ||
export { default as useEnterpriseCurationContext } from './useEnterpriseCurationContext'; | ||
export { default as useUpdateActiveEnterpriseForUser } from './useUpdateActiveEnterpriseForUser'; |
42 changes: 42 additions & 0 deletions
42
src/components/EnterpriseApp/data/hooks/useUpdateActiveEnterpriseForUser.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { | ||
useMutation, useQuery, | ||
} from '@tanstack/react-query'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
|
||
const useUpdateActiveEnterpriseForUser = ({ enterpriseId, user }) => { | ||
// Sets up POST call to update active enterprise. | ||
const { mutate, isLoading: isUpdatingActiveEnterprise } = useMutation({ | ||
mutationFn: () => LmsApiService.updateUserActiveEnterprise(enterpriseId), | ||
onError: () => { | ||
logError("Failed to update user's active enterprise"); | ||
}, | ||
}); | ||
const { username } = user; | ||
const { | ||
data, | ||
isLoading: isLoadingActiveEnterprise, | ||
} = useQuery({ | ||
queryKey: ['activeLinkedEnterpriseCustomer', username], | ||
queryFn: () => LmsApiService.getActiveLinkedEnterprise(username), | ||
meta: { | ||
errorMessage: "Failed to fetch user's active enterprise", | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!data) { return; } | ||
if (data.uuid !== enterpriseId) { | ||
mutate(enterpriseId); | ||
} | ||
}, [data, enterpriseId, mutate]); | ||
|
||
return { | ||
isLoading: isLoadingActiveEnterprise || isUpdatingActiveEnterprise, | ||
}; | ||
}; | ||
|
||
export default useUpdateActiveEnterpriseForUser; |
92 changes: 92 additions & 0 deletions
92
src/components/EnterpriseApp/data/hooks/useUpdateActiveEnterpriseForUser.test.jsx
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,92 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { QueryClientProvider } from '@tanstack/react-query'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { useUpdateActiveEnterpriseForUser } from './index'; | ||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
import { queryClient } from '../../../test/testUtils'; | ||
|
||
jest.mock('../../../../data/services/LmsApiService'); | ||
jest.mock('@edx/frontend-platform/logging', () => ({ | ||
...jest.requireActual('@edx/frontend-platform/logging'), | ||
logError: jest.fn(), | ||
})); | ||
|
||
describe('useUpdateActiveEnterpriseForUser', () => { | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient()}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
const mockEnterpriseId = 'enterprise-uuid'; | ||
const mockUser = { username: 'joe_shmoe' }; | ||
const connectedEnterprise = 'someID'; | ||
beforeEach(() => { | ||
LmsApiService.getActiveLinkedEnterprise.mockResolvedValue({ uuid: connectedEnterprise }); | ||
}); | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
it("should update user's active enterprise if it differs from the current enterprise", async () => { | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useUpdateActiveEnterpriseForUser({ | ||
enterpriseId: mockEnterpriseId, | ||
user: mockUser, | ||
}), | ||
{ wrapper }, | ||
); | ||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(LmsApiService.updateUserActiveEnterprise).toHaveBeenCalledTimes(1); | ||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
|
||
it('should do nothing if active enterprise is the same as current enterprise', async () => { | ||
// Pass the value of the enterprise ID returned by ``getActiveLinkedEnterprise`` to the hook | ||
const { waitForNextUpdate } = renderHook( | ||
() => useUpdateActiveEnterpriseForUser({ | ||
enterpriseId: connectedEnterprise, | ||
user: mockUser, | ||
}), | ||
{ wrapper }, | ||
); | ||
await waitForNextUpdate(); | ||
expect(LmsApiService.updateUserActiveEnterprise).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should handle useMutation errors', async () => { | ||
LmsApiService.updateUserActiveEnterprise.mockRejectedValueOnce(Error('uh oh')); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useUpdateActiveEnterpriseForUser({ | ||
enterpriseId: mockEnterpriseId, | ||
user: mockUser, | ||
}), | ||
{ wrapper }, | ||
); | ||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(LmsApiService.updateUserActiveEnterprise).toHaveBeenCalledTimes(1); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(logError).toHaveBeenCalledTimes(1); | ||
}); | ||
it('should handle useQuery errors', async () => { | ||
LmsApiService.getActiveLinkedEnterprise.mockRejectedValueOnce(Error('uh oh')); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useUpdateActiveEnterpriseForUser({ | ||
enterpriseId: mockEnterpriseId, | ||
user: mockUser, | ||
}), | ||
{ wrapper }, | ||
); | ||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(LmsApiService.getActiveLinkedEnterprise).toHaveBeenCalledTimes(1); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(logError).toHaveBeenCalledWith("Failed to fetch user's active enterprise"); | ||
}); | ||
}); |
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
Oops, something went wrong.