svg-table 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ import {
11
11
  defaultColumnStyle,
12
12
  defaultDimension,
13
13
  defaultFooterStyle,
14
- defaultRowHeaderStyle,
14
+ defaultRowHeaderStyle, defaultRowStyle,
15
15
  defaultTableFont,
16
16
  defaultTableMargin,
17
17
  defaultTablePadding,
@@ -32,9 +32,10 @@ describe('styling data tables', () => {
32
32
  [dateTimeFor(2, 2), 23456, 'gnm-g234', 23.45, 5],
33
33
  [dateTimeFor(3, 3), 34567, 'gnm-h234', 3.65, 40],
34
34
  [dateTimeFor(4, 4), 45678, 'gnm-i234', 314.15, 9],
35
+ [dateTimeFor(5, 5), 56789, 'gnm-j234', 618.3, 10],
35
36
  ]).getOrThrow()
36
37
  const columnHeader = ['Date-Time', 'Customer ID', 'Product ID', 'Purchase Price', 'Amount']
37
- const rowHeader = [1, 2, 3, 4]
38
+ const rowHeader = [1, 2, 3, 4, 5]
38
39
  const footer = ['A', 'B', 'C', 'D', 'E']
39
40
 
40
41
  describe('adding basic table styles', () => {
@@ -94,6 +95,23 @@ describe('styling data tables', () => {
94
95
  test('should be able to retrieve the margin', () => {
95
96
  expect(styledTable.tableMargin()).toEqual({...defaultTableMargin, left: 10, right: 10})
96
97
  })
98
+
99
+ test('should be able to get a copy of the data-frame from the styler', () => {
100
+ const dataFrame = styledTable.data()
101
+ expect(dataFrame.equals(formattedTableData.unwrapDataFrame())).toBeTruthy()
102
+ })
103
+
104
+ test('should be able to report whether styled table has a row header', () => {
105
+ expect(styledTable.hasRowHeader()).toBeTruthy()
106
+ })
107
+
108
+ test('should be able to report whether styled table has a column header', () => {
109
+ expect(styledTable.hasColumnHeader()).toBeTruthy()
110
+ })
111
+
112
+ test('should be able to report whether styled table has a footer', () => {
113
+ expect(styledTable.hasFooter()).toBeTruthy()
114
+ })
97
115
  })
98
116
 
99
117
  test('should be able to set and retrieve style for the column header', () => {
@@ -183,26 +201,26 @@ describe('styling data tables', () => {
183
201
  test('should return a failure when getting a style for a column index that is too large', () => {
184
202
  const result = styledTable.stylesForTableCoordinates(1, 6)
185
203
  expect(result.failed).toBeTruthy()
186
- expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 1; column_index: 6; valid_row_index: [0, 6); valid_column_index: [0, 6)")
204
+ expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 1; column_index: 6; valid_row_index: [0, 7); valid_column_index: [0, 6)")
187
205
  })
188
206
 
189
207
  test('should return a failure when getting a style for a column index that is less than 0', () => {
190
208
  const result = styledTable.stylesForTableCoordinates(3, -1)
191
209
  expect(result.failed).toBeTruthy()
192
- expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 3; column_index: -1; valid_row_index: [0, 6); valid_column_index: [0, 6)")
210
+ expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 3; column_index: -1; valid_row_index: [0, 7); valid_column_index: [0, 6)")
193
211
 
194
212
  })
195
213
 
196
214
  test('should return a failure when getting a style for a row index that is less than 0', () => {
197
215
  const result = styledTable.stylesForTableCoordinates(-1, 3)
198
216
  expect(result.failed).toBeTruthy()
199
- expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: -1; column_index: 3; valid_row_index: [0, 6); valid_column_index: [0, 6)")
217
+ expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: -1; column_index: 3; valid_row_index: [0, 7); valid_column_index: [0, 6)")
200
218
  })
201
219
 
202
220
  test('should return a failure when getting a style for a row index that is too large', () => {
203
- const result = styledTable.stylesForTableCoordinates(6, 3)
221
+ const result = styledTable.stylesForTableCoordinates(7, 3)
204
222
  expect(result.failed).toBeTruthy()
205
- expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 6; column_index: 3; valid_row_index: [0, 6); valid_column_index: [0, 6)")
223
+ expect(result.error).toEqual("(StyledTable::stylesFor) Invalid row and/or column index; row_index: 7; column_index: 3; valid_row_index: [0, 7); valid_column_index: [0, 6)")
206
224
  })
207
225
 
208
226
  test('should get the row header style for (1, 0) because table has row header style', () => {
@@ -264,5 +282,110 @@ describe('styling data tables', () => {
264
282
  } as CellStyle)
265
283
  })
266
284
  })
285
+
286
+ describe('set styles for multiple rows, columns, and cells', () => {
287
+
288
+ function expectDefaultCellStyleFor(styledTable: StyledTable<string>, row: number, column: number) {
289
+ expect(styledTable.stylesForTableCoordinates(row, column).getOrThrow()).toEqual(defaultCellStyle)
290
+ }
291
+
292
+ test('should be able to set the style for multiple columns at once', () => {
293
+ const styledColumns = [1, 3, 4]
294
+ const styledTable: StyledTable<string> = TableStyler.fromTableData(formattedTableData)
295
+ .withColumnStyles(styledColumns, {padding: {left: 1000, right: 1111}}, 75)
296
+ .styleTable()
297
+ const unstyledRows = [1, 3, 4, 5]
298
+ const unstyledColumns = [0, 2, 5]
299
+ for (const row of unstyledRows) {
300
+ for (const column of unstyledColumns) {
301
+ expectDefaultCellStyleFor(styledTable, row, column)
302
+ }
303
+ }
304
+
305
+ for (const column of styledColumns) {
306
+ expect(styledTable.columnStyleFor(column).map(styling => styling.style).getOrThrow())
307
+ .toEqual({...defaultColumnStyle, padding: {left: 1000, right: 1111}})
308
+ }
309
+ })
310
+
311
+ test('should be able to set the style for all columns at once', () => {
312
+ const styledColumns = [0, 1, 2, 3, 4, 5]
313
+ const styledTable: StyledTable<string> = TableStyler.fromTableData(formattedTableData)
314
+ // an empty array means that all the columns should be styled
315
+ .withColumnStyles([], {padding: {left: 1000, right: 1111}}, 75)
316
+ .styleTable()
317
+
318
+ for (const column of styledColumns) {
319
+ expect(styledTable.columnStyleFor(column).map(styling => styling.style).getOrThrow())
320
+ .toEqual({...defaultColumnStyle, padding: {left: 1000, right: 1111}})
321
+ }
322
+ })
323
+
324
+ test('should be able to set the style for multiple rows at once', () => {
325
+ const styledRows = [1, 3, 4]
326
+ const styledTable: StyledTable<string> = TableStyler.fromTableData(formattedTableData)
327
+ .withRowStyles(styledRows, {padding: {top: 1000, bottom: 1111}}, 75)
328
+ .styleTable()
329
+ const unstyledRows = [0, 2]
330
+ const unstyledColumns = [0, 1, 2, 3, 4, 5]
331
+ for (const row of unstyledRows) {
332
+ for (const column of unstyledColumns) {
333
+ expectDefaultCellStyleFor(styledTable, row, column)
334
+ }
335
+ }
336
+
337
+ for (const row of styledRows) {
338
+ expect(styledTable.rowStyleFor(row).map(styling => styling.style).getOrThrow())
339
+ .toEqual({...defaultRowStyle, padding: {top: 1000, bottom: 1111}})
340
+ }
341
+ })
342
+
343
+ test('should be able to set the style for all rows at once', () => {
344
+ const styledRows = [0, 1, 2, 3, 4]
345
+ const styledTable: StyledTable<string> = TableStyler.fromTableData(formattedTableData)
346
+ // an empty array means that all the columns should be styled
347
+ .withRowStyles([], {padding: {top: 1000, bottom: 1111}}, 75)
348
+ .styleTable()
349
+
350
+ for (const row of styledRows) {
351
+ expect(styledTable.rowStyleFor(row).map(styling => styling.style).getOrThrow())
352
+ .toEqual({...defaultRowStyle, padding: {top: 1000, bottom: 1111}})
353
+ }
354
+ })
355
+ })
356
+
357
+ describe('conditionally set styles for cells', () => {
358
+ function expectDefaultCellStyleFor(styledTable: StyledTable<string>, row: number, column: number) {
359
+ expect(styledTable.stylesForTableCoordinates(row, column).getOrThrow()).toEqual(defaultCellStyle)
360
+ }
361
+
362
+ test('should be able to set the style for multiple columns at once', () => {
363
+ // const styledTable: StyledTable<string> = TableStyler.fromTableData(formattedTableData)
364
+ const styledTable: StyledTable<string> = TableStyler.fromDataFrame(formattedTableData.unwrapDataFrame())
365
+ .withCellStyleWhen(
366
+ (value, row, column) => parseInt(value) >= 45678 && column === 2,
367
+ {padding: {...defaultTablePadding, left: 1000, right: 1111}},
368
+ 75
369
+ )
370
+ .styleTable()
371
+ const unstyledColumns = [0, 1, 3, 4]
372
+ const unstyledRows = [0, 1, 2, 3, 4, 5]
373
+ for (const row of unstyledRows) {
374
+ for (const column of unstyledColumns) {
375
+ expectDefaultCellStyleFor(styledTable, row, column)
376
+ }
377
+ }
378
+ expectDefaultCellStyleFor(styledTable, 0, 2)
379
+ expectDefaultCellStyleFor(styledTable, 1, 2)
380
+ expectDefaultCellStyleFor(styledTable, 2, 2)
381
+ expectDefaultCellStyleFor(styledTable, 3, 2)
382
+
383
+ for (const row of [4, 5]) {
384
+ expect(styledTable.cellStyleFor(row, 2).map(styling => styling.style).getOrThrow())
385
+ .toEqual({...defaultCellStyle, padding: {...defaultTablePadding, left: 1000, right: 1111}})
386
+ }
387
+ })
388
+
389
+ })
267
390
  })
268
391
  })