|
4 | 4 | kubeObjectListQuery, |
5 | 5 | ListResponse, |
6 | 6 | makeListRequests, |
| 7 | + useKubeObjectList, |
7 | 8 | useWatchKubeObjectLists, |
8 | 9 | } from './useKubeObjectList'; |
9 | 10 | import * as websocket from './webSocket'; |
@@ -81,6 +82,16 @@ const mockClass = class { |
81 | 82 | static apiVersion = 'v1'; |
82 | 83 | static apiName = 'pods'; |
83 | 84 |
|
| 85 | + static apiEndpoint = { |
| 86 | + apiInfo: [ |
| 87 | + { |
| 88 | + group: '', |
| 89 | + resource: 'pods', |
| 90 | + version: 'v1', |
| 91 | + }, |
| 92 | + ], |
| 93 | + }; |
| 94 | + |
84 | 95 | constructor(public jsonData: any) {} |
85 | 96 | } as any; |
86 | 97 |
|
@@ -221,3 +232,42 @@ describe('useWatchKubeObjectLists', () => { |
221 | 232 | ).toBe(objectB); |
222 | 233 | }); |
223 | 234 | }); |
| 235 | + |
| 236 | +describe('useKubeObjectList', () => { |
| 237 | + it('should call useKubeObjectList with 1 namespace after reducing amount of namespaces', async () => { |
| 238 | + const spy = vi.spyOn(websocket, 'useWebSockets'); |
| 239 | + const queryClient = new QueryClient(); |
| 240 | + |
| 241 | + queryClient.setQueryData(['kubeObject', 'list', 'v1', 'pods', 'default', 'a', {}], { |
| 242 | + list: { items: [], metadata: { resourceVersion: '0' } }, |
| 243 | + cluster: 'default', |
| 244 | + namespace: 'a', |
| 245 | + }); |
| 246 | + queryClient.setQueryData(['kubeObject', 'list', 'v1', 'pods', 'default', 'b', {}], { |
| 247 | + list: { items: [], metadata: { resourceVersion: '0' } }, |
| 248 | + cluster: 'default', |
| 249 | + namespace: 'b', |
| 250 | + }); |
| 251 | + |
| 252 | + const result = renderHook( |
| 253 | + (props: {}) => |
| 254 | + useKubeObjectList({ |
| 255 | + kubeObjectClass: mockClass, |
| 256 | + requests: [{ cluster: 'default', namespaces: ['a', 'b'] }], |
| 257 | + ...props, |
| 258 | + }), |
| 259 | + { |
| 260 | + wrapper: ({ children }) => ( |
| 261 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 262 | + ), |
| 263 | + } |
| 264 | + ); |
| 265 | + |
| 266 | + result.rerender({ requests: [{ cluster: 'default', namespaces: ['a'] }] }); |
| 267 | + |
| 268 | + expect(spy.mock.calls[0][0].connections.length).toBe(0); // initial render |
| 269 | + expect(spy.mock.calls[1][0].connections.length).toBe(2); // new connections with 'a' and 'b' namespaces |
| 270 | + expect(spy.mock.calls[2][0].connections.length).toBe(2); // rerender with new props |
| 271 | + expect(spy.mock.calls[3][0].connections.length).toBe(1); // updated connections after we removed namespace 'b' |
| 272 | + }); |
| 273 | +}); |
0 commit comments