Created
October 23, 2017 12:49
-
-
Save maciejsikora/a8a16706fa79ee852e1e35b910b59a23 to your computer and use it in GitHub Desktop.
Test file of encapsulated es6 module private resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert') | |
const rewire = require('rewire') | |
// standard using of exported functions | |
const { createUser } = require('./user') | |
// using rewire to get into private resources | |
const userRewire = rewire('./user') | |
const hasProp = userRewire.__get__('hasProp') | |
const hasProps = userRewire.__get__('hasProps') | |
const isString = userRewire.__get__('isString') | |
const arePropsStrings = userRewire.__get__('arePropsStrings') | |
const testObj = { | |
name: 'John', | |
lastname: 'Doe', | |
age: 12, | |
origin: 'USA' | |
} | |
describe('hasProp()', () => { | |
it('Object has a prop "name"', () => { | |
assert.strictEqual(true, hasProp(testObj, 'name')) | |
}) | |
it('Object has a prop "age"', () => { | |
assert.strictEqual(true, hasProp(testObj, 'age')) | |
}) | |
it('Object has no such prop "country"', () => { | |
assert.strictEqual(false, hasProp(testObj, 'country')) | |
}) | |
it('Object has no such prop "xyz"', () => { | |
assert.strictEqual(false, hasProp(testObj, 'xyz')) | |
}) | |
}) | |
describe('hasProps()', () => { | |
it('Object has props "name", "lastname"', () => { | |
assert.strictEqual(true, hasProps(testObj, ['name', 'lastname'])) | |
}) | |
it('Object has props "age", "origin", "lastname', () => { | |
assert.strictEqual(true, hasProps(testObj, ['age', 'origin', 'lastname'])) | |
}) | |
it('Object has not all props "age", "country"', () => { | |
assert.strictEqual(false, hasProps(testObj, ['age', 'country'])) | |
}) | |
it('Object has not all props "lastname", "xyz", "age"', () => { | |
assert.strictEqual(false, hasProps(testObj, ['lastname', 'xyz', 'age'])) | |
}) | |
}) | |
describe('isString()', () => { | |
it('"xyz" is a string', () => { | |
assert.strictEqual(true, isString('xyz')) | |
}) | |
it('11 is not a string', () => { | |
assert.strictEqual(false, isString(11)) | |
}) | |
it('null is not a string', () => { | |
assert.strictEqual(false, isString(null)) | |
}) | |
it('[] is not a string', () => { | |
assert.strictEqual(false, isString([])) | |
}) | |
}) | |
describe('arePropsStrings()', () => { | |
it('"name" and "lastname" are strings', () => { | |
const wantedProps = ['name', 'lastname'] | |
assert.strictEqual(true, arePropsStrings(testObj, wantedProps)) | |
}) | |
it('"name" and "lastname" and "origin" are strings', () => { | |
const wantedProps = ['name', 'lastname', 'origin'] | |
assert.strictEqual(true, arePropsStrings(testObj, wantedProps)) | |
}) | |
it('"age" is not a string ', () => { | |
const wantedProps = ['name', 'age'] | |
assert.strictEqual(false, arePropsStrings(testObj, wantedProps)) | |
}) | |
it('"xyz" property doesnt\'t exists ', () => { | |
const wantedProps = ['name', 'xyz'] | |
assert.strictEqual(false, arePropsStrings(testObj, wantedProps)) | |
}) | |
}) | |
describe('createUser()', () => { | |
it('create user John Doe', () => { | |
const user = createUser(testObj) | |
assert.strictEqual('John', user.getName()) | |
assert.strictEqual('Doe', user.getLastName()) | |
assert.strictEqual('John Doe', user.getFullName()) | |
}) | |
it('create user Tom Johnson', () => { | |
const user = createUser({ | |
name: 'Tom', | |
lastname: 'Johnson' | |
}) | |
assert.strictEqual('Tom', user.getName()) | |
assert.strictEqual('Johnson', user.getLastName()) | |
assert.strictEqual('Tom Johnson', user.getFullName()) | |
}) | |
it('Wrong user data no lastname data', () => { | |
assert.throws(() => { | |
createUser({name: 'Timothy'}) | |
}, Error) | |
}) | |
it('Wrong user data - empty object', () => { | |
assert.throws(() => { | |
createUser({}) | |
}, Error) | |
}) | |
it('Wrong user data - numbers instead of strings ', () => { | |
assert.throws(() => { | |
createUser({name: 12, lastname: 14}) | |
}, Error) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment