-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(container): updated user widget and support dynamic updates
ref:MANAGER-14829 Signed-off-by: Omar ALKABOUSS MOUSSANA <[email protected]>
- Loading branch information
Omar ALKABOUSS MOUSSANA
committed
Oct 3, 2024
1 parent
73f8e6b
commit ae4ae52
Showing
9 changed files
with
134 additions
and
14 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
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
60 changes: 60 additions & 0 deletions
60
packages/manager/apps/container/src/hooks/user/useUser.spec.tsx
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,60 @@ | ||
import { renderHook, act, waitFor } from '@testing-library/react'; | ||
import { User } from '@ovh-ux/manager-config'; | ||
import useUser from '@/hooks/user/useUser'; | ||
import { describe, it, vi } from 'vitest'; | ||
|
||
const userMock: Partial<User> = { | ||
phoneCountry: 'FR', | ||
firstname: 'john', | ||
name: 'jihn' | ||
}; | ||
|
||
const updatedUserMock: Partial<User> = { | ||
...userMock, | ||
phoneCountry: 'EN', | ||
firstname: 'doe', | ||
}; | ||
|
||
const environmentMock = { | ||
user: userMock, | ||
}; | ||
const onUserChangeMock = vi.fn(); | ||
const pluginEnvironmentMock = { | ||
getEnvironment: vi.fn(() => environmentMock), | ||
onUserChange: onUserChangeMock, | ||
}; | ||
|
||
const shellMock = { | ||
getPlugin: vi.fn(() => pluginEnvironmentMock), | ||
}; | ||
|
||
vi.mock('@/context', () => ({ | ||
useShell: vi.fn(() => shellMock), | ||
})); | ||
|
||
describe('useUser', () => { | ||
it('returns the initial user from the environment', async () => { | ||
const { result } = renderHook(() => useUser()); | ||
|
||
onUserChangeMock.mockClear(); | ||
|
||
await waitFor(() => { | ||
expect(result.current).toEqual(userMock); | ||
expect(shellMock.getPlugin).toHaveBeenCalledWith('environment'); | ||
}); | ||
}); | ||
|
||
it('updates the user when onUserChange is triggered', async () => { | ||
const { result } = renderHook(() => useUser()); | ||
|
||
expect(result.current).not.toEqual(updatedUserMock); | ||
|
||
act(() => { | ||
onUserChangeMock.mock.calls[0][0](updatedUserMock); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current).toEqual(updatedUserMock); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/manager/apps/container/src/hooks/user/useUser.tsx
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,22 @@ | ||
import { Environment, User } from '@ovh-ux/manager-config'; | ||
import { useEffect, useState } from 'react'; | ||
import { useShell } from '@/context'; | ||
|
||
|
||
const useUser = (): User => { | ||
const shell = useShell(); | ||
|
||
const pluginEnvironement = shell.getPlugin('environment'); | ||
const environment: Environment = pluginEnvironement.getEnvironment(); | ||
const [user, setUser] = useState<User>(environment.user); | ||
|
||
useEffect(() => { | ||
pluginEnvironement.onUserChange((updatedUser: User) => { | ||
setUser(updatedUser); | ||
}); | ||
}, []); | ||
|
||
return user; | ||
} | ||
|
||
export default useUser; |
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