-
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(pci-common): add 3AZ chip condition (#14716)
Signed-off-by: tsiorifamonjena <[email protected]> Signed-off-by: Simon Chaumet <[email protected]> Co-authored-by: Simon Chaumet <[email protected]>
- Loading branch information
1 parent
24b6611
commit 0afc18d
Showing
9 changed files
with
247 additions
and
5 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
20 changes: 20 additions & 0 deletions
20
...ages/manager/modules/manager-pci-common/src/contexts/PCICommonContext/PCICommonContext.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,20 @@ | ||
import { createContext } from 'react'; | ||
|
||
export interface InternalMeta { | ||
has3AZ?: boolean; | ||
} | ||
|
||
/** | ||
* This type is only used to add completion with LSP | ||
*/ | ||
export type PCICommonMetaType = | ||
| InternalMeta | ||
| Record<string, unknown> | ||
| undefined; | ||
|
||
/** | ||
* Use {usePCICommonContextFactory} for general usage. | ||
* | ||
* Use this to override previously set properties | ||
*/ | ||
export const PCICommonContext = createContext<PCICommonMetaType>(undefined); |
32 changes: 32 additions & 0 deletions
32
packages/manager/modules/manager-pci-common/src/hooks/useHas3AZ/useHas3AZ.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,32 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useHas3AZ } from './useHas3AZ'; | ||
import { PCICommonContext } from '../../contexts/PCICommonContext/PCICommonContext'; | ||
import { usePCICommonContextFactory } from '../usePCICommonContextFactory/usePCICommonContextFactory'; | ||
|
||
describe('useHas3AZ', () => { | ||
it.each([ | ||
[false, undefined], | ||
[false, {}], | ||
[false, { has3AZ: false }], | ||
[false, { has3AZ: 'whatever' }], | ||
[false, { has3AZ: null }], | ||
[true, { has3AZ: true }], | ||
])( | ||
'should return %s when meta is %o', | ||
(expected: boolean, meta: { has3AZ: boolean | string } | undefined) => { | ||
const { result } = renderHook(() => useHas3AZ(), { | ||
wrapper: ({ children }) => { | ||
const pciCommonContext = usePCICommonContextFactory(meta); | ||
|
||
return ( | ||
<PCICommonContext.Provider value={pciCommonContext}> | ||
{children} | ||
</PCICommonContext.Provider> | ||
); | ||
}, | ||
}); | ||
|
||
expect(result.current).toBe(expected); | ||
}, | ||
); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/manager/modules/manager-pci-common/src/hooks/useHas3AZ/useHas3AZ.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,10 @@ | ||
import { useContext } from 'react'; | ||
import { PCICommonContext } from '../../contexts/PCICommonContext/PCICommonContext'; | ||
|
||
export const useHas3AZ = (): boolean => { | ||
const meta = useContext(PCICommonContext); | ||
|
||
return meta && 'has3AZ' in meta && typeof meta.has3AZ === 'boolean' | ||
? meta.has3AZ | ||
: false; | ||
}; |
116 changes: 116 additions & 0 deletions
116
...nager-pci-common/src/hooks/usePCICommonContextFactory/usePCICommonContextFactory.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,116 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { PropsWithChildren, useContext } from 'react'; | ||
import { PCICommonContext } from '@/contexts/PCICommonContext/PCICommonContext'; | ||
import { usePCICommonContextFactory } from './usePCICommonContextFactory'; | ||
|
||
const myVarChildren = { | ||
myVar: 'children', | ||
}; | ||
|
||
const TestChildrenMerge = ({ children }: PropsWithChildren) => { | ||
const pciCommonContext = usePCICommonContextFactory(myVarChildren); | ||
|
||
return ( | ||
<PCICommonContext.Provider value={pciCommonContext}> | ||
{children} | ||
</PCICommonContext.Provider> | ||
); | ||
}; | ||
|
||
const TestChildrenOverride = ({ children }: PropsWithChildren) => ( | ||
<PCICommonContext.Provider value={myVarChildren}> | ||
{children} | ||
</PCICommonContext.Provider> | ||
); | ||
|
||
const ParentWithoutProvider = ({ children }: PropsWithChildren) => ( | ||
<TestChildrenMerge>{children}</TestChildrenMerge> | ||
); | ||
|
||
const myVarParent = { | ||
myVar: 'parent', | ||
}; | ||
|
||
const ParentWithMyVar = ({ children }: PropsWithChildren) => { | ||
const pciCommonContext = usePCICommonContextFactory(myVarParent); | ||
|
||
return ( | ||
<PCICommonContext.Provider value={pciCommonContext}> | ||
<TestChildrenMerge>{children}</TestChildrenMerge> | ||
</PCICommonContext.Provider> | ||
); | ||
}; | ||
|
||
const myVar2Parent = { | ||
myVar2: 'parent', | ||
}; | ||
|
||
const ParentWithMyVar2 = ({ children }: PropsWithChildren) => { | ||
const pciCommonContext = usePCICommonContextFactory(myVar2Parent); | ||
|
||
return ( | ||
<PCICommonContext.Provider value={pciCommonContext}> | ||
{children} | ||
</PCICommonContext.Provider> | ||
); | ||
}; | ||
|
||
const ParentWithEmptyFactory = ({ children }: PropsWithChildren) => { | ||
const pciCommonContext = usePCICommonContextFactory(); | ||
|
||
return ( | ||
<PCICommonContext.Provider value={pciCommonContext}> | ||
{children} | ||
</PCICommonContext.Provider> | ||
); | ||
}; | ||
|
||
describe('usePCICommonContextFactory', () => { | ||
it('should set value', () => { | ||
const { result } = renderHook(() => useContext(PCICommonContext), { | ||
wrapper: ParentWithoutProvider, | ||
}); | ||
|
||
expect(result.current).toEqual({ myVar: 'children' }); | ||
}); | ||
|
||
it('should override previously set value', () => { | ||
const { result } = renderHook(() => useContext(PCICommonContext), { | ||
wrapper: ParentWithMyVar, | ||
}); | ||
|
||
expect(result.current).toEqual({ myVar: 'children' }); | ||
}); | ||
|
||
it('should merge with old value', () => { | ||
const { result } = renderHook(() => useContext(PCICommonContext), { | ||
wrapper: ({ children }) => ( | ||
<ParentWithMyVar2> | ||
<TestChildrenMerge>{children}</TestChildrenMerge> | ||
</ParentWithMyVar2> | ||
), | ||
}); | ||
|
||
expect(result.current).toEqual({ myVar: 'children', myVar2: 'parent' }); | ||
}); | ||
|
||
it('should override old value', () => { | ||
const { result } = renderHook(() => useContext(PCICommonContext), { | ||
wrapper: ({ children }) => ( | ||
<ParentWithMyVar2> | ||
<TestChildrenOverride>{children}</TestChildrenOverride> | ||
</ParentWithMyVar2> | ||
), | ||
}); | ||
|
||
expect(result.current).toEqual({ myVar: 'children' }); | ||
}); | ||
|
||
it('should work with no values', () => { | ||
const { result } = renderHook(() => useContext(PCICommonContext), { | ||
wrapper: ParentWithEmptyFactory, | ||
}); | ||
|
||
expect(result.current).toEqual({}); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
...les/manager-pci-common/src/hooks/usePCICommonContextFactory/usePCICommonContextFactory.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,17 @@ | ||
import { useContext, useMemo } from 'react'; | ||
import { | ||
PCICommonContext, | ||
PCICommonMetaType, | ||
} from '../../contexts/PCICommonContext/PCICommonContext'; | ||
|
||
/** | ||
* Use this to merge meta properties with previously set properties (merge is only on level 1) | ||
* | ||
* To override all properties use the provider directly | ||
* @param meta Be sure to create it outside your component or memoize it to avoid performance issues | ||
*/ | ||
export function usePCICommonContextFactory(meta?: PCICommonMetaType) { | ||
const base = useContext(PCICommonContext); | ||
|
||
return useMemo(() => ({ ...(base || {}), ...(meta || {}) }), [base, meta]); | ||
} |
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