Skip to content

Commit

Permalink
Merge pull request #263 from saphal1998/master
Browse files Browse the repository at this point in the history
wrote unit tests for read
  • Loading branch information
atherdon authored Sep 20, 2019
2 parents 891b6ac + 29b27a6 commit 5ed77c8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
24 changes: 23 additions & 1 deletion output/items.json
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
[{"item_id":1,"name":"Bread","description":"something about the item","quantity":50,"purchase":false},{"item_id":2,"name":"Milk","description":"something about the item","quantity":200,"purchase":false},{"item_id":2,"name":"Cheese","description":"something about the item","quantity":200,"purchase":false}]
[
{
"item_id": 1,
"name": "Bread",
"description": "something about the item",
"quantity": 50,
"purchase": false
},
{
"item_id": 2,
"name": "Milk",
"description": "something about the item",
"quantity": 200,
"purchase": false
},
{
"item_id": 2,
"name": "Cheese",
"description": "something about the item",
"quantity": 200,
"purchase": false
}
]
4 changes: 2 additions & 2 deletions src/fileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ const read = (absolutePath) => new Promise((resolve, reject) => {
console.log('path is invalid');
}
let dataStr;
readFile(absolutePath, (err, data) => {
readFile(absolutePath, 'utf8', (err, data) => {
if (!err) {
if (data === '') {
console.log(`${absolutePath} returned empty`);
}
console.log(data);
dataStr = JSON.parse(data);
console.log(dataStr);
resolve(dataStr);
} else {
console.log(err);
Expand Down
23 changes: 20 additions & 3 deletions tests/fileSystem.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs, { readdirSync } from 'fs';
import {
write,
save,
Expand All @@ -15,7 +15,7 @@ const testFullPath = testFolder + testFile;
const testFileContent = [{ name: 'Test' }];

// @TODO next step for us will be to extend our test with real cases.
// i.e. instead of passing simple array into test,
// i.e. instead of passing simple array into test,
// we can actually run tests with our real files / objects

describe('testing function write()', () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('testing function isFolderExists()', () => {

test('test isFolderExists() returns false', async () => {
// @TODO lets make a const variable for path to folder and use
// some important folders(few), like `src` or
// some important folders(few), like `src` or
// `dist`(sometimes dist are actually removed, cleaned up)
const result = isFolderExists('./nofolderxxx');
expect(result).toBe(false);
Expand All @@ -92,4 +92,21 @@ describe('testing function read()', () => {
// removed test file
fs.unlinkSync(testFullPath);
});

test('testing with output', async () => {
// @ODO later we can use consts from outside for cases like this.
const outputFiles = readdirSync('./output/');
let testPath, content, result;

for (const file of outputFiles) {
if (file !== 'undefined.json') {
// @ODO later we can use consts from outside for cases like this.
testPath = `./output/${file}`;
// @TODO long line
content = JSON.parse(fs.readFileSync(testPath));
result = await read(testPath);
expect(result).toStrictEqual(content);
}
}
});
});

0 comments on commit 5ed77c8

Please sign in to comment.