-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for perspective detection using extensions
- Loading branch information
1 parent
3f882db
commit a81dcef
Showing
11 changed files
with
229 additions
and
23 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
47 changes: 47 additions & 0 deletions
47
frontend/packages/console-app/src/components/detect-perspective/DetectPerspective.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,47 @@ | ||
import * as React from 'react'; | ||
import { connect, Dispatch } from 'react-redux'; | ||
import { getActivePerspective } from '@console/internal/reducers/ui'; | ||
import { RootState } from '@console/internal/redux'; | ||
import * as UIActions from '@console/internal/actions/ui'; | ||
import PerspectiveDetector from './PerspectiveDetector'; | ||
|
||
type OwnProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
type StateProps = { | ||
activePerspective: string; | ||
}; | ||
|
||
type DispatchProps = { | ||
setActivePerspective: (string) => void; | ||
}; | ||
|
||
type DetectPerspectiveProps = OwnProps & StateProps & DispatchProps; | ||
|
||
const DetectPerspective: React.FC<DetectPerspectiveProps> = ({ | ||
activePerspective, | ||
children, | ||
setActivePerspective, | ||
}) => | ||
activePerspective ? ( | ||
<>{children}</> | ||
) : ( | ||
<PerspectiveDetector setActivePerspective={setActivePerspective} /> | ||
); | ||
|
||
const mapStateToProps = (state: RootState) => ({ | ||
activePerspective: getActivePerspective(state), | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch: Dispatch) => ({ | ||
setActivePerspective: (perspective) => dispatch(UIActions.setActivePerspective(perspective)), | ||
}); | ||
|
||
// For testing | ||
export const InternalDetectPerspective = DetectPerspective; | ||
|
||
export default connect<StateProps, DispatchProps, OwnProps>( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(DetectPerspective); |
40 changes: 40 additions & 0 deletions
40
frontend/packages/console-app/src/components/detect-perspective/PerspectiveDetector.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,40 @@ | ||
import * as React from 'react'; | ||
import { useExtensions, isPerspective } from '@console/plugin-sdk'; | ||
|
||
type PerspectiveDetectorProps = { | ||
setActivePerspective: (string) => void; | ||
}; | ||
|
||
const PerspectiveDetector: React.FC<PerspectiveDetectorProps> = ({ setActivePerspective }) => { | ||
const [detectedPerspective, setDetectedPerspective] = React.useState<string>(); | ||
const perspectiveExtensions = useExtensions(isPerspective); | ||
const defaultPerspective = perspectiveExtensions.find((p) => p.properties.default); | ||
const detectors = perspectiveExtensions.filter((p) => p.properties.usePerspectiveDetection); | ||
const detectionResults = detectors.map((p) => p.properties.usePerspectiveDetection()); | ||
|
||
const detectionComplete = detectionResults.every((result, index) => { | ||
const [enablePerspective, loading] = result; | ||
if (!detectedPerspective && !loading && enablePerspective) { | ||
setDetectedPerspective(detectors[index].properties.id); | ||
} | ||
return loading === false; | ||
}); | ||
|
||
React.useEffect(() => { | ||
if (detectedPerspective) { | ||
setActivePerspective(detectedPerspective); | ||
} else if (detectors.length < 1 || detectionComplete) { | ||
setActivePerspective(defaultPerspective.properties.id); // set default perspective if there are no detectors or none of the detections were successfull | ||
} | ||
}, [ | ||
defaultPerspective, | ||
detectedPerspective, | ||
detectionComplete, | ||
detectors.length, | ||
setActivePerspective, | ||
]); | ||
|
||
return null; | ||
}; | ||
|
||
export default PerspectiveDetector; |
26 changes: 26 additions & 0 deletions
26
...ckages/console-app/src/components/detect-perspective/__tests__/DetectPerspective.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,26 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { InternalDetectPerspective } from '../DetectPerspective'; | ||
import PerspectiveDetector from '../PerspectiveDetector'; | ||
|
||
const MockApp = () => <h1>App</h1>; | ||
|
||
describe('DetectPerspective', () => { | ||
it('should render children if there is an activePerspective', () => { | ||
const wrapper = shallow( | ||
<InternalDetectPerspective activePerspective="dev" setActivePerspective={() => {}}> | ||
<MockApp /> | ||
</InternalDetectPerspective>, | ||
); | ||
expect(wrapper.find(MockApp).exists()).toBe(true); | ||
}); | ||
|
||
it('should render PerspectiveDetector if there is no activePerspective', () => { | ||
const wrapper = shallow( | ||
<InternalDetectPerspective activePerspective={undefined} setActivePerspective={() => {}}> | ||
<MockApp /> | ||
</InternalDetectPerspective>, | ||
); | ||
expect(wrapper.find(PerspectiveDetector).exists()).toBe(true); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
...ages/console-app/src/components/detect-perspective/__tests__/PerspectiveDetector.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,57 @@ | ||
import * as React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { Perspective, useExtensions } from '@console/plugin-sdk'; | ||
import PerspectiveDetector from '../PerspectiveDetector'; | ||
|
||
jest.mock('@console/plugin-sdk', () => ({ | ||
useExtensions: jest.fn(), | ||
})); | ||
|
||
const mockPerspectives = [ | ||
{ | ||
type: 'Perspective', | ||
properties: { | ||
id: 'admin', | ||
name: 'Admin Perspective', | ||
default: true, | ||
}, | ||
}, | ||
{ | ||
type: 'Perspective', | ||
properties: { | ||
id: 'dev', | ||
name: 'Dev Perspective', | ||
usePerspectiveDetection: undefined, | ||
}, | ||
}, | ||
] as Perspective[]; | ||
|
||
const setActivePerspective = jest.fn(); | ||
|
||
describe('PerspectiveDetector', () => { | ||
it('should set default perspective if there are no perspective detectors available', () => { | ||
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives); | ||
|
||
const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />); | ||
expect(wrapper.isEmptyRender()).toBe(true); | ||
expect(setActivePerspective).toHaveBeenCalledWith('admin'); | ||
}); | ||
|
||
it('should set detected perspective if detection is successful', () => { | ||
mockPerspectives[1].properties.usePerspectiveDetection = () => [true, false]; | ||
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives); | ||
|
||
const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />); | ||
expect(wrapper.isEmptyRender()).toBe(true); | ||
expect(setActivePerspective).toHaveBeenCalledWith('dev'); | ||
}); | ||
|
||
it('should set default perspective if detection fails', () => { | ||
mockPerspectives[1].properties.usePerspectiveDetection = () => [false, false]; | ||
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives); | ||
|
||
const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />); | ||
expect(wrapper.isEmptyRender()).toBe(true); | ||
expect(setActivePerspective).toHaveBeenCalledWith('admin'); | ||
}); | ||
}); |
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
37 changes: 37 additions & 0 deletions
37
frontend/packages/dev-console/src/utils/__tests__/usePerspectiveDetection.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,37 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version | ||
import { useSelector } from 'react-redux'; | ||
import { testHook } from '@console/shared/src/test-utils/hooks-utils'; | ||
import { usePerspectiveDetection } from '../usePerspectiveDetection'; | ||
|
||
jest.mock('react-redux', () => ({ | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
describe('usePerspectiveDetection', () => { | ||
it('should return loading as true if CAN_GET_NS flag is pending', () => { | ||
(useSelector as jest.Mock).mockImplementation(() => ({ | ||
CAN_GET_NS: undefined, | ||
})); | ||
|
||
testHook(() => { | ||
const [enablePerspective, loading] = usePerspectiveDetection(); | ||
|
||
expect(enablePerspective).toBe(true); | ||
expect(loading).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should return loading as false if CAN_GET_NS flag is loaded', () => { | ||
(useSelector as jest.Mock).mockImplementation(() => ({ | ||
CAN_GET_NS: false, | ||
})); | ||
|
||
testHook(() => { | ||
const [enablePerspective, loading] = usePerspectiveDetection(); | ||
|
||
expect(enablePerspective).toBe(true); | ||
expect(loading).toBe(false); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
frontend/packages/dev-console/src/utils/usePerspectiveDetection.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,14 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version | ||
import { useSelector } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { getFlagsObject, flagPending } from '@console/internal/reducers/features'; | ||
|
||
export const usePerspectiveDetection = () => { | ||
const flags = useSelector((state: RootState) => getFlagsObject(state)); | ||
const canGetNS = flags.CAN_GET_NS; | ||
const loadingFlag = flagPending(canGetNS); | ||
const enablePerspective = !canGetNS; | ||
|
||
return [enablePerspective, loadingFlag] as [boolean, boolean]; | ||
}; |
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