-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(EMS-3539): improve xlsx styled column functions (#3251)
* chore(EMS-3539): improve xlsx styled column functions * chore(EMS-3539): improve xlsx styled column functions * chore(EMS-3539): improve xlsx generation functions * chore(EMS-3539): add missing unit test * chore(EMS-3539): improve documentation * chore(EMS-3539): fix/update unit test * chore(EMS-3539): fix typo * chore(tests): fix date related unit test
- Loading branch information
Showing
15 changed files
with
409 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/api/constants/XLSX-CONFIG/INDEXES/APPLICATION_INFORMATION/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import APPLICATION_INFORMATION_INDEXES from '.'; | ||
|
||
describe('api/constants/XLSX-CONFIG/INDEXES/APPLICATION_INFORMATION', () => { | ||
it('should return an object with indexes', () => { | ||
const expected = { | ||
EXPORTER_CONTACT_DETAILS: 8, | ||
KEY_INFORMATION: 13, | ||
}; | ||
|
||
expect(APPLICATION_INFORMATION_INDEXES).toEqual(expected); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/api/constants/XLSX-CONFIG/INDEXES/APPLICATION_INFORMATION/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* APPLICATION_INFORMATION | ||
* Default indexes for the "Application information" XLSX worksheet. | ||
*/ | ||
const APPLICATION_INFORMATION_INDEXES = { | ||
EXPORTER_CONTACT_DETAILS: 8, | ||
KEY_INFORMATION: 13, | ||
}; | ||
|
||
export default APPLICATION_INFORMATION_INDEXES; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,46 @@ | ||
import ExcelJS from 'exceljs'; | ||
import styledColumns, { worksheetRowHeights } from '.'; | ||
import { XLSX_CONFIG } from '../../constants'; | ||
import styledColumns, { getAdditionalRowHeightIndexes } from '.'; | ||
import XLSX_ROW_INDEXES from '../../constants/XLSX-CONFIG/INDEXES'; | ||
import modifyRowStyles from './modify-row-styles'; | ||
import modifyRowHeights from './modify-row-heights'; | ||
import { mockApplicationMinimalBrokerBuyerAndCompany as mockApplication } from '../../test-mocks'; | ||
|
||
const { LARGE_ADDITIONAL_COLUMN_HEIGHT, ADDITIONAL_TITLE_COLUMN_HEIGHT, FONT_SIZE } = XLSX_CONFIG; | ||
import createMockWorksheet from '../../test-mocks/create-mock-worksheet'; | ||
|
||
describe('api/generate-xlsx/styled-columns/index', () => { | ||
const mockSheetName = 'mock sheet name'; | ||
const { mockWorksheet, mockSheetName } = createMockWorksheet(); | ||
|
||
const workbook = new ExcelJS.Workbook(); | ||
describe('getAdditionalRowHeightIndexes', () => { | ||
describe('when a provided sheetName is in XLSX_ROW_INDEXES', () => { | ||
it('should return index values', () => { | ||
const result = getAdditionalRowHeightIndexes(mockApplication, mockSheetName); | ||
|
||
const worksheet = workbook.addWorksheet(mockSheetName); | ||
const sheetIndexes = XLSX_ROW_INDEXES[mockSheetName](mockApplication); | ||
|
||
describe('worksheetRowHeights', () => { | ||
it('should add column heights to particular columns', async () => { | ||
const mockIndexes = [5, 6]; | ||
const expected = Object.values(sheetIndexes); | ||
|
||
const result = worksheetRowHeights(mockIndexes, worksheet); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
|
||
result.eachRow((row, rowNumber) => { | ||
if (rowNumber === 1) { | ||
expect(row.height).toEqual(ADDITIONAL_TITLE_COLUMN_HEIGHT); | ||
} else if (mockIndexes.includes(rowNumber)) { | ||
expect(row.height).toEqual(LARGE_ADDITIONAL_COLUMN_HEIGHT); | ||
} | ||
describe('when a provided sheetName is NOT in XLSX_ROW_INDEXES', () => { | ||
it('should return an empty array', () => { | ||
const result = getAdditionalRowHeightIndexes(mockApplication, 'invalid sheet name'); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('styledColumns', () => { | ||
it('should add custom `alignment` and font size properties to each column', async () => { | ||
const result = styledColumns(mockApplication, worksheet, mockSheetName); | ||
|
||
result.eachRow((row, rowNumber) => { | ||
const isHeaderRow = rowNumber === 1; | ||
|
||
if (isHeaderRow) { | ||
row.eachCell((cell, colNumber) => { | ||
const cellData = row.getCell(colNumber); | ||
|
||
expect(cellData.font.bold).toEqual(true); | ||
expect(cellData.font.size).toEqual(FONT_SIZE.TITLE); | ||
}); | ||
} else { | ||
row.eachCell((cell, colNumber) => { | ||
const cellData = row.getCell(colNumber); | ||
|
||
expect(cellData.font.bold).toEqual(false); | ||
expect(cellData.font.size).toEqual(FONT_SIZE.DEFAULT); | ||
}); | ||
} | ||
}); | ||
it('should return a modified worksheet', async () => { | ||
const result = styledColumns(mockApplication, mockWorksheet, mockSheetName); | ||
|
||
const modifiedRowStyles = modifyRowStyles(mockWorksheet, mockSheetName); | ||
|
||
const indexes = getAdditionalRowHeightIndexes(mockApplication, mockSheetName); | ||
|
||
const expected = modifyRowHeights(indexes, modifiedRowStyles, mockSheetName); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/api/generate-xlsx/styled-columns/is-title-row/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import isTitleRow from '.'; | ||
import SECTION_NAMES from '../../../constants/XLSX-CONFIG/SECTION_NAMES'; | ||
import APPLICATION_INFORMATION_INDEXES from '../../../constants/XLSX-CONFIG/INDEXES/APPLICATION_INFORMATION'; | ||
|
||
const { APPLICATION_INFORMATION } = SECTION_NAMES; | ||
const { EXPORTER_CONTACT_DETAILS, KEY_INFORMATION } = APPLICATION_INFORMATION_INDEXES; | ||
|
||
describe('api/generate-xlsx/styled-columns/is-title-row', () => { | ||
describe(`when the sheetName is ${APPLICATION_INFORMATION}`, () => { | ||
const mockSheetName = APPLICATION_INFORMATION; | ||
|
||
describe(`when rowNumber=${EXPORTER_CONTACT_DETAILS}`, () => { | ||
it('should return true', () => { | ||
const result = isTitleRow(mockSheetName, EXPORTER_CONTACT_DETAILS); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe(`when rowNumber=${KEY_INFORMATION}`, () => { | ||
it('should return true', () => { | ||
const result = isTitleRow(mockSheetName, KEY_INFORMATION); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when rowNumber is NOT 8 or 13', () => { | ||
it('should return false', () => { | ||
const result = isTitleRow(mockSheetName, 5); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`when the sheetName is NOT ${APPLICATION_INFORMATION}`, () => { | ||
const mockSheetName = 'Mock sheet name'; | ||
|
||
describe('when rowNumber=1', () => { | ||
it('should return true', () => { | ||
const result = isTitleRow(mockSheetName, 1); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when rowNumber is NOT 1', () => { | ||
it('should return false', () => { | ||
const result = isTitleRow(mockSheetName, 5); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.