-
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
c67c5e4
commit 70423df
Showing
6 changed files
with
205 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'; |
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
|
||
const useUpdateActiveEnterpriseForUser = ({ | ||
enterpriseId, | ||
user, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const updateActiveEnterpriseAndRefreshJWT = useCallback(async (username, currentEnterpriseId) => { | ||
setIsLoading(true); | ||
try { | ||
await LmsApiService.getActiveLinkedEnterprise(username).then(async (linkedEnterpriseId) => { | ||
if (linkedEnterpriseId !== currentEnterpriseId) { | ||
await LmsApiService.updateUserActiveEnterprise(currentEnterpriseId); | ||
await LmsApiService.loginRefresh(); | ||
} | ||
}); | ||
} catch (error) { | ||
logError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!(enterpriseId && user)) { | ||
return; | ||
} | ||
const { username } = user; | ||
updateActiveEnterpriseAndRefreshJWT(username, enterpriseId); | ||
}, [enterpriseId, user, updateActiveEnterpriseAndRefreshJWT]); | ||
return { | ||
isLoading, | ||
}; | ||
}; | ||
|
||
export default useUpdateActiveEnterpriseForUser; |
78 changes: 78 additions & 0 deletions
78
src/components/EnterpriseApp/data/hooks/useUpdateActiveEnterpriseForUser.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useUpdateActiveEnterpriseForUser } from './index'; | ||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
|
||
jest.mock('../../../../data/services/LmsApiService'); | ||
|
||
describe('useUpdateActiveEnterpriseForUser', () => { | ||
const mockEnterpriseId = 'enterprise-uuid'; | ||
const mockCurrentActiveEnterpriseId = 'current-active-enterprise-uuid'; | ||
const mockUser = { | ||
roles: [ | ||
'enterprise_admin:random-enterprise-uuid', | ||
`enterprise_learner:${mockCurrentActiveEnterpriseId}`, | ||
`enterprise_learner:${mockEnterpriseId}`, | ||
], | ||
}; | ||
beforeEach(() => { | ||
LmsApiService.getActiveLinkedEnterprise.mockResolvedValue('someID'); | ||
}); | ||
|
||
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, | ||
})); | ||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(LmsApiService.updateUserActiveEnterprise).toHaveBeenCalledTimes(1); | ||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
enterpriseId: undefined, | ||
user: mockUser, | ||
}, | ||
{ | ||
enterpriseId: mockEnterpriseId, | ||
user: undefined, | ||
}, | ||
{ | ||
enterpriseId: mockEnterpriseId, | ||
user: { | ||
roles: [`enterprise_learner:${mockEnterpriseId}`], | ||
}, | ||
}, | ||
])('should do nothing if missing enterpriseId or user, or active enterprise is the same as current enterprise', async ( | ||
{ | ||
enterpriseId, | ||
user, | ||
}, | ||
) => { | ||
renderHook(() => useUpdateActiveEnterpriseForUser({ | ||
enterpriseId, | ||
user, | ||
})); | ||
|
||
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, | ||
})); | ||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(LmsApiService.updateUserActiveEnterprise).toHaveBeenCalledTimes(1); | ||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
}); |
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