Skip to content

Commit

Permalink
Fixed all flow errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vikingair committed Nov 1, 2017
1 parent 33fd467 commit 22bdb2d
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 262 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"rules": {
"prettier/prettier": ["error", {
"useTabs": false,
"printWidth": 120,
"printWidth": 80,
"tabWidth": 4,
"singleQuote": true,
"trailingComma": "es5",
Expand Down
1 change: 0 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[ignore]
<PROJECT_ROOT>/test/.*

[include]

Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"useTabs": false,
"printWidth": 120,
"printWidth": 80,
"tabWidth": 4,
"singleQuote": true,
"trailingComma": "es5",
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ fits more to the rest of the written code.
are treated as class instances and come with a lot of useful features. See below for more.

### Installation

Like every other npm package. You may `npm install spy4js --save-dev` to save the
latest version to your dev dependencies.
##### With yarn
```
yarn add --dev spy4js
```
##### With npm
```
npm install --save-dev spy4js
```

### Interface

Expand Down Expand Up @@ -425,6 +430,8 @@ const differentNumber = callArgs[2]['attr2'];

## Changes

* **1.2.2**
* Updated development environment and fixed a comparison issue with functions.
* **1.2.1**
* Updated flow to latest version and fixed production errors.
* **1.2.0**
Expand Down
6 changes: 5 additions & 1 deletion dist/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ SpyRegistry.prototype.restore = function (index) {
*/
SpyRegistry.prototype.push = function (obj, methodName) {
this.registerCount += 1;
this.register[this.registerCount] = { obj: obj, method: obj[methodName], methodName: methodName };
this.register[this.registerCount] = {
obj: obj,
method: obj[methodName],
methodName: methodName
};
return this.registerCount;
};

Expand Down
15 changes: 12 additions & 3 deletions dist/registry.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const restoreAttributeForEntry = (value: Object): void => {
*/
function SpyRegistry() {
if (!(this instanceof SpyRegistry)) {
throw new Error('\n\nPlease make sure to use this constructor only with "new" keyword.\n\n');
throw new Error(
'\n\nPlease make sure to use this constructor only with "new" keyword.\n\n'
);
}
this.register = {};
this.persReg = {};
Expand Down Expand Up @@ -73,7 +75,11 @@ SpyRegistry.prototype.restore = function(index: number): void {
*/
SpyRegistry.prototype.push = function(obj: Object, methodName: string): number {
this.registerCount += 1;
this.register[this.registerCount] = { obj, method: obj[methodName], methodName };
this.register[this.registerCount] = {
obj,
method: obj[methodName],
methodName,
};
return this.registerCount;
};

Expand Down Expand Up @@ -103,7 +109,10 @@ SpyRegistry.prototype.getOriginalMethod = function(index: number): any {
* @param {boolean} intoPersReg -> boolean to determine the moving
* direction.
*/
SpyRegistry.prototype.persist = function(index: number, intoPersReg: boolean): void {
SpyRegistry.prototype.persist = function(
index: number,
intoPersReg: boolean
): void {
const fromReg = intoPersReg ? this.register : this.persReg;
const toReg = intoPersReg ? this.persReg : this.register;
const entry = fromReg[index];
Expand Down
5 changes: 4 additions & 1 deletion dist/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var registry = new _registry.SpyRegistry();

var __LOCK__ = true;

/**
* Those symbols are used to protect the private spy properties from outer manipulation by mistake.
*/
var Symbols = {
name: (0, _symbol2.default)('__Spy_name__'),
isSpy: (0, _symbol2.default)('__Spy_isSpy__'),
Expand Down Expand Up @@ -160,7 +163,7 @@ Spy.on = function on(obj, methodName) {
throw new Error('The objects attribute \'' + methodName + '\'' + ' was already spied. Please make sure to spy' + ' only once at a time at any attribute.');
}
__LOCK__ = false;
var spy = new Spy("the spy on '" + methodName + "'", { obj: obj, methodName: methodName });
var spy = new Spy('the spy on \'' + methodName + '\'', { obj: obj, methodName: methodName });
__LOCK__ = true;
obj[methodName] = spy;
return spy;
Expand Down
49 changes: 38 additions & 11 deletions dist/spy.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const registry = new SpyRegistry();

let __LOCK__ = true;

const Symbols = {
/**
* Those symbols are used to protect the private spy properties from outer manipulation by mistake.
*/
const Symbols: any = {
name: Symbol('__Spy_name__'),
isSpy: Symbol('__Spy_isSpy__'),
func: Symbol('__Spy_func__'),
Expand Down Expand Up @@ -64,7 +67,9 @@ const DefaultSettings = {
*/
function Spy(name: string = 'the spy', __mock: any) {
if (!(this instanceof Spy)) {
throw new Error('\n\nPlease make sure to use this constructor only with "new" keyword.\n\n');
throw new Error(
'\n\nPlease make sure to use this constructor only with "new" keyword.\n\n'
);
}
let spy: any = function(...args: Array<any>) {
spy[Symbols.calls].push({ arguments: args });
Expand Down Expand Up @@ -147,7 +152,7 @@ Spy.on = function on(obj: Object, methodName: string): Spy {
);
}
__LOCK__ = false;
const spy = new Spy("the spy on '" + methodName + "'", { obj, methodName });
const spy = new Spy(`the spy on '${methodName}'`, { obj, methodName });
__LOCK__ = true;
obj[methodName] = spy;
return spy;
Expand All @@ -174,7 +179,10 @@ Spy.on = function on(obj: Object, methodName: string): Spy {
*
* @return {Array<Spy>}
*/
Spy.onMany = function onMany(obj: Object, ...methodNames: Array<string>): Array<Spy> {
Spy.onMany = function onMany(
obj: Object,
...methodNames: Array<string>
): Array<Spy> {
const spies = [];
for (let i = 0; i < methodNames.length; i++) {
const spy = Spy.on(obj, methodNames[i]);
Expand Down Expand Up @@ -213,7 +221,10 @@ Spy.restoreAll = function restoreAll(): void {
* for special configuration
* @return {Spy} <- BuilderPattern.
*/
Spy.prototype.configure = function(config: { useOwnEquals?: boolean, persistent?: boolean }): Spy {
Spy.prototype.configure = function(config: {
useOwnEquals?: boolean,
persistent?: boolean,
}): Spy {
if (config.useOwnEquals !== undefined) {
this[Symbols.config].useOwnEquals = config.useOwnEquals;
}
Expand Down Expand Up @@ -292,7 +303,9 @@ Spy.prototype.returns = function(...args: Array<any>): Spy {
*/
Spy.prototype.throws = function(message: ?string): Spy {
this[Symbols.func] = () => {
throw new Error(message || `${this[Symbols.name]} was requested to throw`);
throw new Error(
message || `${this[Symbols.name]} was requested to throw`
);
};
return this;
};
Expand Down Expand Up @@ -324,7 +337,10 @@ Spy.prototype.reset = function(): Spy {
*/
Spy.prototype.restore = function(): Spy {
if (this[Symbols.config].persistent) {
throw new Error(`\n\n${this[Symbols.name]} can not be restored!` + ' It was configured to be persistent.');
throw new Error(
`\n\n${this[Symbols.name]} can not be restored!` +
' It was configured to be persistent.'
);
}
registry.restore(this[Symbols.index]);
return this;
Expand Down Expand Up @@ -380,7 +396,9 @@ Spy.prototype.transparentAfter = function(callCount: number): Spy {
// are more than the call count were we
// need to modify the behavior
if (this[Symbols.calls].length > callCount) {
const originalMethod = registry.getOriginalMethod(this[Symbols.index]);
const originalMethod = registry.getOriginalMethod(
this[Symbols.index]
);
if (originalMethod) {
return originalMethod(...args);
}
Expand Down Expand Up @@ -454,7 +472,11 @@ Spy.prototype.wasCalledWith = function(...args: Array<any>) {
}
const diffInfo = [];
for (let i = 0; i < madeCalls.length; i++) {
const diff = differenceOf(madeCalls[i].arguments, args, this[Symbols.config]);
const diff = differenceOf(
madeCalls[i].arguments,
args,
this[Symbols.config]
);
if (!diff) {
return;
}
Expand Down Expand Up @@ -560,7 +582,10 @@ Spy.prototype.getCallArguments = function(callNr: number = 0): Array<any> {
* @return {any} -> the (argNr + 1)'th call argument
* of the (callNr + 1)'th call.
*/
Spy.prototype.getCallArgument = function(callNr: number = 0, argNr: number = 0): any {
Spy.prototype.getCallArgument = function(
callNr: number = 0,
argNr: number = 0
): any {
return this.getCallArguments(callNr)[argNr];
};

Expand Down Expand Up @@ -605,7 +630,9 @@ Spy.prototype.getCallCount = function(): number {
*
* @return {string} -> The information about made calls.
*/
Spy.prototype.showCallArguments = function(additionalInformation: Array<string> = []): string {
Spy.prototype.showCallArguments = function(
additionalInformation: Array<string> = []
): string {
const madeCalls = this[Symbols.calls];
if (madeCalls.length === 0) {
return `${this[Symbols.name]} was never called!\n`;
Expand Down
19 changes: 16 additions & 3 deletions dist/utils.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
* @param {Function} handler <- Handler function to process all values.
*
*/
const forEach = (arrOrObj: any, handler: (key: string, value: any) => any): void => {
const forEach = (
arrOrObj: any,
handler: (key: string, value: any) => any
): void => {
for (let key in arrOrObj) {
if (arrOrObj.hasOwnProperty(key)) {
handler(key, arrOrObj[key]);
Expand Down Expand Up @@ -136,7 +139,13 @@ const __diff = (
alreadyComparedArray.push(a);
for (let i = 0; i < aKeys.length; i++) {
const key = aKeys[i];
const diffStr = __diff(a[key], b[key], false, useOwnEquals, alreadyComparedArray);
const diffStr = __diff(
a[key],
b[key],
false,
useOwnEquals,
alreadyComparedArray
);
if (diffStr !== undefined) {
return `${initial ? `--> ${key}` : `${key}`} / ${diffStr}`;
}
Expand Down Expand Up @@ -177,7 +186,11 @@ const __diff = (
* @return {string|void} <- information about the difference
* of the provided arguments.
*/
const differenceOf = (a: any, b: any, config: { useOwnEquals: boolean } = { useOwnEquals: true }): string | void => {
const differenceOf = (
a: any,
b: any,
config: { useOwnEquals: boolean } = { useOwnEquals: true }
): string | void => {
return __diff(a, b, true, config.useOwnEquals);
};

Expand Down
15 changes: 12 additions & 3 deletions src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const restoreAttributeForEntry = (value: Object): void => {
*/
function SpyRegistry() {
if (!(this instanceof SpyRegistry)) {
throw new Error('\n\nPlease make sure to use this constructor only with "new" keyword.\n\n');
throw new Error(
'\n\nPlease make sure to use this constructor only with "new" keyword.\n\n'
);
}
this.register = {};
this.persReg = {};
Expand Down Expand Up @@ -73,7 +75,11 @@ SpyRegistry.prototype.restore = function(index: number): void {
*/
SpyRegistry.prototype.push = function(obj: Object, methodName: string): number {
this.registerCount += 1;
this.register[this.registerCount] = { obj, method: obj[methodName], methodName };
this.register[this.registerCount] = {
obj,
method: obj[methodName],
methodName,
};
return this.registerCount;
};

Expand Down Expand Up @@ -103,7 +109,10 @@ SpyRegistry.prototype.getOriginalMethod = function(index: number): any {
* @param {boolean} intoPersReg -> boolean to determine the moving
* direction.
*/
SpyRegistry.prototype.persist = function(index: number, intoPersReg: boolean): void {
SpyRegistry.prototype.persist = function(
index: number,
intoPersReg: boolean
): void {
const fromReg = intoPersReg ? this.register : this.persReg;
const toReg = intoPersReg ? this.persReg : this.register;
const entry = fromReg[index];
Expand Down
Loading

0 comments on commit 22bdb2d

Please sign in to comment.