Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrote unit tests for read #263

Merged
merged 4 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
});
});