Skip to content

Commit

Permalink
Merge pull request #1 from ajimae/add-unit-test-#0001
Browse files Browse the repository at this point in the history
#1 setup unit tests
  • Loading branch information
ajimae committed Aug 26, 2019
2 parents 25f2e83 + 3904e63 commit f7dfc43
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
package-lock.json

# TypeScript v1 declaration files
typings/
Expand Down
25 changes: 25 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cache": false,
"check-coverage": false,
"extension": [
".ts"
],
"include": [
"**/*js",
"**/*ts"
],
"exclude": [
"coverage/**",
"node_modules/**",
"**/*.d.ts",
"**/*.test.ts"
],
"sourceMap": true,
"reporter": [
"html",
"text",
"text-summary"
],
"all": true,
"instrument": true
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "node"
cache:
directories:
- "node_modules"
script:
- npm test
after_success:
- npm run coverage
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# ncrypt-js

[![Build Status](https://travis-ci.com/ajimae/ncrypt-js.svg?branch=master)](https://travis-ci.com/ajimae/ncrypt-js) [![Coverage Status](https://coveralls.io/repos/github/ajimae/ncrypt-js/badge.svg)](https://coveralls.io/github/ajimae/ncrypt-js)

A light weight plain text encoder and decoder
5 changes: 0 additions & 5 deletions dist/index.js

This file was deleted.

61 changes: 0 additions & 61 deletions dist/src/ncrypt.js

This file was deleted.

70 changes: 0 additions & 70 deletions dist/src/utils.js

This file was deleted.

20 changes: 0 additions & 20 deletions package-lock.json

This file was deleted.

20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"start": "tsc --outDir dist && node dist/index",
"start:dev": "tsc -w index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "cross-env TS_NODE_FILES=true nyc mocha --exit --require ts-node/register --colors test/**/*.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
"type": "git",
Expand All @@ -29,11 +30,22 @@
"author": "meeky",
"license": "MIT",
"bugs": {
"url": "https://github.com/ajimae/node-encode/issues"
"url": "https://github.com/ajimae/ncrypt-js/issues"
},
"homepage": "https://github.com/ajimae/node-encode#readme",
"homepage": "https://github.com/ajimae/ncrypt-js#readme",
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.2",
"crypto": "^1.0.1"
"chai": "^4.2.0",
"coveralls": "^3.0.6",
"cross-env": "^5.2.0",
"crypto": "^1.0.1",
"mocha": "^6.2.0",
"mocha-istanbul": "^0.3.0",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
}
}
97 changes: 97 additions & 0 deletions test/ncrypt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as chai from "chai";

import * as ncrypt from '../index';

const expect = chai.expect;
const { encrypt, decrypt } = ncrypt;

const object = {
SimpleCrypto: "is great.",
You: "should try it!"
};
const string = "SimpleCrypto is great.";
const number = 19960404;
const boolean = false;

const _secret = 'shhh its a secret';
const _nullSecret: any = null;

const encryptString = encrypt(string, _secret);
const encryptNumber = encrypt(number, _secret);
const encryptObject = encrypt(object, _secret);
const encryptBoolean = encrypt(boolean, _secret);

const decryptString = decrypt(encryptString);
const decryptNumber = decrypt(encryptNumber);
const decryptObject = decrypt(encryptObject);
const decryptBoolean = decrypt(encryptBoolean);

describe('Encrytion', () => {
it('should be able to encrypt a string', () => {
expect(string).to.be.a('string');
expect(typeof encryptString).to.eql('string');
});

it('should be able to encrypt an object', () => {
expect(object).to.be.a('object');
expect(typeof encryptObject).to.eql('string');
});

it('should be able to encrypt a number', () => {
expect(number).to.be.a('number');
expect(typeof encryptNumber).to.eql('string');
});

it('should be able to encrypt a boolean', () => {
expect(boolean).to.be.a('boolean');
expect(typeof encryptBoolean).to.eql('string');
});
});

describe('Decrytion', () => {
it('should be able to decrypt original string', () => {
expect(decryptString).to.be.eql(string);
expect(typeof decryptString).to.eql('string');
});

it('should be able to decrypt original object', () => {
expect(decryptObject).to.be.eql(object);
expect(typeof decryptObject).to.eql('object');
});

it('should be able to decrypt original number', () => {
expect(decryptNumber).to.be.eql(number);
expect(typeof decryptNumber).to.eql('number');
});

it('should be able to decrypt original boolean', () => {
expect(decryptBoolean).to.be.eql(boolean);
expect(typeof decryptBoolean).to.eql('boolean');
});
});

describe('Error handling and validations', () => {
it('should error when secret is not provided', () => {
try {
encrypt('nullSecret', _nullSecret);
} catch (error) {
expect(error.message).equal('must be initialized with a secret key of type string');
}
});

it('should throw an error when an undefined data is to be encrypted', () => {
try {
encrypt(undefined, _secret);
} catch (error) {
expect(error.message).equal('no data was entered, enter data of type object, number, string or boolean to be encrypted.');
}
});

it('should throw an error when an undefined data is to be encrypted', () => {
try {
encrypt(null, _secret);
} catch (error) {
expect(error.message).equal('no data was entered, enter data of type object, number, string or boolean to be encrypted.');
}
});
});
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2015",
"module": "commonjs",
"lib": ["es2015", "es2016", "dom", "es2017", "es6", "es5"],
"esModuleInterop": true,
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": false,
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
Expand All @@ -15,5 +15,8 @@
},
"include": [
"src/**/*", "index.ts",
],
"exclude": [
"test/**/*"
]
}

0 comments on commit f7dfc43

Please sign in to comment.