-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2/storage): add tests for CsvGenerator classes (#3975)
* ref: move CsvGenerator and EncryptedResponseCsvGenerator to own dir in preparation for colocation of tests * feat(CsvGenerator): add tests * feat(EncryptedResponseCsvGenerator): add tests
- Loading branch information
Showing
9 changed files
with
891 additions
and
10 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
...eatures/admin-form/responses/ResponsesPage/common/utils/CsvGenerator/CsvGenerator.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,184 @@ | ||
import { stringify } from 'csv-string' | ||
import FileSaver from 'file-saver' | ||
import { mocked } from 'ts-jest/utils' | ||
|
||
import { CsvGenerator } from './CsvGenerator' | ||
|
||
const UTF8_BYTE_ORDER_MARK = '\uFEFF' | ||
|
||
jest.mock('file-saver') | ||
const MockFileSaver = mocked(FileSaver) | ||
|
||
describe('CsvGenerator', () => { | ||
afterAll(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('Constructor', () => { | ||
it('should initialise the instance properties correctly with defaults if params are not passed to constructor', () => { | ||
// Arrange | ||
const csv = new CsvGenerator() | ||
|
||
// Assert | ||
expect(csv.expectedNumberOfRecords).toEqual(0) | ||
expect(csv.numOfMetaDataRows).toEqual(0) | ||
expect(csv.numOfHeaderRows).toEqual(1) | ||
expect(csv.lastCreatedAt).toEqual(0) | ||
expect(csv.startIdx).toEqual(2) | ||
expect(csv.idx).toEqual(2) | ||
expect(csv.records.length).toEqual(2) | ||
expect(csv.records[0]).toEqual(UTF8_BYTE_ORDER_MARK) | ||
}) | ||
|
||
it('should initialise the instance properties correctly if params are passed to constructor', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 2 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
|
||
// Assert | ||
expect(csv.expectedNumberOfRecords).toEqual(expectedNumberOfRecords) | ||
expect(csv.numOfMetaDataRows).toEqual(numOfMetaDataRows) | ||
expect(csv.numOfHeaderRows).toEqual(1) | ||
expect(csv.lastCreatedAt).toEqual(0) | ||
expect(csv.startIdx).toEqual(numOfMetaDataRows + 2) | ||
expect(csv.idx).toEqual(numOfMetaDataRows + 2) | ||
expect(csv.records.length).toEqual( | ||
expectedNumberOfRecords + numOfMetaDataRows + 2, | ||
) | ||
expect(csv.records[0]).toEqual(UTF8_BYTE_ORDER_MARK) | ||
}) | ||
}) | ||
|
||
describe('addLine', () => { | ||
it('should insert raw data of type string correctly', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 0 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const lineToAdd = ['a', 'b'] | ||
const stringifiedData = stringify(lineToAdd) | ||
|
||
// Act | ||
csv.addLine(lineToAdd) | ||
|
||
// Assert | ||
expect(csv.idx).toEqual(numOfMetaDataRows + 3) // Add 3 because line added | ||
expect(csv.records.length).toEqual( | ||
expectedNumberOfRecords + numOfMetaDataRows + 2, | ||
) | ||
expect(csv.records[2]).toEqual(stringifiedData) | ||
}) | ||
|
||
it('should insert raw data of type number correctly', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 0 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const lineToAdd = [1, 2] | ||
const stringifiedData = stringify(lineToAdd) | ||
|
||
// Act | ||
csv.addLine(lineToAdd) | ||
|
||
// Assert | ||
expect(csv.idx).toEqual(numOfMetaDataRows + 3) // Add 3 because line added | ||
expect(csv.records.length).toEqual(numOfMetaDataRows + 3) | ||
expect(csv.records[2]).toEqual(stringifiedData) | ||
}) | ||
}) | ||
|
||
describe('setHeader', () => { | ||
it('should set headers correctly', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 0 | ||
const numOfMetaDataRows = 0 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const headers = ['field1', 'field2'] | ||
const stringifiedHeaders = stringify(headers) | ||
|
||
// Act | ||
csv.setHeader(headers) | ||
|
||
// Assert | ||
expect(csv.records[1]).toEqual(stringifiedHeaders) | ||
}) | ||
}) | ||
|
||
describe('addMetaData', () => { | ||
it('should set metaData correctly for both string and numbers', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 3 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const metaData = [ | ||
['metaData1a', 'metaData1b'], | ||
[1, 2], | ||
['metaData3a', 3], | ||
] | ||
const stringifiedMetaData = metaData.map((data) => stringify(data)) | ||
|
||
// Act | ||
csv.addMetaData(metaData) | ||
|
||
// Assert | ||
expect(csv.records[1]).toEqual(stringifiedMetaData[0]) | ||
expect(csv.records[2]).toEqual(stringifiedMetaData[1]) | ||
expect(csv.records[3]).toEqual(stringifiedMetaData[2]) | ||
}) | ||
}) | ||
|
||
describe('triggerFileDownload', () => { | ||
it('should call triggerFileDownload with the correct parameters', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 0 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const lineToAdd = [1, 2] | ||
const blob = new Blob(csv.records, { | ||
type: 'text/csv;charset=utf-8', | ||
}) | ||
|
||
// Act | ||
csv.addLine(lineToAdd) | ||
csv.triggerFileDownload('some filename') | ||
|
||
// Assert | ||
expect(MockFileSaver.saveAs).toHaveBeenCalledWith(blob, 'some filename') | ||
}) | ||
}) | ||
|
||
describe('length', () => { | ||
it('should corrently return length of csv file when header and meta data are empty', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 0 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const lineToAdd = [1, 2] | ||
|
||
// Act | ||
csv.addLine(lineToAdd) | ||
|
||
// Assert | ||
expect(csv.length()).toEqual(expectedNumberOfRecords) | ||
}) | ||
|
||
it('should corrently return length of csv file when header and meta data are present', () => { | ||
// Arrange | ||
const expectedNumberOfRecords = 1 | ||
const numOfMetaDataRows = 1 | ||
const csv = new CsvGenerator(expectedNumberOfRecords, numOfMetaDataRows) | ||
const lineToAdd = [1, 2] | ||
const metaData = [['metaData1a', 'metaData1b']] | ||
const headers = ['field1', 'field2'] | ||
|
||
// Act | ||
csv.addLine(lineToAdd) | ||
csv.setHeader(headers) | ||
csv.addMetaData(metaData) | ||
|
||
// Assert | ||
expect(csv.length()).toEqual(expectedNumberOfRecords) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
frontend/src/features/admin-form/responses/ResponsesPage/common/utils/CsvGenerator/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 @@ | ||
export { CsvGenerator } from './CsvGenerator' |
2 changes: 1 addition & 1 deletion
2
frontend/src/features/admin-form/responses/ResponsesPage/common/utils/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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './CsvGenerator.class' | ||
export * from './CsvGenerator' |
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
Oops, something went wrong.