|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Test framework dependencies |
| 4 | +const Lab = require('@hapi/lab') |
| 5 | +const Code = require('@hapi/code') |
| 6 | + |
| 7 | +const { describe, it, beforeEach } = (exports.lab = Lab.script()) |
| 8 | +const { expect } = Code |
| 9 | + |
| 10 | +// Thing under test |
| 11 | +const { transformArrayToCSVRow } = require('../../app/lib/transform-to-csv.lib.js') |
| 12 | + |
| 13 | +describe('Transform to csv', () => { |
| 14 | + describe('#transformArrayToCSVRow', () => { |
| 15 | + let testArray |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + testArray = _testArray() |
| 19 | + }) |
| 20 | + |
| 21 | + it('correctly transforms all data types to csv', () => { |
| 22 | + const result = transformArrayToCSVRow(testArray) |
| 23 | + |
| 24 | + expect(result).to.equal( |
| 25 | + '"20146cdc-9b40-4769-aa78-b51c17080d56",' + |
| 26 | + '"4.1.1",' + |
| 27 | + '9700,' + |
| 28 | + '"Low loss tidal abstraction of water up to and ""including"" 25,002 megalitres, known as ML/yr a year where no model applies",' + |
| 29 | + '2022-12-14T18:39:45.000Z,' + |
| 30 | + 'true,' + |
| 31 | + ',' + |
| 32 | + ',' + |
| 33 | + 'false,' + |
| 34 | + ',' + |
| 35 | + '25002,' + |
| 36 | + '"{""message"": ""a json object""}"' + |
| 37 | + '\n' |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('when the data type is', () => { |
| 42 | + describe('an object', () => { |
| 43 | + it('correctly formats the object to a string', () => { |
| 44 | + const result = transformArrayToCSVRow([{ message: 'a json object' }]) |
| 45 | + |
| 46 | + expect(result).to.equal('"{""message"": ""a json object""}"\n') |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('a UUID', () => { |
| 51 | + it('correctly formats the UUID to a string', () => { |
| 52 | + const result = transformArrayToCSVRow(['20146cdc-9b40-4769-aa78-b51c17080d56']) |
| 53 | + |
| 54 | + expect(result).to.equal('"20146cdc-9b40-4769-aa78-b51c17080d56"\n') |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + describe('a boolean', () => { |
| 59 | + it('correctly formats the boolean to a string', () => { |
| 60 | + const result = transformArrayToCSVRow([true]) |
| 61 | + |
| 62 | + expect(result).to.equal('true\n') |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('a number', () => { |
| 67 | + it('correctly formats the number to a string', () => { |
| 68 | + const result = transformArrayToCSVRow([100]) |
| 69 | + |
| 70 | + expect(result).to.equal('100\n') |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe('a string containing', () => { |
| 75 | + describe('a comma ,', () => { |
| 76 | + it('correctly formats the string', () => { |
| 77 | + const result = transformArrayToCSVRow(['I am a, comma seperated sentence.']) |
| 78 | + |
| 79 | + expect(result).to.equal('"I am a, comma seperated sentence."\n') |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('a single double quote "', () => { |
| 84 | + it('correctly formats the string', () => { |
| 85 | + const result = transformArrayToCSVRow(['I am a " double quote sentence.']) |
| 86 | + |
| 87 | + expect(result).to.equal('"I am a "" double quote sentence."\n') |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + describe('a double double quote ""', () => { |
| 92 | + it('correctly formats the string', () => { |
| 93 | + const result = transformArrayToCSVRow(['I am a "" double quote sentence.']) |
| 94 | + |
| 95 | + expect(result).to.equal('"I am a """" double quote sentence."\n') |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe('a back slash "\\" ', () => { |
| 100 | + it('correctly formats the string', () => { |
| 101 | + const result = transformArrayToCSVRow(['I am a "\\" back slash sentence.']) |
| 102 | + |
| 103 | + expect(result).to.equal('"I am a ""\\"" back slash sentence."\n') |
| 104 | + }) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('a date', () => { |
| 109 | + it('correctly formats the date to an iso string', () => { |
| 110 | + const result = transformArrayToCSVRow([new Date('2021-02-01')]) |
| 111 | + |
| 112 | + expect(result).to.equal('2021-02-01T00:00:00.000Z\n') |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('when an array of strings us provided', () => { |
| 118 | + it('converts the data to a CSV format', () => { |
| 119 | + const result = transformArrayToCSVRow(['name', 'age']) |
| 120 | + |
| 121 | + expect(result).to.equal('"name","age"\n') |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + describe('when no array is provided', () => { |
| 126 | + it('returns undefined', () => { |
| 127 | + const result = transformArrayToCSVRow() |
| 128 | + |
| 129 | + expect(result).to.equal(undefined) |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
| 134 | + |
| 135 | +function _testArray() { |
| 136 | + return [ |
| 137 | + '20146cdc-9b40-4769-aa78-b51c17080d56', |
| 138 | + '4.1.1', |
| 139 | + 9700, |
| 140 | + 'Low loss tidal abstraction of water up to and "including" 25,002 megalitres, known as ML/yr a year where no model applies', |
| 141 | + new Date(2022, 11, 14, 18, 39, 45), |
| 142 | + true, |
| 143 | + null, |
| 144 | + undefined, |
| 145 | + false, |
| 146 | + '', |
| 147 | + 25002, |
| 148 | + { message: 'a json object' } |
| 149 | + ] |
| 150 | +} |
0 commit comments