Skip to content

Commit

Permalink
clean up and improved unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ntodorov committed Dec 17, 2023
1 parent fc65f16 commit 1b0713b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
# - run: npm run build --if-present
- run: npm run lint
- run: npm test
- name: Coveralls
Expand Down
3 changes: 0 additions & 3 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//generate index file from this folder
// Path: src/utils/index.js

const { logErrors } = require('./log-errors');
const { formatTime } = require('./format-time');
const { readJsonFileNames } = require('./read-data-files');
Expand Down
4 changes: 1 addition & 3 deletions src/utils/read-data-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ const path = require('path');
function readJsonFileNames(dataFolder) {
const files = fs.readdirSync(dataFolder);
const jsonFileNames = files.filter((file) => path.extname(file) === '.json');
// console.table(jsonFiles);
console.log('Sorting files...');
// Sort the filenames using the Array.prototype.sort method
jsonFileNames.sort(alphaNumericSort);
// console.table(jsonFiles);

return jsonFileNames;
}

Expand Down
60 changes: 56 additions & 4 deletions src/utils/read-data-files.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const fs = require('fs');
jest.mock('fs');
const { readJsonFileNames } = require('./read-data-files');
// jest.mock('path');

describe('readJsonFileNames', () => {
it('should return an array of JSON file names sorted alphabetically', () => {
it('should return an array of JSON file names sorted alphanumeric', () => {
const dataFolder = '/path/to/data/folder';
const expectedFileNames = ['file1.json', 'file2.json', 'file3.json'];
fs.readdirSync.mockReturnValue([
Expand All @@ -13,15 +12,68 @@ describe('readJsonFileNames', () => {
'file2.json',
'file4.txt',
]);
const result = readJsonFileNames(dataFolder);

expect(result).toEqual(expectedFileNames);
expect(fs.readdirSync).toHaveBeenCalledWith(dataFolder);
expect(result).not.toBe(expectedFileNames); // Ensure a new array is returned
});

it('should return properly sorted array of more complex file names', () => {
const dataFolder = '/path/to/data/folder';
const expectedFileNames = [
'Payload_Id ge 1 and Id le 1500_EAS14.json',
'Payload_Id ge 1501 and Id le 3000_EAS14.json',
'Payload_Id ge 3001 and Id le 4500_EAS14.json',
'Payload_Id ge 4501 and Id le 6000_EAS14.json',
'Payload_Id ge 6001 and Id le 7500_EAS14.json',
'Payload_Id ge 7501 and Id le 9000_EAS14.json',
'Payload_Id ge 9001 and Id le 10500_EAS14.json',
];

fs.readdirSync.mockReturnValue([
'Payload_Id ge 1501 and Id le 3000_EAS14.json',
'Payload_Id ge 9001 and Id le 10500_EAS14.json',
'Payload_Id ge 3001 and Id le 4500_EAS14.json',
'Payload_Id ge 4501 and Id le 6000_EAS14.json',
'Payload_Id ge 1 and Id le 1500_EAS14.json',
'Payload_Id ge 6001 and Id le 7500_EAS14.json',
'Payload_Id ge 7501 and Id le 9000_EAS14.json',
'blebleh.txt',
]);

// path.extname.mockReturnValue('.json');
const result = readJsonFileNames(dataFolder);

expect(result).toEqual(expectedFileNames);
expect(fs.readdirSync).toHaveBeenCalledWith(dataFolder);
expect(result).not.toBe(expectedFileNames); // Ensure a new array is returned
});

it('should sorted numeric files too', () => {
const dataFolder = '/path/to/data/folder';
const expectedFileNames = ['1.json', '2.json', '3.json'];
fs.readdirSync.mockReturnValue(['3.json', '1.json', '2.json', 'file4.txt']);
const result = readJsonFileNames(dataFolder);

expect(result).toEqual(expectedFileNames);
expect(fs.readdirSync).toHaveBeenCalledWith(dataFolder);
expect(result).not.toBe(expectedFileNames); // Ensure a new array is returned
});

// Add more test cases if needed
it('should sort similar file names too', () => {
const dataFolder = '/path/to/data/folder';
const expectedFileNames = ['1.json', '2a.json', '2b.json', '3.json'];
fs.readdirSync.mockReturnValue([
'3.json',
'1.json',
'2b.json',
'file4.txt',
'2a.json',
]);
const result = readJsonFileNames(dataFolder);

expect(result).toEqual(expectedFileNames);
expect(fs.readdirSync).toHaveBeenCalledWith(dataFolder);
expect(result).not.toBe(expectedFileNames); // Ensure a new array is returned
});
});

0 comments on commit 1b0713b

Please sign in to comment.