Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
CalionVarduk committed May 26, 2019
2 parents 83df2bd + 0415a37 commit 6cb9f0a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frlluc-mocking",
"version": "0.9.4",
"version": "0.9.5",
"description": "A simple mocking framework targetting TypeScript",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
35 changes: 18 additions & 17 deletions src/core/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@ import { IMock } from './mock.interface';

let GLOBAL_INVOCATION_NO = 0;

type InvocationCounter = {
value: number;
};

function createMethodInfo(type: MockedInfoType, data: IInvocationData[], counter: InvocationCounter): IMockedMethodInfo {
function createMethodInfo(type: MockedInfoType, data: IInvocationData[]): IMockedMethodInfo {
return Object.freeze({
type: type,
get count(): number {
return counter.value;
return data.length;
},
getData(no: number): IInvocationData | null {
return data[no] || null;
},
clear(): void {
data.splice(0, data.length);
}
});
}

function createFunctionMock(func: Function, subject: any, data: IInvocationData[], counter: InvocationCounter): Function {
function createFunctionMock(func: Function, subject: any, data: IInvocationData[]): Function {
return function() {
const result = func.bind(subject)(...arguments);
data.push(Object.freeze({
no: counter.value++,
no: data.length,
globalNo: GLOBAL_INVOCATION_NO++,
timestamp: new Date().valueOf(),
result: Object.freeze(result),
Expand All @@ -41,9 +40,8 @@ function createMethodMock(
subject: any, func: Function, name: string): Function {

const data: IInvocationData[] = [];
const counter: InvocationCounter = { value: 0 };
invocationCache[name] = createMethodInfo(MockedInfoType.Method, data, counter);
return createFunctionMock(func, subject, data, counter);
invocationCache[name] = createMethodInfo(MockedInfoType.Method, data);
return createFunctionMock(func, subject, data);
}

function createPropertyMock(
Expand All @@ -58,15 +56,13 @@ function createPropertyMock(
const result: { getter?(): any; setter?(value: any): void } = {};
if (prop.get) {
const data: IInvocationData[] = [];
const counter: InvocationCounter = { value: 0 };
info.get = createMethodInfo(MockedInfoType.PropertyGetter, data, counter);
result.getter = createFunctionMock(prop.get, subject, data, counter) as () => any;
info.get = createMethodInfo(MockedInfoType.PropertyGetter, data);
result.getter = createFunctionMock(prop.get, subject, data) as () => any;
}
if (prop.set) {
const data: IInvocationData[] = [];
const counter: InvocationCounter = { value: 0 };
info.set = createMethodInfo(MockedInfoType.PropertySetter, data, counter);
result.setter = createFunctionMock(prop.set, subject, data, counter) as (value: any) => void;
info.set = createMethodInfo(MockedInfoType.PropertySetter, data);
result.setter = createFunctionMock(prop.set, subject, data) as (value: any) => void;
}
invocationCache[name] = Object.freeze(info);
return result;
Expand Down Expand Up @@ -110,3 +106,8 @@ export function mock<T>(setup: Partial<T>): IMock<T> {
export function resetGlobalMockInvocationNo(): void {
GLOBAL_INVOCATION_NO = 0;
}

/** Returns current global invocation bo counter. */
export function getGlobalMockInvocationNo(): number {
return GLOBAL_INVOCATION_NO;
}
2 changes: 2 additions & 0 deletions src/core/mocked-method-info.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export interface IMockedMethodInfo {
* @returns invocation data
* */
getData(invocationNo: number): IInvocationData | null;
/** Clears all stored invocation data. */
clear(): void;
}
17 changes: 16 additions & 1 deletion src/tests/mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MockedInfoType } from '../core/mocked-info-type.enum';
import { IMockedMethodInfo } from '../core/mocked-method-info.interface';
import { IMockedPropertyInfo } from '../core/mocked-property-info.interface';
import { IMock } from '../core/mock.interface';
import { mock, resetGlobalMockInvocationNo } from '../core/mock';
import { mock, resetGlobalMockInvocationNo, getGlobalMockInvocationNo } from '../core/mock';

abstract class Test {
public abstract field: string;
Expand Down Expand Up @@ -488,6 +488,21 @@ test('data global no should increment properly',
expect(readonlyPropertyInfo.get!.getData(0)!.globalNo).toBe(2);
expect(voidMethodInfo.getData(0)!.globalNo).toBe(1);
expect(returningMethodInfo.getData(0)!.globalNo).toBe(3);
expect(getGlobalMockInvocationNo()).toBe(4);
}
);

test('mocked method info clear should remove all invocation data',
() => {
const sut = mock<Test>({
set property(value: number) { return; }
});
const info = sut.getMemberInfo('property') as IMockedPropertyInfo;
sut.subject.property = 0;
sut.subject.property = 1;
sut.subject.property = 2;
info.set!.clear();
expect(info.set!.count).toBe(0);
}
);

Expand Down

0 comments on commit 6cb9f0a

Please sign in to comment.