|
| 1 | +import { configureStore } from '@reduxjs/toolkit'; |
| 2 | +import { Meta, StoryFn } from '@storybook/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { Cluster } from '../../lib/k8s/cluster'; |
| 5 | +import { initialState } from '../../redux/configSlice'; |
| 6 | +import { TestContext } from '../../test'; |
| 7 | +import Chooser from './Chooser'; |
| 8 | + |
| 9 | +const ourState = (clusters?: Cluster[]) => ({ |
| 10 | + config: { |
| 11 | + ...initialState, |
| 12 | + clusters: clusters || [ |
| 13 | + { |
| 14 | + name: 'cluster1', |
| 15 | + auth_type: '', |
| 16 | + }, |
| 17 | + { |
| 18 | + name: 'cluster2', |
| 19 | + auth_type: '', |
| 20 | + }, |
| 21 | + { |
| 22 | + name: 'cluster3', |
| 23 | + auth_type: '', |
| 24 | + }, |
| 25 | + ], |
| 26 | + allClusters: |
| 27 | + clusters === null |
| 28 | + ? null |
| 29 | + : clusters?.reduce((acc, cluster) => { |
| 30 | + acc[cluster.name] = cluster; |
| 31 | + return acc; |
| 32 | + }, {} as Record<string, Cluster>) || null, |
| 33 | + }, |
| 34 | + filter: { |
| 35 | + search: '', |
| 36 | + }, |
| 37 | + plugins: { |
| 38 | + loaded: true, |
| 39 | + }, |
| 40 | + ui: { |
| 41 | + clusterChooserButtonComponent: null, |
| 42 | + }, |
| 43 | + theme: { |
| 44 | + logo: null, |
| 45 | + name: 'light', |
| 46 | + }, |
| 47 | +}); |
| 48 | + |
| 49 | +export default { |
| 50 | + title: 'cluster/Chooser', |
| 51 | + component: Chooser, |
| 52 | + parameters: { |
| 53 | + docs: { |
| 54 | + description: { |
| 55 | + component: |
| 56 | + 'A dialog component for choosing a cluster. Can be controlled via open/onClose props.', |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + argTypes: { |
| 61 | + open: { |
| 62 | + control: 'boolean', |
| 63 | + description: 'Controls whether the dialog is open', |
| 64 | + }, |
| 65 | + onClose: { |
| 66 | + action: 'closed', |
| 67 | + description: 'Callback fired when the dialog requests to be closed', |
| 68 | + }, |
| 69 | + }, |
| 70 | +} as Meta; |
| 71 | + |
| 72 | +const Template: StoryFn = args => { |
| 73 | + const { clusters, ...otherProps } = args; |
| 74 | + |
| 75 | + // Clear recent clusters for clean state |
| 76 | + localStorage.setItem('recent_clusters', '[]'); |
| 77 | + |
| 78 | + return ( |
| 79 | + <TestContext |
| 80 | + store={configureStore({ |
| 81 | + reducer: (state = ourState()) => state, |
| 82 | + preloadedState: ourState(clusters), |
| 83 | + })} |
| 84 | + urlPrefix="/c" |
| 85 | + > |
| 86 | + <Chooser {...otherProps} /> |
| 87 | + </TestContext> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export const Normal = Template.bind({}); |
| 92 | +Normal.args = { |
| 93 | + clusters: ourState().config.clusters, |
| 94 | + open: true, |
| 95 | +}; |
| 96 | + |
| 97 | +export const SingleCluster = Template.bind({}); |
| 98 | +SingleCluster.args = { |
| 99 | + clusters: [ |
| 100 | + { |
| 101 | + name: 'only-cluster', |
| 102 | + auth_type: '', |
| 103 | + }, |
| 104 | + ], |
| 105 | + open: true, |
| 106 | +}; |
| 107 | + |
| 108 | +export const ManyClusters = Template.bind({}); |
| 109 | +ManyClusters.args = { |
| 110 | + clusters: Array(10) |
| 111 | + .fill(0) |
| 112 | + .map((_, i) => ({ |
| 113 | + name: `cluster-${i + 1}`, |
| 114 | + auth_type: '', |
| 115 | + })), |
| 116 | + open: true, |
| 117 | +}; |
| 118 | + |
| 119 | +export const NoClusters = Template.bind({}); |
| 120 | +NoClusters.args = { |
| 121 | + clusters: [], |
| 122 | + open: true, |
| 123 | +}; |
| 124 | + |
| 125 | +export const Closed = Template.bind({}); |
| 126 | +Closed.args = { |
| 127 | + clusters: ourState().config.clusters, |
| 128 | + open: false, |
| 129 | +}; |
0 commit comments