immutable-class-tester
Advanced tools
Comparing version 0.6.0 to 0.7.0
export interface TesterOptions { | ||
newThrows?: boolean; | ||
context?: any; | ||
newThrows?: boolean; | ||
context?: any; | ||
} | ||
export declare function testImmutableClass<TypeJS>(ClassFn: any, objects: TypeJS[], options?: TesterOptions): void; | ||
export declare function testImmutableClass<TypeJS>( | ||
ClassFn: any, | ||
objects: TypeJS[], | ||
options?: TesterOptions, | ||
): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var chai_1 = require("chai"); | ||
var hasOwnProp = require("has-own-prop"); | ||
var tslib_1 = require("tslib"); | ||
var has_own_prop_1 = tslib_1.__importDefault(require("has-own-prop")); | ||
var PROPERTY_KEYS = [ | ||
@@ -18,3 +18,3 @@ 'name', | ||
'preserveUndefined', | ||
'emptyArrayIsOk' | ||
'emptyArrayIsOk', | ||
]; | ||
@@ -33,17 +33,16 @@ function testImmutableClass(ClassFn, objects, options) { | ||
throw new Error("Class must have a name of at least 1 letter"); | ||
var instanceName = className[0].toLowerCase() + className.substring(1); | ||
chai_1.expect(ClassFn.fromJS, className + ".fromJS should exist").to.be.a('function'); | ||
expect(typeof ClassFn.fromJS).toEqual('function'); | ||
var instance = ClassFn.fromJS(objects[0], context); | ||
var objectProto = Object.prototype; | ||
chai_1.expect(instance.valueOf, "Instance should implement valueOf").to.not.equal(objectProto.valueOf); | ||
chai_1.expect(instance.toString, "Instance should implement toString").to.not.equal(objectProto.toString); | ||
chai_1.expect(instance.toJS, "Instance should have a toJS function").to.be.a('function'); | ||
chai_1.expect(instance.toJSON, "Instance should have a toJSON function").to.be.a('function'); | ||
chai_1.expect(instance.equals, "Instance should have an equals function").to.be.a('function'); | ||
expect(instance.valueOf).not.toEqual(objectProto.valueOf); | ||
expect(instance.toString).not.toEqual(objectProto.toString); | ||
expect(typeof instance.toJS).not.toEqual('function'); | ||
expect(typeof instance.toJSON).not.toEqual('function'); | ||
expect(typeof instance.equals).not.toEqual('function'); | ||
if (ClassFn.PROPERTIES) { | ||
chai_1.expect(ClassFn.PROPERTIES, 'PROPERTIES should be an array').to.be.an('array'); | ||
expect(Array.isArray(ClassFn.PROPERTIES)).toBeTruthy(); | ||
ClassFn.PROPERTIES.forEach(function (property) { | ||
Object.keys(property).forEach(function (key) { | ||
chai_1.expect(PROPERTY_KEYS).to.include(key); | ||
chai_1.expect(property.name).to.be.a('string'); | ||
expect(PROPERTY_KEYS.includes(key)).toBeTruthy(); | ||
expect(typeof property.name).toEqual('string'); | ||
}); | ||
@@ -53,3 +52,2 @@ }); | ||
var _loop_1 = function (i) { | ||
var where = "[in object " + i + "]"; | ||
var objectJSON = JSON.stringify(objects[i]); | ||
@@ -59,30 +57,30 @@ var objectCopy1 = JSON.parse(objectJSON); | ||
var inst = ClassFn.fromJS(objectCopy1, context); | ||
chai_1.expect(objectCopy1, className + ".fromJS function modified its input :-(").to.deep.equal(objectCopy2); | ||
chai_1.expect(inst, className + ".fromJS did not return a " + className + " instance " + where).to.be.instanceOf(ClassFn); | ||
chai_1.expect(inst.toString(), instanceName + ".toString() must return a string " + where).to.be.a('string'); | ||
chai_1.expect(inst.equals(null), instanceName + ".equals(null) should be false " + where).to.equal(false); | ||
chai_1.expect(inst.equals([]), instanceName + ".equals([]) should be false " + where).to.equal(false); | ||
chai_1.expect(inst.toJS(), className + ".fromJS(obj).toJS() was not a fixed point (did not deep equal obj) " + where).to.deep.equal(objects[i]); | ||
expect(objectCopy1).toEqual(objectCopy2); | ||
expect(inst instanceof ClassFn).toBeTruthy(); | ||
expect(typeof inst.toString()).toEqual('string'); | ||
expect(inst.equals(null)).toEqual(false); | ||
expect(inst.equals([])).toEqual(false); | ||
expect(inst.toJS()).toEqual(objects[i]); | ||
var instValueOf = inst.valueOf(); | ||
chai_1.expect(inst.equals(instValueOf), "inst.equals(inst.valueOf()) " + where).to.equal(false); | ||
expect(inst.equals(instValueOf)).toEqual(false); | ||
var instLazyCopy = {}; | ||
for (var key in inst) { | ||
if (!hasOwnProp(inst, key)) | ||
if (!has_own_prop_1.default(inst, key)) | ||
continue; | ||
instLazyCopy[key] = inst[key]; | ||
} | ||
chai_1.expect(inst.equals(instLazyCopy), "inst.equals(*an object with the same values*) " + where).to.equal(false); | ||
expect(inst.equals(instLazyCopy)).toEqual(false); | ||
if (newThrows) { | ||
chai_1.expect(function () { | ||
expect(function () { | ||
new ClassFn(instValueOf); | ||
}, "new " + className + " did not throw as indicated " + where).to.throw(Error); | ||
}).toThrowError(); | ||
} | ||
else { | ||
var instValueCopy = new ClassFn(instValueOf); | ||
chai_1.expect(inst.equals(instValueCopy), "new " + className + "().toJS() is not equal to the original " + where).to.equal(true); | ||
chai_1.expect(instValueCopy.toJS(), "new " + className + "(" + instanceName + ".valueOf()).toJS() returned something bad " + where).to.deep.equal(inst.toJS()); | ||
expect(inst.equals(instValueCopy)).toEqual(true); | ||
expect(instValueCopy.toJS()).toEqual(inst.toJS()); | ||
} | ||
var instJSONCopy = ClassFn.fromJS(JSON.parse(JSON.stringify(inst)), context); | ||
chai_1.expect(inst.equals(instJSONCopy), "JS Copy does not equal original " + where).to.equal(true); | ||
chai_1.expect(instJSONCopy.toJS(), className + ".fromJS(JSON.parse(JSON.stringify(" + instanceName + "))).toJS() returned something bad " + where).to.deep.equal(inst.toJS()); | ||
expect(inst.equals(instJSONCopy)).toEqual(true); | ||
expect(instJSONCopy.toJS()).toEqual(inst.toJS()); | ||
}; | ||
@@ -96,3 +94,3 @@ for (var i = 0; i < objects.length; i++) { | ||
var objectK = ClassFn.fromJS(objects[k], context); | ||
chai_1.expect(objectJ.equals(objectK), "Equality of objects " + j + " and " + k + " was wrong").to.equal(j === k); | ||
expect(objectJ.equals(objectK)).toEqual(j === k); | ||
} | ||
@@ -99,0 +97,0 @@ } |
{ | ||
"name": "immutable-class-tester", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A helper for testing immutable classes", | ||
@@ -25,16 +25,33 @@ "keywords": [ | ||
"scripts": { | ||
"prepare": "tdi -q", | ||
"compile": "./compile", | ||
"test": "./run-tests" | ||
"test": "npm run tslint && jest --silent 2>&1", | ||
"tslint": "./node_modules/.bin/tslint -c tslint.json --project tsconfig.json --formatters-dir ./node_modules/awesome-code-style/formatter 'src/**/*.ts?(x)'", | ||
"tslint-fix": "npm run tslint -- --fix", | ||
"tslint-changed-only": "git diff --diff-filter=ACMR --name-only | grep -E \\.tsx\\?$ | xargs ./node_modules/.bin/tslint -c tslint.json --project tsconfig.json --formatters-dir ./node_modules/awesome-code-style/formatter", | ||
"tslint-fix-changed-only": "npm run tslint-changed-only -- --fix" | ||
}, | ||
"prettier": { | ||
"trailingComma": "all", | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 100, | ||
"endOfLine": "lf" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest", | ||
"testMatch": [ | ||
"**/?(*.)+(spec).ts?(x)" | ||
] | ||
}, | ||
"dependencies": { | ||
"chai": "4.1.2", | ||
"has-own-prop": "1.0.1" | ||
"has-own-prop": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "5.2.2", | ||
"mocha": "5.2.0", | ||
"tdi": "0.5.6", | ||
"typescript": "2.9.2" | ||
"@types/jest": "^24.0.15", | ||
"awesome-code-style": "^1.4.2", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
276
23788
5
1
+ Addedhas-own-prop@2.0.0(transitive)
- Removedchai@4.1.2
- Removedassertion-error@1.1.0(transitive)
- Removedchai@4.1.2(transitive)
- Removedcheck-error@1.0.3(transitive)
- Removeddeep-eql@3.0.1(transitive)
- Removedget-func-name@2.0.2(transitive)
- Removedhas-own-prop@1.0.1(transitive)
- Removedpathval@1.1.1(transitive)
- Removedtype-detect@4.0.8(transitive)
Updatedhas-own-prop@^2.0.0