|
1 | 1 | const path = require('path');
|
| 2 | +const rewire = require('rewire'); |
2 | 3 | const { CSVModule } = require('../../src/modules');
|
3 | 4 | const exampleResponse = require('./fixtures/csv-response.json');
|
4 | 5 |
|
| 6 | +const CSVModuleRewired = rewire('../../src/modules/CSVModule.js'); |
| 7 | +const normalizeEmptyValues = CSVModuleRewired.__get__('normalizeEmptyValues'); |
| 8 | + |
5 | 9 | const INVALID_MRN = 'INVALID MRN';
|
6 | 10 | const csvModule = new CSVModule(path.join(__dirname, './fixtures/example-csv.csv'));
|
7 | 11 | const csvModuleWithBOMs = new CSVModule(path.join(__dirname, './fixtures/example-csv-bom.csv'));
|
8 | 12 |
|
9 |
| -test('Reads data from CSV', async () => { |
10 |
| - const data = await csvModule.get('mrn', 'example-mrn-1'); |
11 |
| - expect(data).toEqual(exampleResponse); |
12 |
| -}); |
13 | 13 |
|
14 |
| -test('Reads data from CSV with a Byte Order Mark', async () => { |
15 |
| - const data = await csvModuleWithBOMs.get('mrn', 'example-mrn-1'); |
16 |
| - expect(data).toEqual(exampleResponse); |
17 |
| -}); |
| 14 | +describe('CSVModule', () => { |
| 15 | + describe('get', () => { |
| 16 | + test('Reads data from CSV', async () => { |
| 17 | + const data = await csvModule.get('mrn', 'example-mrn-1'); |
| 18 | + expect(data).toEqual(exampleResponse); |
| 19 | + }); |
18 | 20 |
|
19 |
| -test('Returns multiple rows', async () => { |
20 |
| - const data = await csvModule.get('mrn', 'example-mrn-2'); |
21 |
| - expect(data).toHaveLength(2); |
22 |
| -}); |
| 21 | + test('Reads data from CSV with a Byte Order Mark', async () => { |
| 22 | + const data = await csvModuleWithBOMs.get('mrn', 'example-mrn-1'); |
| 23 | + expect(data).toEqual(exampleResponse); |
| 24 | + }); |
23 | 25 |
|
24 |
| -test('Returns all rows when both key and value are undefined', async () => { |
25 |
| - const data = await csvModule.get(); |
26 |
| - expect(data).toHaveLength(csvModule.data.length); |
27 |
| - expect(data).toEqual(csvModule.data); |
28 |
| -}); |
| 26 | + test('Returns multiple rows', async () => { |
| 27 | + const data = await csvModule.get('mrn', 'example-mrn-2'); |
| 28 | + expect(data).toHaveLength(2); |
| 29 | + }); |
29 | 30 |
|
30 |
| -test('Returns data with recordedDate after specified from date', async () => { |
31 |
| - const data = await csvModule.get('mrn', 'example-mrn-2', '2020-05-01'); |
32 |
| - expect(data).toHaveLength(1); |
33 |
| -}); |
| 31 | + test('Returns all rows when both key and value are undefined', async () => { |
| 32 | + const data = await csvModule.get(); |
| 33 | + expect(data).toHaveLength(csvModule.data.length); |
| 34 | + expect(data).toEqual(csvModule.data); |
| 35 | + }); |
34 | 36 |
|
35 |
| -test('Returns data with recordedDate before specified to date', async () => { |
36 |
| - const data = await csvModule.get('mrn', 'example-mrn-2', null, '2020-05-01'); |
37 |
| - expect(data).toHaveLength(1); |
38 |
| -}); |
| 37 | + test('Returns data with recordedDate after specified from date', async () => { |
| 38 | + const data = await csvModule.get('mrn', 'example-mrn-2', '2020-05-01'); |
| 39 | + expect(data).toHaveLength(1); |
| 40 | + }); |
39 | 41 |
|
40 |
| -test('Should return an empty array when key-value pair does not exist', async () => { |
41 |
| - const data = await csvModule.get('mrn', INVALID_MRN); |
42 |
| - expect(data).toEqual([]); |
43 |
| -}); |
| 42 | + test('Returns data with recordedDate before specified to date', async () => { |
| 43 | + const data = await csvModule.get('mrn', 'example-mrn-2', null, '2020-05-01'); |
| 44 | + expect(data).toHaveLength(1); |
| 45 | + }); |
| 46 | + |
| 47 | + test('Should return an empty array when key-value pair does not exist', async () => { |
| 48 | + const data = await csvModule.get('mrn', INVALID_MRN); |
| 49 | + expect(data).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + test('Should return proper value regardless of key casing', async () => { |
| 53 | + const data = await csvModule.get('mRN', 'example-mrn-1'); |
| 54 | + expect(data).toEqual(exampleResponse); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('normalizeEmptyValues', () => { |
| 59 | + it('Should turn "null" values into empty strings, regardless of case', () => { |
| 60 | + const data = [{ key: 'null' }, { key: 'NULL' }, { key: 'nuLL' }]; |
| 61 | + const normalizedData = normalizeEmptyValues(data); |
| 62 | + normalizedData.forEach((d) => { |
| 63 | + expect(d.key).toBe(''); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Should turn "nil" values into empty strings, regardless of case', () => { |
| 68 | + const data = [{ key: 'nil' }, { key: 'NIL' }, { key: 'NIl' }]; |
| 69 | + const normalizedData = normalizeEmptyValues(data); |
| 70 | + normalizedData.forEach((d) => { |
| 71 | + expect(d.key).toBe(''); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('Should not modify unalterableColumns, regardless of their value', () => { |
| 76 | + const data = [{ key: 'null' }, { key: 'NULL' }, { key: 'nuLL' }, { key: 'nil' }, { key: 'NIL' }, { key: 'NIl' }]; |
| 77 | + const normalizedData = normalizeEmptyValues(data, ['key']); |
| 78 | + normalizedData.forEach((d) => { |
| 79 | + expect(d.key).not.toBe(''); |
| 80 | + }); |
| 81 | + }); |
44 | 82 |
|
45 |
| -test('Should return proper value regardless of key casing', async () => { |
46 |
| - const data = await csvModule.get('mRN', 'example-mrn-1'); |
47 |
| - expect(data).toEqual(exampleResponse); |
| 83 | + it('Should leave all other values uneffected, regardless of case', () => { |
| 84 | + const data = [{ key: 'anything' }, { key: 'any' }, { key: 'thing' }]; |
| 85 | + const normalizedData = normalizeEmptyValues(data); |
| 86 | + normalizedData.forEach((d) => { |
| 87 | + expect(d.key).not.toBe(''); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
48 | 91 | });
|
0 commit comments