Skip to content

Commit

Permalink
shell-ui: Add test for getNodesCount
Browse files Browse the repository at this point in the history
Refs: #3257
  • Loading branch information
ChengYanJin committed Apr 14, 2021
1 parent 347d76e commit 1878730
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions shell-ui/src/platform/service/k8s.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//@flow
import '../library';
import React, { createContext, useContext } from 'react';
import { version } from '../../../package.json';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
import { getNodesCountQuery, getVolumesCountQuery } from './k8s.js';
import { afterAll, beforeAll, jest } from '@jest/globals';

const k8sUrl = 'https://10.0.0.1:8443/api/kubernetes';

const nodes = {
kind: 'NodeList',
apiVersion: 'v1',
metadata: {
selfLink: '/api/v1/nodes',
resourceVersion: '8885971',
},
items: [{}],
};

const persistentvolumes = {
kind: 'PersistentVolumeList',
apiVersion: 'v1',
metadata: {
selfLink: '/api/v1/persistentvolumes',
resourceVersion: '9186732',
},
items: [{}, {}, {}, {}, {}],
};

const token = 'eyxxxx';
const server = setupServer(
rest.get(`${k8sUrl}/api/v1/nodes`, (req, res, ctx) => {
return res(ctx.json(nodes));
}),
rest.get(`${k8sUrl}/api/v1/persistentvolumes`, (req, res, ctx) => {
return res(ctx.json(persistentvolumes));
}),
);

describe('getNodesCount', () => {
jest.useFakeTimers();
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => server.resetHandlers());

const wrapper = ({ children }) => (
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
);

it('should return the number of nodes in the cluster', async () => {
// S
const { result, waitForNextUpdate } = renderHook(
() => useQuery(getNodesCountQuery(k8sUrl, token)),
{ wrapper },
);
// E
await waitForNextUpdate();
// V
expect(result.current.data).toEqual(1);
});

it('should return the number of volumes in the cluster', async () => {
// S
const { result, waitForNextUpdate } = renderHook(
() => useQuery(getVolumesCountQuery(k8sUrl, token)),
{ wrapper },
);
// E
await waitForNextUpdate();
// V
expect(result.current.data).toEqual(5);
});

afterAll(() => {
server.close();
});
});

0 comments on commit 1878730

Please sign in to comment.