1- import { getContainer } from 'tests/util/helpers' ;
2- import { assembleDataCfg , assembleOptions } from 'tests/util' ;
1+ /* eslint-disable jest/expect-expect */
2+ import { createPivotSheet } from 'tests/util/helpers ' ;
33import { get } from 'lodash' ;
44import { ShapeAttrs } from '@antv/g-canvas' ;
5+ import { S2DataConfig } from './../../esm/common/interface/s2DataConfig.d' ;
6+ import { TextTheme } from '@/common/interface/theme' ;
57import { PivotSheet } from '@/sheet-type' ;
6- import { CellTypes , TextAlign } from '@/common' ;
8+ import { CellTypes , EXTRA_FIELD , TextAlign } from '@/common' ;
79import { RowCell } from '@/cell' ;
10+ import { Node } from '@/facet/layout/node' ;
811
912describe ( 'SpreadSheet Theme Tests' , ( ) => {
1013 let s2 : PivotSheet ;
11- const dataCfg = assembleDataCfg ( ) ;
1214
1315 beforeAll ( ( ) => {
14- s2 = new PivotSheet (
15- getContainer ( ) ,
16- dataCfg ,
17- assembleOptions ( {
16+ s2 = createPivotSheet (
17+ {
1818 headerActionIcons : [
1919 {
2020 iconNames : [ 'DrillDownIcon' ] ,
@@ -23,8 +23,11 @@ describe('SpreadSheet Theme Tests', () => {
2323 action : ( ) => { } ,
2424 } ,
2525 ] ,
26- } ) ,
26+ } ,
27+ { useSimpleData : false } ,
2728 ) ;
29+
30+ s2 . render ( ) ;
2831 } ) ;
2932
3033 afterAll ( ( ) => {
@@ -135,4 +138,195 @@ describe('SpreadSheet Theme Tests', () => {
135138 } ,
136139 ) ;
137140 } ) ;
141+
142+ describe ( 'Measure Fields Theme Tests' , ( ) => {
143+ const expectTextAlign = ( options : {
144+ textAlign : TextAlign ;
145+ fontWight : TextTheme [ 'fontWeight' ] ;
146+ containsDataCells ?: boolean ;
147+ customNodes ?: Node [ ] ;
148+ } ) => {
149+ const {
150+ textAlign,
151+ fontWight,
152+ containsDataCells = false ,
153+ customNodes,
154+ } = options ;
155+ const targetNodes = customNodes || s2 . getColumnLeafNodes ( ) ;
156+ const dataCells = s2 . interaction . getPanelGroupAllDataCells ( ) ;
157+
158+ expect ( targetNodes ) . not . toHaveLength ( 0 ) ;
159+
160+ if ( ! containsDataCells ) {
161+ expect (
162+ targetNodes . every ( ( node ) => {
163+ const nodeTextShape = node . belongsCell . getTextShape ( ) ;
164+ return (
165+ nodeTextShape . attr ( 'textAlign' ) === textAlign &&
166+ nodeTextShape . attr ( 'fontWeight' ) === fontWight
167+ ) ;
168+ } ) ,
169+ ) . toBeTruthy ( ) ;
170+ return ;
171+ }
172+
173+ targetNodes . forEach ( ( node ) => {
174+ const nodeTextShape = node . belongsCell . getTextShape ( ) ;
175+ const isEqualTextAlign = dataCells . every ( ( cell ) => {
176+ return (
177+ cell . getTextShape ( ) . attr ( 'textAlign' ) ===
178+ nodeTextShape . attr ( 'textAlign' )
179+ ) ;
180+ } ) ;
181+ expect ( isEqualTextAlign ) . toBeTruthy ( ) ;
182+ expect ( nodeTextShape . attr ( 'fontWeight' ) ) . toStrictEqual ( fontWight ) ;
183+ } ) ;
184+ expect ( dataCells [ 0 ] . getTextShape ( ) . attr ( 'textAlign' ) ) . toEqual ( textAlign ) ;
185+ } ;
186+
187+ it ( 'should default align column headers with data cells' , ( ) => {
188+ expectTextAlign ( {
189+ textAlign : 'right' ,
190+ fontWight : 'normal' ,
191+ containsDataCells : true ,
192+ } ) ;
193+ } ) ;
194+
195+ it ( 'should render normal font wight and left text align text with row cells' , ( ) => {
196+ s2 . setDataCfg ( {
197+ fields : {
198+ valueInCols : false ,
199+ } ,
200+ } as S2DataConfig ) ;
201+
202+ s2 . render ( ) ;
203+
204+ const rowMeasureFields = s2
205+ . getRowNodes ( )
206+ . filter ( ( node ) => node . field === EXTRA_FIELD ) ;
207+
208+ expectTextAlign ( {
209+ textAlign : 'left' ,
210+ fontWight : 'normal' ,
211+ containsDataCells : false ,
212+ customNodes : rowMeasureFields ,
213+ } ) ;
214+ } ) ;
215+
216+ it ( 'should render normal font wight and left text align text with col cell' , ( ) => {
217+ s2 . setDataCfg ( {
218+ fields : {
219+ valueInCols : true ,
220+ } ,
221+ } as S2DataConfig ) ;
222+
223+ s2 . render ( ) ;
224+
225+ const colMeasureFields = s2
226+ . getColumnNodes ( )
227+ . filter ( ( node ) => node . field === EXTRA_FIELD ) ;
228+
229+ expectTextAlign ( {
230+ textAlign : 'right' ,
231+ fontWight : 'normal' ,
232+ containsDataCells : false ,
233+ customNodes : colMeasureFields ,
234+ } ) ;
235+ } ) ;
236+
237+ it . each ( [ 'left' , 'center' , 'right' ] as TextAlign [ ] ) (
238+ 'should render %s text align for column nodes' ,
239+ ( textAlign ) => {
240+ s2 . setThemeCfg ( {
241+ theme : {
242+ colCell : {
243+ measureText : {
244+ textAlign,
245+ } ,
246+ } ,
247+ } ,
248+ } ) ;
249+
250+ s2 . render ( true ) ;
251+
252+ expectTextAlign ( { textAlign, fontWight : 'normal' } ) ;
253+ } ,
254+ ) ;
255+
256+ it . each ( [
257+ { isRowCell : true , textAlign : 'left' } ,
258+ { isRowCell : true , textAlign : 'center' } ,
259+ { isRowCell : true , textAlign : 'right' } ,
260+ { isRowCell : false , textAlign : 'left' } ,
261+ { isRowCell : false , textAlign : 'center' } ,
262+ { isRowCell : false , textAlign : 'right' } ,
263+ ] as Array < { isRowCell : boolean ; textAlign : TextAlign } > ) (
264+ 'should render %s text align for totals nodes' ,
265+ ( { isRowCell, textAlign } ) => {
266+ s2 . setOptions ( {
267+ totals : {
268+ col : {
269+ showGrandTotals : true ,
270+ showSubTotals : true ,
271+ reverseLayout : true ,
272+ reverseSubLayout : false ,
273+ } ,
274+ row : {
275+ showGrandTotals : true ,
276+ showSubTotals : true ,
277+ reverseLayout : true ,
278+ reverseSubLayout : false ,
279+ } ,
280+ } ,
281+ } ) ;
282+
283+ s2 . setThemeCfg ( {
284+ theme : {
285+ colCell : {
286+ // 小计/总计是加粗字体
287+ bolderText : {
288+ textAlign,
289+ } ,
290+ } ,
291+ rowCell : {
292+ bolderText : {
293+ textAlign,
294+ } ,
295+ } ,
296+ } ,
297+ } ) ;
298+
299+ s2 . render ( ) ;
300+
301+ const rowTotalNodes = s2 . getRowNodes ( ) . filter ( ( node ) => node . isTotals ) ;
302+
303+ const colTotalNodes = s2
304+ . getColumnNodes ( )
305+ . filter ( ( node ) => node . isTotals ) ;
306+
307+ expectTextAlign ( {
308+ textAlign,
309+ fontWight : 520 ,
310+ customNodes : isRowCell ? rowTotalNodes : colTotalNodes ,
311+ } ) ;
312+ } ,
313+ ) ;
314+
315+ it ( 'should not align column headers with data cells and render normal font wight leaf node text if hideMeasureColumn' , ( ) => {
316+ s2 . setOptions ( {
317+ style : {
318+ colCfg : {
319+ hideMeasureColumn : true ,
320+ } ,
321+ } ,
322+ totals : null ,
323+ } ) ;
324+ s2 . render ( ) ;
325+
326+ expectTextAlign ( {
327+ textAlign : 'center' ,
328+ fontWight : 'normal' ,
329+ } ) ;
330+ } ) ;
331+ } ) ;
138332} ) ;
0 commit comments