Skip to content

Commit

Permalink
fix: Don't record property call stats for function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
NiGhTTraX committed Aug 10, 2022
1 parent 0975e52 commit de204a5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
17 changes: 16 additions & 1 deletion src/expectation/repository/flexible-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('FlexibleRepository', () => {
expect(repo.getCallStats()).toEqual(callStats);
});

it('it should record function calls', () => {
it('it should record method calls', () => {
const repo = new FlexibleRepository();
const expectation = new MatchingCallExpectation('foo', { value: 23 });
repo.add(expectation);
Expand All @@ -190,6 +190,21 @@ describe('FlexibleRepository', () => {

expect(repo.getCallStats()).toEqual(callStats);
});

it('it should record function calls', () => {
const repo = new FlexibleRepository();
const expectation = new MatchingCallExpectation(ApplyProp, { value: 23 });
repo.add(expectation);

repo.get(ApplyProp).value(1, 2);

const callStats: CallStats = {
expected: new Map([[ApplyProp, [{ arguments: [1, 2] }]]]),
unexpected: new Map(),
};

expect(repo.getCallStats()).toEqual(callStats);
});
});

describe('clearing', () => {
Expand Down
68 changes: 43 additions & 25 deletions src/expectation/repository/flexible-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,57 @@ export class FlexibleRepository implements ExpectationRepository {
const expectations = this.expectations.get(property);

if (expectations && expectations.length) {
// We record that an expected property access has happened, but an
// unexpected call could still happen later.
return this.handlePropertyWithMatchingExpectations(
property,
expectations
);
}

return this.handlePropertyWithNoExpectations(property);
}

private handlePropertyWithMatchingExpectations = (
property: Property,
expectations: CountableExpectation[]
) => {
// Avoid recording call stats for function calls, since the property is an
// internal detail.
if (property !== ApplyProp) {
// An unexpected call could still happen later, if the property returns a
// function that will not match the given args.
this.recordExpected(property, undefined);
}

const propertyExpectation = expectations.find((e) =>
e.expectation.matches(undefined)
);
const propertyExpectation = expectations.find((e) =>
e.expectation.matches(undefined)
);

if (propertyExpectation) {
this.countAndConsume(propertyExpectation);
if (propertyExpectation) {
this.countAndConsume(propertyExpectation);

return propertyExpectation.expectation.returnValue;
}
return propertyExpectation.expectation.returnValue;
}

return {
value: (...args: any[]) => {
const callExpectation = expectations.find((e) =>
e.expectation.matches(args)
);
return {
value: (...args: any[]) => {
const callExpectation = expectations.find((e) =>
e.expectation.matches(args)
);

if (callExpectation) {
this.recordExpected(property, args);
this.countAndConsume(callExpectation);
if (callExpectation) {
this.recordExpected(property, args);
this.countAndConsume(callExpectation);

// TODO: this is duplicated in stub
return returnOrThrow(callExpectation.expectation.returnValue);
}
// TODO: this is duplicated in stub
return returnOrThrow(callExpectation.expectation.returnValue);
}

return this.getValueForUnexpectedCall(property, args);
},
};
}
return this.getValueForUnexpectedCall(property, args);
},
};
};

private handlePropertyWithNoExpectations = (property: Property) => {
switch (property) {
case 'toString':
return { value: () => 'mock' };
Expand All @@ -106,7 +124,7 @@ export class FlexibleRepository implements ExpectationRepository {
default:
return this.getValueForUnexpectedAccess(property);
}
}
};

getAllProperties(): Property[] {
return Array.from(this.expectations.keys());
Expand Down

0 comments on commit de204a5

Please sign in to comment.