-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Create a new generateDefaultWorkspaceName #85789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bernhardoj
wants to merge
5
commits into
Expensify:main
Choose a base branch
from
bernhardoj:chore/66574-refactor-policy-to-use-useonyx-9
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−14
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f5ac7d
create a new generateDefaultWorkspaceName that doesn't use deprecated…
bernhardoj d2efed7
Merge branch 'main' into chore/66574-refactor-policy-to-use-useonyx-9
bernhardoj 80a6e20
use the new generateDefaultWorkspaceName
bernhardoj 8d2a524
int
bernhardoj e14eb14
remove unused param (yet)
bernhardoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,15 @@ | ||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
|
Check warning on line 2 in src/hooks/useLastWorkspaceNumber.ts
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {lastWorkspaceNumberSelector} from '@src/selectors/Policy'; | ||
| import {emailSelector} from '@src/selectors/Session'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| function useLastWorkspaceNumber() { | ||
| const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector}); | ||
| const lastWorkspaceNumberSelectorWithEmail = (policies: OnyxCollection<Policy>) => lastWorkspaceNumberSelector(policies, sessionEmail ?? ''); | ||
| const [lastWorkspaceNumber] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: lastWorkspaceNumberSelectorWithEmail}); | ||
| return lastWorkspaceNumber; | ||
| } | ||
|
|
||
| export default useLastWorkspaceNumber; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {lastWorkspaceNumberSelector} from '@src/selectors/Policy'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| describe('lastWorkspaceNumberSelector', () => { | ||
| const email = 'jdoe@expensify.com'; | ||
| const displayName = 'Expensify'; | ||
| const workspaceName = `${displayName} Workspace`; | ||
|
|
||
| it('should return undefined when there are no policies', () => { | ||
| expect(lastWorkspaceNumberSelector({}, email)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when email is invalid', () => { | ||
| expect(lastWorkspaceNumberSelector({}, 'invalid-email')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return 0 when there is a matching workspace without a number', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(0); | ||
| }); | ||
|
|
||
| it('should return the number when there is a matching workspace with a number', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: `${workspaceName} 2`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(2); | ||
| }); | ||
|
|
||
| it('should return the maximum number when there are multiple matching workspaces', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${workspaceName} 2`} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}3`]: {name: `${workspaceName} 5`} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}4`]: {name: 'Other Workspace'} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(5); | ||
| }); | ||
|
|
||
| it('should handle SMS domain correctly', () => { | ||
| const smsEmail = `+15555555555${CONST.SMS.DOMAIN}`; | ||
| const smsDisplayName = 'My Group Workspace'; | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: smsDisplayName} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${smsDisplayName} 3`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, smsEmail)).toBe(3); | ||
| }); | ||
|
|
||
| it('should ignore case when matching workspace names', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName.toLowerCase()} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${workspaceName.toUpperCase()} 4`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(4); | ||
| }); | ||
| }); |
This file contains hidden or 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,55 @@ | ||
| import {renderHook} from '@testing-library/react-native'; | ||
| import Onyx from 'react-native-onyx'; | ||
| import useLastWorkspaceNumber from '@hooks/useLastWorkspaceNumber'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
| import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
|
||
| describe('useLastWorkspaceNumber', () => { | ||
| const email = 'jdoe@expensify.com'; | ||
| const displayName = 'Expensify'; | ||
| const workspaceName = `${displayName} Workspace`; | ||
|
|
||
| beforeAll(() => { | ||
| Onyx.init({keys: ONYXKEYS}); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| Onyx.clear(); | ||
| return waitForBatchedUpdates(); | ||
| }); | ||
|
|
||
| it('should return the correct last workspace number from Onyx', async () => { | ||
| await Onyx.merge(ONYXKEYS.SESSION, {email}); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, {name: `${workspaceName} 3`} as Policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => useLastWorkspaceNumber()); | ||
|
|
||
| expect(result.current).toBe(3); | ||
| }); | ||
|
|
||
| it('should update when Onyx data changes', async () => { | ||
| await Onyx.merge(ONYXKEYS.SESSION, {email}); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, {name: `${workspaceName} 3`} as Policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => useLastWorkspaceNumber()); | ||
| expect(result.current).toBe(3); | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}2`, {name: `${workspaceName} 5`} as Policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(result.current).toBe(5); | ||
| }); | ||
|
|
||
| it('should return undefined if no matching workspaces exist', async () => { | ||
| await Onyx.merge(ONYXKEYS.SESSION, {email}); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, {name: 'Other Workspace'} as Policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => useLastWorkspaceNumber()); | ||
|
|
||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issues @bernhardoj