|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import uiSlice, { uiSlice as uiSliceObject } from './uiSlice'; |
| 19 | + |
| 20 | +const initialState = { |
| 21 | + isVersionDialogOpen: false, |
| 22 | + hideAppBar: false, |
| 23 | + isFullWidth: false, |
| 24 | + functionsToOverride: {}, |
| 25 | + panels: [], |
| 26 | +}; |
| 27 | + |
| 28 | +describe('uiSlice', () => { |
| 29 | + it('should handle initial state', () => { |
| 30 | + expect(uiSlice(undefined, { type: '' })).toEqual(initialState); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should set hideAppBar', () => { |
| 34 | + const nextState = uiSlice(initialState, uiSliceObject.actions.setHideAppBar(true)); |
| 35 | + expect(nextState.hideAppBar).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should set isVersionDialogOpen', () => { |
| 39 | + const nextState = uiSlice(initialState, uiSliceObject.actions.setVersionDialogOpen(true)); |
| 40 | + expect(nextState.isVersionDialogOpen).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should set isFullWidth', () => { |
| 44 | + const nextState = uiSlice(initialState, uiSliceObject.actions.setIsFullWidth(true)); |
| 45 | + expect(nextState.isFullWidth).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should set clusterChooserButtonComponent', () => { |
| 49 | + const testComponent = () => 'test component'; |
| 50 | + const nextState = uiSlice( |
| 51 | + initialState, |
| 52 | + uiSliceObject.actions.setClusterChooserButton(testComponent) |
| 53 | + ); |
| 54 | + expect(nextState.clusterChooserButtonComponent).toBe(testComponent); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should set functionsToOverride', () => { |
| 58 | + const testSetToken = () => {}; |
| 59 | + const testGetToken = () => 'test-token'; |
| 60 | + |
| 61 | + const nextState = uiSlice( |
| 62 | + initialState, |
| 63 | + uiSliceObject.actions.setFunctionsToOverride({ |
| 64 | + setToken: testSetToken, |
| 65 | + getToken: testGetToken, |
| 66 | + }) |
| 67 | + ); |
| 68 | + |
| 69 | + expect(nextState.functionsToOverride.setToken).toBe(testSetToken); |
| 70 | + expect(nextState.functionsToOverride.getToken).toBe(testGetToken); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should add UI panel', () => { |
| 74 | + const testComponent = () => React.createElement('div'); |
| 75 | + const testPanel = { |
| 76 | + id: 'test-panel', |
| 77 | + side: 'left' as const, |
| 78 | + component: testComponent, |
| 79 | + }; |
| 80 | + |
| 81 | + const nextState = uiSlice(initialState, uiSliceObject.actions.addUIPanel(testPanel)); |
| 82 | + |
| 83 | + expect(nextState.panels.length).toBe(1); |
| 84 | + expect(nextState.panels[0]).toEqual(testPanel); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should replace UI panel with same id', () => { |
| 88 | + const testComponent1 = () => React.createElement('div'); |
| 89 | + const testPanel1 = { |
| 90 | + id: 'test-panel', |
| 91 | + side: 'left' as const, |
| 92 | + component: testComponent1, |
| 93 | + }; |
| 94 | + |
| 95 | + const stateWithPanel = uiSlice(initialState, uiSliceObject.actions.addUIPanel(testPanel1)); |
| 96 | + |
| 97 | + const testComponent2 = () => React.createElement('span'); |
| 98 | + const testPanel2 = { |
| 99 | + id: 'test-panel', |
| 100 | + side: 'right' as const, |
| 101 | + component: testComponent2, |
| 102 | + }; |
| 103 | + |
| 104 | + const finalState = uiSlice(stateWithPanel, uiSliceObject.actions.addUIPanel(testPanel2)); |
| 105 | + |
| 106 | + expect(finalState.panels.length).toBe(1); |
| 107 | + expect(finalState.panels[0]).toEqual(testPanel2); |
| 108 | + }); |
| 109 | +}); |
0 commit comments