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 1 commit
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
}
]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"eslint-plugin-json": "^1.4.0",
"fs": "^0.0.2",
"is-valid-path": "^0.1.1",
"jshint": "^2.10.2",
"lodash": "^4.17.11",
"path": "^0.12.7",
"path-exists": "^4.0.0",
Expand Down
106 changes: 53 additions & 53 deletions src/fileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
existsSync,
readdirSync,
statSync,
readFile,
} from 'fs';
import isValid from 'is-valid-path';
import { stripSymbols, getFileName } from './writeFile';
readFile
} from "fs";
atherdon marked this conversation as resolved.
Show resolved Hide resolved
import isValid from "is-valid-path";
atherdon marked this conversation as resolved.
Show resolved Hide resolved
import { stripSymbols, getFileName } from "./writeFile";
atherdon marked this conversation as resolved.
Show resolved Hide resolved

/**
* Write in file
Expand All @@ -20,55 +20,57 @@ import { stripSymbols, getFileName } from './writeFile';
*/
// @TODO cover a test case, when dataStr is not an array.
// we can just pass a string there
const write = (path, data) => new Promise((resolve) => {
if (!isValid(path)) {
console.log('path is not valid');
}

let dataStr;
const write = (path, data) =>
new Promise(resolve => {
if (!isValid(path)) {
console.log("path is not valid");
atherdon marked this conversation as resolved.
Show resolved Hide resolved
}

if (typeof data === 'string') {
dataStr = data;
} else {
dataStr = stripSymbols(data);
}
let dataStr;

writeFile(path, dataStr, (err) => {
if (err) {
console.error(err);
resolve(false);
if (typeof data === "string") {
atherdon marked this conversation as resolved.
Show resolved Hide resolved
dataStr = data;
} else {
console.info(`${path} file generated successfully!`);
resolve(true);
dataStr = stripSymbols(data);
}

writeFile(path, dataStr, err => {
if (err) {
console.error(err);
resolve(false);
} else {
console.info(`${path} file generated successfully!`);
resolve(true);
}
});
});
});

/**
* read()
* @param {string} absolutePath
*
*/
const read = (absolutePath) => new Promise((resolve, reject) => {
console.log(absolutePath);
if (!isValid(absolutePath)) {
console.log('path is invalid');
}
let dataStr;
readFile(absolutePath, (err, data) => {
if (!err) {
if (data === '') {
console.log(`${absolutePath} returned empty`);
}
console.log(data);
dataStr = JSON.parse(data);
resolve(dataStr);
} else {
console.log(err);
reject(err);
const read = absolutePath =>
new Promise((resolve, reject) => {
console.log(absolutePath);
if (!isValid(absolutePath)) {
console.log("path is invalid");
}
let dataStr;
readFile(absolutePath, "utf8", (err, data) => {
if (!err) {
if (data === "") {
console.log(`${absolutePath} returned empty`);
}
dataStr = JSON.parse(data);
console.log(dataStr);
resolve(dataStr);
} else {
console.log(err);
reject(err);
}
});
});
});

/**
* @param {String} folderNamePath
Expand All @@ -81,7 +83,7 @@ const read = (absolutePath) => new Promise((resolve, reject) => {
// there should be another way
const save = (folderNamePath, file, fileData, flag) => {
if (!isValid(folderNamePath)) {
console.log('path is not valid');
console.log("path is not valid");
}
const fileDataLength = fileData.length;
let success = true;
Expand All @@ -97,16 +99,16 @@ const save = (folderNamePath, file, fileData, flag) => {
const result = write(elementPath, fileData[i]);
if (!result) {
console.log(
`${fileName} is the filename, `
+ `${elementPath} is the elementPath `
+ 'and success is false',
`${fileName} is the filename, ` +
`${elementPath} is the elementPath ` +
"and success is false"
);
}

success = success && result;
}

return new Promise((resolve) => {
return new Promise(resolve => {
resolve(success);
});
};
Expand All @@ -116,29 +118,29 @@ const save = (folderNamePath, file, fileData, flag) => {
* @param {string} folderNamePath
*
*/
const isFolderExists = (folderNamePath) => existsSync(folderNamePath);
const isFolderExists = folderNamePath => existsSync(folderNamePath);

/**
* @param {string} path
*
*/
const dirSync = (filepath) => readdirSync(filepath);
const dirSync = filepath => readdirSync(filepath);

/**
* @param {string} path
*
*/
const syncStats = (filepath) => statSync(filepath);
const syncStats = filepath => statSync(filepath);

/**
* @param {String} path
* @param {String} file
*/
const makeFolder = (path, file) => {
if (!isValid(path)) {
console.log('path is not valid');
console.log("path is not valid");
}
const suffix = '_elements';
const suffix = "_elements";
const folderName = file.slice(0, -5) + suffix;
const folderNamePath = path + folderName;

Expand All @@ -148,6 +150,4 @@ const makeFolder = (path, file) => {
return folderNamePath;
};

export {
write, read, save, makeFolder, isFolderExists, dirSync, syncStats,
};
export { write, read, save, makeFolder, isFolderExists, dirSync, syncStats };
20 changes: 17 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,18 @@ describe('testing function read()', () => {
// removed test file
fs.unlinkSync(testFullPath);
});

test('testing with output', async () => {
const outputFiles = readdirSync('./output/');
let testPath; let content; let
result;
for (const file of outputFiles) {
if (file !== 'undefined.json') {
testPath = `./output/${file}`;
content = JSON.parse(fs.readFileSync(testPath));
result = await read(testPath);
expect(result).toStrictEqual(content);
}
}
});
});