-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user preference for Create/Edit method (form/yaml)
- Loading branch information
Showing
18 changed files
with
337 additions
and
40 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
1 change: 1 addition & 0 deletions
1
frontend/packages/console-app/src/components/user-preferences/synced-editor/index.ts
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 @@ | ||
export * from './usePreferredCreateEditMethod'; |
11 changes: 11 additions & 0 deletions
11
...console-app/src/components/user-preferences/synced-editor/usePreferredCreateEditMethod.ts
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,11 @@ | ||
import { useUserSettings } from '@console/shared'; | ||
|
||
export const PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST = 'latest'; | ||
const PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_KEY = 'console.preferredCreateEditMethod'; | ||
|
||
export const usePreferredCreateEditMethod = (): [string, boolean] => { | ||
const [preferredCreateEditMethod, , preferredCreateEditMethodLoaded] = useUserSettings<string>( | ||
PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_KEY, | ||
); | ||
return [preferredCreateEditMethod, preferredCreateEditMethodLoaded]; | ||
}; |
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
103 changes: 103 additions & 0 deletions
103
...tend/packages/console-shared/src/components/synced-editor/__tests__/useEditorType.spec.ts
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,103 @@ | ||
import { | ||
PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST, | ||
usePreferredCreateEditMethod, | ||
} from '@console/app/src/components/user-preferences/synced-editor'; | ||
import { useUserSettings } from '@console/shared'; | ||
import { testHook } from '../../../../../../__tests__/utils/hooks-utils'; | ||
import { EditorType } from '../editor-toggle'; | ||
import { useEditorType } from '../useEditorType'; | ||
|
||
jest.mock('@console/shared/src/hooks/useUserSettings', () => ({ | ||
useUserSettings: jest.fn(), | ||
})); | ||
|
||
jest.mock( | ||
'@console/app/src/components/user-preferences/synced-editor/usePreferredCreateEditMethod', | ||
() => ({ | ||
usePreferredCreateEditMethod: jest.fn(), | ||
}), | ||
); | ||
|
||
const mockUserSettings = useUserSettings as jest.Mock; | ||
const mockUsePreferredCreateEditMethod = usePreferredCreateEditMethod as jest.Mock; | ||
|
||
describe('useEditorType', () => { | ||
const lastViewUserSettingKey = 'key'; | ||
const defaultValue = EditorType.Form; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should return editor type corresponding to preferred editor type if it is defined and enabled', () => { | ||
mockUserSettings.mockReturnValue([EditorType.Form, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(true); | ||
}); | ||
|
||
it(`should return editor type corresponding to last viewed editor type if it is defined and enabled and preferred editor type is ${PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST}`, () => { | ||
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([ | ||
PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST, | ||
true, | ||
]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(true); | ||
}); | ||
|
||
it('should return editor type corresponding to last viewed editor type if it is defined and enabled preferred editor type is defined but disabled', () => { | ||
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.Form, true]); | ||
const { result } = testHook(() => | ||
useEditorType( | ||
lastViewUserSettingKey, | ||
defaultValue, | ||
(type: string) => !(type === EditorType.Form), | ||
), | ||
); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(true); | ||
}); | ||
|
||
it('should return editor type corresponding to last viewed editor type if it is defined and enabled and preferred editor type is not defined', () => { | ||
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, true]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(true); | ||
}); | ||
|
||
it('should return editor type corresponding to default value if both preferred and last viewed editor type are not defined or disabled', () => { | ||
mockUserSettings.mockReturnValue([undefined, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, true]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(defaultValue); | ||
expect(loaded).toBe(true); | ||
}); | ||
|
||
it('should return false for loaded if preferred editor type has not loaded', () => { | ||
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, false]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(false); | ||
}); | ||
|
||
it('should return false for loaded if last viewed editor type has not loaded', () => { | ||
mockUserSettings.mockReturnValue([undefined, jest.fn(), false]); | ||
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]); | ||
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue)); | ||
const [editorType, , loaded] = result.current; | ||
expect(editorType).toEqual(EditorType.YAML); | ||
expect(loaded).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.