Skip to content

Commit 3fe2e98

Browse files
committed
Fix AXIOS call; added test of the CSVURLModule
1 parent 599bd72 commit 3fe2e98

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/modules/CSVURLModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CSVURLModule {
1616
// If data is already cached, this function does nothing
1717
async fillDataCache() {
1818
if (!this.data) {
19-
const csvData = await axios(this.url).then((res) => res.data);
19+
const csvData = await axios.get(this.url).then((res) => res.data);
2020
// Parse then normalize the data
2121
const parsedData = parse(csvData, {
2222
columns: (header) => header.map((column) => stringNormalizer(column)),

test/modules/CSVURLModule.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const axios = require('axios');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const { CSVURLModule } = require('../../src/modules');
5+
const exampleResponse = require('./fixtures/csv-response.json');
6+
7+
const exampleCSV = fs.readFileSync(path.join(__dirname, './fixtures/example-csv.csv'));
8+
9+
// Instantiate module with mock parameters
10+
const INVALID_MRN = 'INVALID MRN';
11+
const MOCK_URL = 'http://example.com/some/data.csv';
12+
const csvURLModule = new CSVURLModule(MOCK_URL);
13+
jest.mock('axios');
14+
15+
describe('CSVURLModule', () => {
16+
describe('fillDataCache', () => {
17+
it('should make an axios-request when there is no data cached', async () => {
18+
// Mock response from axios before call
19+
axios.get.mockReset();
20+
axios.get.mockResolvedValue({ data: exampleCSV });
21+
expect(csvURLModule.data).toBeUndefined();
22+
await csvURLModule.fillDataCache();
23+
expect(axios.get).toHaveBeenCalled();
24+
expect(csvURLModule.data).not.toBeUndefined();
25+
});
26+
it('should make no requests when there is data cached', async () => {
27+
axios.get.mockReset();
28+
const exampleData = ['anything'];
29+
// Fix the data stored on the module
30+
csvURLModule.data = exampleData;
31+
expect(axios.get).not.toHaveBeenCalled();
32+
expect(csvURLModule.data).toBe(exampleData);
33+
// Since data is defined, this function call should do nothing
34+
await csvURLModule.fillDataCache();
35+
expect(axios.get).not.toHaveBeenCalled();
36+
expect(csvURLModule.data).toBe(exampleData);
37+
// Reset the data stored on the module
38+
csvURLModule.data = undefined;
39+
});
40+
});
41+
42+
describe('get', () => {
43+
test('Reads data from CSV', async () => {
44+
axios.get.mockReset();
45+
axios.get.mockResolvedValue({ data: exampleCSV });
46+
const data = await csvURLModule.get('mrn', 'example-mrn-1');
47+
expect(data).toEqual(exampleResponse);
48+
});
49+
50+
test('Returns multiple rows', async () => {
51+
axios.get.mockReset();
52+
axios.get.mockResolvedValue({ data: exampleCSV });
53+
const data = await csvURLModule.get('mrn', 'example-mrn-2');
54+
expect(data).toHaveLength(2);
55+
});
56+
57+
test('Returns all rows when both key and value are undefined', async () => {
58+
axios.get.mockReset();
59+
axios.get.mockResolvedValue({ data: exampleCSV });
60+
const data = await csvURLModule.get();
61+
expect(data).toHaveLength(csvURLModule.data.length);
62+
expect(data).toEqual(csvURLModule.data);
63+
});
64+
65+
test('Returns data with recordedDate after specified from date', async () => {
66+
axios.get.mockReset();
67+
axios.get.mockResolvedValue({ data: exampleCSV });
68+
const data = await csvURLModule.get('mrn', 'example-mrn-2', '2020-05-01');
69+
expect(data).toHaveLength(1);
70+
});
71+
72+
test('Returns data with recordedDate before specified to date', async () => {
73+
axios.get.mockReset();
74+
axios.get.mockResolvedValue({ data: exampleCSV });
75+
const data = await csvURLModule.get('mrn', 'example-mrn-2', null, '2020-05-01');
76+
expect(data).toHaveLength(1);
77+
});
78+
79+
test('Should return an empty array when key-value pair does not exist', async () => {
80+
axios.get.mockReset();
81+
axios.get.mockResolvedValue({ data: exampleCSV });
82+
const data = await csvURLModule.get('mrn', INVALID_MRN);
83+
expect(data).toEqual([]);
84+
});
85+
86+
test('Should return proper value regardless of key casing', async () => {
87+
axios.get.mockReset();
88+
axios.get.mockResolvedValue({ data: exampleCSV });
89+
const data = await csvURLModule.get('mRN', 'example-mrn-1');
90+
expect(data).toEqual(exampleResponse);
91+
});
92+
});
93+
});

0 commit comments

Comments
 (0)