@@ -5,50 +5,76 @@ import { Provider } from 'react-redux';
55import { configureStore } from '@reduxjs/toolkit' ;
66import rootReducer from 'src/redux/reducers/rootReducer' ;
77import { within , expect } from 'storybook/test' ;
8- import i18next from 'i18next ' ;
8+ import { getTranslatedText } from 'helpers/testTranslationHelper ' ;
99
1010const createMockDocumentViewer = ( getSelectedCellRange , cells ) => ( ) => ( {
1111 getSpreadsheetEditorManager : ( ) => ( {
1212 getSelectedCellRange,
1313 getSelectedCells : ( ) => cells ,
1414 } ) ,
1515} ) ;
16- const store = configureStore ( { reducer : rootReducer } ) ;
1716const originalCoreGetDocumentViewer = core . getDocumentViewer ;
1817
19- export default {
20- title : 'ModularComponents/CellBorders' ,
21- component : CellBorders ,
22- decorators : [
23- ( Story ) => {
24- core . getDocumentViewer = originalCoreGetDocumentViewer ;
25- return (
26- < Provider store = { store } >
27- < Story />
28- </ Provider >
29- ) ;
30- } ,
31- ] ,
18+ const mockConfigs = {
19+ multipleCells : {
20+ range : ( ) => ( { firstRow : 0 , lastRow : 1 , firstColumn : 0 , lastColumn : 1 } ) ,
21+ cells : [
22+ { getStyle : ( ) => ( { getCellBorder : ( ) => ( { style : 'Thin' } ) } ) } ,
23+ { getStyle : ( ) => ( { getCellBorder : ( ) => ( { } ) } ) }
24+ ] ,
25+ isSingleCell : false
26+ } ,
27+ singleCell : {
28+ range : ( ) => ( { firstRow : 1 , lastRow : 1 , firstColumn : 1 , lastColumn : 1 } ) ,
29+ cells : [ {
30+ getStyle : ( ) => ( {
31+ getCellBorder : ( side ) => {
32+ return [ 'Right' , 'Left' ] . includes ( side )
33+ ? { style : 'Thin' }
34+ : { style : 'None' } ;
35+ }
36+ } )
37+ } ] ,
38+ isSingleCell : true
39+ } ,
40+ noBorders : {
41+ range : ( ) => ( { firstRow : 1 , lastRow : 1 , firstColumn : 1 , lastColumn : 1 } ) ,
42+ cells : [ {
43+ getStyle : ( ) => ( { getCellBorder : ( ) => ( { style : 'None' } ) } )
44+ } ] ,
45+ isSingleCell : true
46+ }
3247} ;
3348
34- const withMultipleCells = ( Story ) => {
35- core . getDocumentViewer = createMockDocumentViewer (
36- ( ) => ( { firstRow : 0 , lastRow : 1 , firstColumn : 0 , lastColumn : 1 } ) ,
37- [ 1 , 2 ]
38- ) ;
39- return < Story /> ;
49+ const createMockStore = ( isSingleCell ) => {
50+ return configureStore ( {
51+ reducer : rootReducer ,
52+ preloadedState : {
53+ spreadsheetEditor : {
54+ cellProperties : {
55+ isSingleCell,
56+ styles : { }
57+ }
58+ }
59+ }
60+ } ) ;
4061} ;
4162
42- const withSingleCell = ( Story ) => {
43- core . getDocumentViewer = createMockDocumentViewer (
44- ( ) => ( { firstRow : 1 , lastRow : 1 , firstColumn : 1 , lastColumn : 1 } ) ,
45- [ 1 ]
46- ) ;
47- return < Story /> ;
63+ const createMockDecorator = ( configKey ) => {
64+ const MockDecorator = ( Story ) => {
65+ const config = mockConfigs [ configKey ] ;
66+ core . getDocumentViewer = createMockDocumentViewer ( config . range , config . cells ) ;
67+ const mockStore = createMockStore ( config . isSingleCell ) ;
68+ return (
69+ < Provider store = { mockStore } >
70+ < Story />
71+ </ Provider >
72+ ) ;
73+ } ;
74+ MockDecorator . displayName = `MockDecorator_${ configKey } ` ;
75+ return MockDecorator ;
4876} ;
4977
50- const Template = ( args ) => < CellBorders { ...args } /> ;
51-
5278const expectedButtons = {
5379 'none' : 'alwaysShow' ,
5480 'all' : 'showForMultipleCells' ,
@@ -66,52 +92,72 @@ const checkButtonVisibility = async (canvas, expectedMapping) => {
6692 await Promise . all (
6793 Object . entries ( expectedMapping ) . map ( async ( [ name , state ] ) => {
6894 const button = canvas . queryByRole ( 'button' , {
69- name : i18next . t ( `spreadsheetEditor.${ name } ` ) ,
95+ name : getTranslatedText ( `spreadsheetEditor.${ name } ` ) ,
7096 } ) ;
71- if ( state === 'alwaysShow' ) {
72- expect ( button ) . toBeInTheDocument ( ) ;
73- } else {
74- expect ( button ) . toBeNull ( ) ;
75- }
97+ expect ( button ) [ state === 'alwaysShow' ? 'toBeInTheDocument' : 'toBeNull' ] ( ) ;
7698 } )
7799 ) ;
78100} ;
79101
80- export const FlyoutWithSingleCell = Template . bind ( { } ) ;
81- FlyoutWithSingleCell . args = {
82- isFlyoutItem : true ,
83- disabled : false ,
84- dataElement : 'cellBordersFlyoutSingle' ,
85- } ;
86- FlyoutWithSingleCell . decorators = [ withSingleCell ] ;
87- FlyoutWithSingleCell . play = async ( { canvasElement } ) => {
102+ const createPlayFunction = ( checkAll = false ) => async ( { canvasElement } ) => {
88103 const canvas = within ( canvasElement ) ;
89104 try {
90- await checkButtonVisibility ( canvas , expectedButtons ) ;
105+ if ( checkAll ) {
106+ await Promise . all (
107+ Object . keys ( expectedButtons ) . map ( async ( name ) => {
108+ const button = canvas . getByRole ( 'button' , {
109+ name : getTranslatedText ( `spreadsheetEditor.${ name } ` ) ,
110+ } ) ;
111+ expect ( button ) . toBeInTheDocument ( ) ;
112+ } )
113+ ) ;
114+ } else {
115+ await checkButtonVisibility ( canvas , expectedButtons ) ;
116+ }
91117 } finally {
92118 core . getDocumentViewer = originalCoreGetDocumentViewer ;
93119 }
94120} ;
95121
96- export const FlyoutWithAllButtons = Template . bind ( { } ) ;
97- FlyoutWithAllButtons . args = {
98- isFlyoutItem : true ,
99- disabled : false ,
100- dataElement : 'cellBordersFlyoutAll' ,
101- } ;
102- FlyoutWithAllButtons . decorators = [ withMultipleCells ] ;
103- FlyoutWithAllButtons . play = async ( { canvasElement } ) => {
104- const canvas = within ( canvasElement ) ;
105- try {
106- await Promise . all (
107- Object . keys ( expectedButtons ) . map ( async ( name ) => {
108- const button = canvas . getByRole ( 'button' , {
109- name : i18next . t ( `spreadsheetEditor.${ name } ` ) ,
110- } ) ;
111- expect ( button ) . toBeInTheDocument ( ) ;
112- } )
113- ) ;
114- } finally {
115- core . getDocumentViewer = originalCoreGetDocumentViewer ;
122+ const createStory = ( dataElement , mockConfig , checkAll = false ) => {
123+ const story = Template . bind ( { } ) ;
124+ story . args = {
125+ isFlyoutItem : true ,
126+ disabled : false ,
127+ dataElement,
128+ } ;
129+ story . decorators = [ createMockDecorator ( mockConfig ) ] ;
130+ story . play = createPlayFunction ( checkAll ) ;
131+ if ( ! checkAll ) {
132+ story . parameters = window . storybook . disableRtlMode ;
116133 }
117- } ;
134+ return story ;
135+ } ;
136+
137+ export default {
138+ title : 'ModularComponents/CellBorders' ,
139+ component : CellBorders ,
140+ decorators : [
141+ ( Story ) => {
142+ core . getDocumentViewer = originalCoreGetDocumentViewer ;
143+ return (
144+ < div >
145+ < style >
146+ { `
147+ .icon-grid .row .Button.active {
148+ box-shadow: inset 0 0 0 1px var(--blue-5);
149+ }
150+ ` }
151+ </ style >
152+ < Story />
153+ </ div >
154+ ) ;
155+ } ,
156+ ] ,
157+ } ;
158+
159+ const Template = ( args ) => < CellBorders { ...args } /> ;
160+
161+ export const FlyoutWithSingleCell = createStory ( 'cellBordersFlyoutSingle' , 'singleCell' ) ;
162+ export const FlyoutWithAllButtons = createStory ( 'cellBordersFlyoutAll' , 'multipleCells' , true ) ;
163+ export const FlyoutWithNoBorders = createStory ( 'cellBordersFlyoutNoBorders' , 'noBorders' ) ;
0 commit comments