-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shell-ui: Add test for getNodesCount
Refs: #3257
- Loading branch information
1 parent
f926b2b
commit bcfa1a0
Showing
1 changed file
with
82 additions
and
0 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
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(); | ||
}); | ||
}); |