11import React , { useState } from 'react'
22import type { Meta , StoryObj } from '@storybook/react-vite'
33import { action } from 'storybook/actions'
4+ import { expect , fn , screen } from 'storybook/test'
45
56import SentinelDatabases from './SentinelDatabases'
67import {
78 AddRedisDatabaseStatus ,
89 ModifiedSentinelMaster ,
910} from 'uiSrc/slices/interfaces'
1011import { RowSelectionState } from '@redis-ui/table'
11- import {
12- colFactory ,
13- getRowId ,
14- } from 'uiSrc/pages/autodiscover-sentinel/sentinel-databases/useSentinelDatabasesConfig'
12+ import { colFactory , getRowId } from '../../useSentinelDatabasesConfig'
1513
1614const meta : Meta < typeof SentinelDatabases > = {
1715 component : SentinelDatabases ,
16+ args : {
17+ onBack : fn ( ) ,
18+ onClose : fn ( ) ,
19+ onSubmit : fn ( ) ,
20+ } ,
1821}
1922
2023export default meta
@@ -74,9 +77,9 @@ const DefaultRender = () => {
7477 selection = { selection || [ ] }
7578 columns = { columnsMock }
7679 masters = { mastersMock }
77- onClose = { action ( ' onClose' ) }
78- onBack = { action ( ' onBack' ) }
79- onSubmit = { action ( ' onSubmit' ) }
80+ onClose = { meta . args ?. onClose ! }
81+ onBack = { meta . args ?. onBack ! }
82+ onSubmit = { meta . args ?. onSubmit ! }
8083 onSelectionChange = { ( sel ) => {
8184 setSelection ( sel )
8285 } }
@@ -86,6 +89,41 @@ const DefaultRender = () => {
8689
8790export const Default : Story = {
8891 render : ( ) => < DefaultRender /> ,
92+ play : async ( { canvas, userEvent, args, step } ) => {
93+ await step ( 'Ensure render' , async ( ) => {
94+ await expect ( canvas . getByTestId ( 'row-selection' ) ) . toBeInTheDocument ( )
95+ await expect ( canvas . getByText ( 'mymaster' ) ) . toBeInTheDocument ( )
96+ await expect ( canvas . getByText ( 'mymaster2' ) ) . toBeInTheDocument ( )
97+ await expect (
98+ canvas . getByText ( 'Auto-Discover Redis Sentinel Primary Groups' ) ,
99+ ) . toBeInTheDocument ( )
100+ await expect ( canvas . getByText ( 'Add databases' ) ) . toBeInTheDocument ( )
101+ } )
102+ // you can group interactions with `step`
103+ await step ( 'Ensure cancel is called' , async ( ) => {
104+ await userEvent . click ( canvas . getByRole ( 'button' , { name : 'Cancel' } ) )
105+ await userEvent . click ( screen . getByRole ( 'button' , { name : 'Proceed' } ) )
106+ await expect ( args . onClose ) . toHaveBeenCalled ( )
107+ } )
108+ // or you can just call actions sequentially
109+ // await userEvent.click(canvas.getByRole('button', { name: 'Cancel' }))
110+ // await userEvent.click(screen.getByRole('button', { name: 'Proceed' }))
111+ // await expect(args.onClose).toHaveBeenCalled()
112+ await step ( 'Back to add databases screen is called' , async ( ) => {
113+ await userEvent . click (
114+ canvas . getByRole ( 'button' , { name : 'Add databases' } ) ,
115+ )
116+ await expect ( args . onBack ) . toHaveBeenCalled ( )
117+ } )
118+ await step ( 'Primary group selection' , async ( ) => {
119+ await userEvent . click ( canvas . getByTestId ( 'row-selection-1' ) )
120+ await userEvent . click (
121+ // name can be exact matching string or regex
122+ canvas . getByRole ( 'button' , { name : / a d d p r i m a r y g r o u p / i } ) ,
123+ )
124+ await expect ( args . onSubmit ) . toHaveBeenCalled ( )
125+ } )
126+ } ,
89127}
90128const emptyColumnsMock = colFactory ( [ ] , ( ) => { } )
91129export const Empty : Story = {
@@ -98,4 +136,9 @@ export const Empty: Story = {
98136 onSubmit : action ( 'onSubmit' ) ,
99137 onSelectionChange : action ( 'on selection change' ) ,
100138 } ,
139+ play : async ( { canvas } ) => {
140+ await expect (
141+ canvas . getByText ( 'Your Redis Sentinel has no primary groups available' ) ,
142+ ) . toBeInTheDocument ( )
143+ } ,
101144}
0 commit comments