Skip to content

Commit 336da5e

Browse files
authored
add-mocks (#72)
1 parent f1186cd commit 336da5e

File tree

12 files changed

+105
-25
lines changed

12 files changed

+105
-25
lines changed

.github/workflows/pr__unit_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Yarn
1919
uses: actions/setup-node@v2
2020
with:
21-
node-version: '14'
21+
node-version: '16'
2222
cache: 'yarn'
2323
- run: yarn install
2424
- run: yarn build
@@ -33,7 +33,7 @@ jobs:
3333
- name: Setup Yarn
3434
uses: actions/setup-node@v2
3535
with:
36-
node-version: '14'
36+
node-version: '16'
3737
cache: 'yarn'
3838
- run: yarn install
3939

__mocks__/custom.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.svg' {
2+
const content: any;
3+
export default content;
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useK8sWatchResource';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from 'react';
2+
import { K8sResourceCommon, WatchK8sResource } from '@openshift-console/dynamic-plugin-sdk';
3+
4+
const getURL = (props: WatchK8sResource): string => {
5+
const {name, namespace, namespaced, isList, groupVersionKind} = props;
6+
const {group, version, kind} = groupVersionKind;
7+
if (!group || !kind || !version) {
8+
return;
9+
}
10+
11+
const baseURL ='/api/kubernetes/apis/' + group + '/' + version;
12+
const namespaceURL =
13+
namespaced ? '/namespaces/' + (namespace.toString() || 'default') : '';
14+
const resourceKind = kind.toLowerCase();
15+
const pluralize = isList ? 's' : '';
16+
const resourceName = name ? name.toString() : '';
17+
const url = baseURL + namespaceURL + '/' + resourceKind + pluralize + '/' + resourceName;
18+
19+
return url;
20+
};
21+
22+
export const useK8sWatchResource = <R extends K8sResourceCommon | K8sResourceCommon[]>(
23+
props: WatchK8sResource | null,
24+
) => {
25+
const [loaded, setLoaded] = React.useState(false);
26+
const [loadError, setLoadError] = React.useState<string>(null);
27+
const [data, setData] = React.useState<R>();
28+
29+
React.useEffect(() => {
30+
const url = getURL(props);
31+
32+
fetch(url)
33+
.then(response => response.json())
34+
.then((jsonData) => {
35+
setData(jsonData as R);
36+
setLoaded(true);
37+
})
38+
.catch((error) => {
39+
setLoadError(error);
40+
setLoaded(true);
41+
});
42+
43+
// eslint-disable-next-line react-hooks/exhaustive-deps
44+
}, [props?.name, props?.namespace]);
45+
46+
return [data, loaded, loadError];
47+
};
48+
49+
export default useK8sWatchResource;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "Yaacov Zamir <[email protected]>",
1111
"scripts": {
1212
"clean": "rm -rf dist",
13-
"build": "rollup -c",
13+
"build": "rollup -c rollup.config.js",
1414
"lint": "eslint src --color",
1515
"lint:fix": "yarn lint --fix",
1616
"check-types": "tsc",
@@ -74,6 +74,7 @@
7474
"rollup": "^2.63.0",
7575
"rollup-plugin-dts": "^4.1.0",
7676
"rollup-plugin-postcss": "^4.0.2",
77+
"rollup-plugin-svg": "^2.0.0",
7778
"sass": "^1.49.4",
7879
"sass-loader": "^12.4.0",
7980
"style-loader": "^3.3.1",

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default [
2727
external: ['react', 'react-dom'],
2828
},
2929
{
30-
input: 'dist/esm/types/src/index.d.ts',
30+
input: 'src/index.ts',
3131
output: [{ file: 'dist/index.d.ts', format: 'esm' }],
3232
plugins: [dts()],
3333
external: ['react', 'react-dom', /\.(css|less|scss)$/],

src/components/utils/ConditionLabel/ConditionLabel.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22

33
import { Label, Popover, PopoverPosition } from '@patternfly/react-core';
4+
45
export interface k8sStatusConditions {
56
/**
67
* Type indicate which type of condition is.
@@ -26,12 +27,9 @@ export interface k8sStatusConditions {
2627
*
2728
* On mouse click it creates a popover to explain the state in a more disursive way.
2829
*/
29-
export const ConditionLabel: React.FC<k8sStatusConditions> = React.memo((condition) => {
30+
export const ConditionLabel: React.FC<k8sStatusConditions> = ({ message, status, reason }) => {
3031
const preventLabelLink = React.useCallback((e) => e.preventDefault(), []);
31-
const getBodyContent = React.useCallback(
32-
() => <div>{condition?.message}</div>,
33-
[condition?.message],
34-
);
32+
const getBodyContent = React.useCallback(() => <div>{message}</div>, [message]);
3533

3634
return (
3735
<Popover
@@ -40,9 +38,9 @@ export const ConditionLabel: React.FC<k8sStatusConditions> = React.memo((conditi
4038
bodyContent={getBodyContent}
4139
>
4240
<Label color="grey" isTruncated href="#" onClick={preventLabelLink}>
43-
{condition?.reason}={condition?.status}
41+
{reason}={status}
4442
</Label>
4543
</Popover>
4644
);
47-
});
45+
};
4846
ConditionLabel.displayName = 'ConditionLabel';

styleguide.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
const path = require('path');
3-
const { createProxyMiddleware } = require('http-proxy-middleware');
43

54
module.exports = {
65
propsParser: require('react-docgen-typescript').withDefaultConfig().parse,

styleguide.config.proxy.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ module.exports = {
6060
},
6161
assetsDir: path.join(__dirname, 'styleguide/assets'),
6262
configureServer(app) {
63-
app.use('/api/kubernetes', createProxyMiddleware({
64-
target: process.env.BRIDGE_CLUSTER_ENDPOINT,
65-
secure: false,
66-
changeOrigin: true,
67-
ws: true,
68-
headers: {Authorization:`Bearer ${process.env.BRIDGE_AUTH_BEARER_TOKEN}`},
69-
pathRewrite: {
63+
app.use(
64+
'/api/kubernetes',
65+
createProxyMiddleware({
66+
target: process.env.BRIDGE_CLUSTER_ENDPOINT,
67+
secure: false,
68+
changeOrigin: true,
69+
ws: true,
70+
headers: { Authorization: `Bearer ${process.env.BRIDGE_AUTH_BEARER_TOKEN}` },
71+
pathRewrite: {
7072
[`^/api/kubernetes`]: '',
71-
},
72-
}));
73-
},
73+
},
74+
}),
75+
);
76+
},
7477
};

styleguide/webpack.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const webpack = require('webpack');
55
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
66

77
const allKubevirtTypes = fs
8-
.readdirSync(path.resolve(__dirname, '../node_modules/@kubevirt-ui/kubevirt-api/kubevirt/models'))
8+
.readdirSync(path.resolve(__dirname, '../node_modules/@kubevirt-ui/kubevirt-api'))
99
.map((filename) => filename.replace(/\.[^/.]+$/, ''));
1010

1111
module.exports = () => {
@@ -54,6 +54,9 @@ module.exports = () => {
5454
},
5555
resolve: {
5656
extensions: ['.js', '.ts', '.tsx', '.jsx'],
57+
alias: {
58+
'@openshift-console/dynamic-plugin-sdk': '@dynamic-plugin-sdk-mocks',
59+
},
5760
plugins: [
5861
new TsconfigPathsPlugin({
5962
configFile: path.resolve(__dirname, '../tsconfig.json'),

0 commit comments

Comments
 (0)