-
Notifications
You must be signed in to change notification settings - Fork 32
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
4b56103
commit 6e1fe54
Showing
7 changed files
with
204 additions
and
2 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
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'; |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { | ||
useQuery, | ||
} from '@tanstack/react-query'; | ||
|
||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
|
||
const useUpdateActiveEnterpriseForUser = ({ enterpriseId, user }) => { | ||
const { username } = user; | ||
const { isLoading, error } = useQuery({ | ||
queryKey: ['updateUsersActiveEnterprise'], | ||
queryFn: async () => { | ||
await LmsApiService.getActiveLinkedEnterprise(username).then(async (linkedEnterprise) => { | ||
if (linkedEnterprise.uuid !== enterpriseId) { | ||
await LmsApiService.updateUserActiveEnterprise(enterpriseId); | ||
} | ||
}); | ||
return true; | ||
}, | ||
}); | ||
|
||
if (error) { logError(`Could not set active enterprise for learner, failed with error: ${logError}`); } | ||
|
||
return { | ||
isLoading, | ||
}; | ||
}; | ||
|
||
export default useUpdateActiveEnterpriseForUser; |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
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 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); | ||
}); | ||
}); |
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