Skip to content

Commit

Permalink
fix: Don't mark toString accesses as unexpected
Browse files Browse the repository at this point in the history
  • Loading branch information
NiGhTTraX committed May 5, 2020
1 parent 9a06ae3 commit 20247b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
19 changes: 16 additions & 3 deletions src/base-repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expectation } from './expectation';
import { ApplyProp, Expectation } from './expectation';
import { CallMap, ExpectationRepository } from './expectation-repository';

export type CountableExpectation = {
Expand Down Expand Up @@ -70,8 +70,21 @@ export abstract class BaseRepository implements ExpectationRepository {
};
}

this.recordUnexpected(property, undefined);
return this.getValueForUnexpectedAccess(property);
switch (property) {
case 'toString':
return () => 'mock';
case '@@toStringTag':
case Symbol.toStringTag:
return 'mock';
case ApplyProp:
return (...args: any[]) => {
this.recordUnexpected(property, args);
return this.getValueForUnexpectedCall(property, args);
};
default:
this.recordUnexpected(property, undefined);
return this.getValueForUnexpectedAccess(property);
}
}

getCallStats() {
Expand Down
18 changes: 1 addition & 17 deletions src/strong-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BaseRepository, CountableExpectation } from './base-repository';
import { UnexpectedAccess, UnexpectedCall } from './errors';
import { ApplyProp } from './expectation';

/**
* Throw if no expectation matches.
Expand All @@ -19,26 +18,11 @@ export class StrongRepository extends BaseRepository {
}
}

private static readonly TO_STRING_VALUE = 'strong-mock';

protected getValueForUnexpectedCall(property: PropertyKey, args: any[]) {
throw new UnexpectedCall(property, args, this.getUnmet());
}

protected getValueForUnexpectedAccess(property: PropertyKey) {
// TODO: abstract the toString logic away (maybe move it to the base repo)
switch (property) {
case 'toString':
return () => StrongRepository.TO_STRING_VALUE;
case '@@toStringTag':
case Symbol.toStringTag:
return StrongRepository.TO_STRING_VALUE;
case ApplyProp:
return (...args: any[]) => {
throw new UnexpectedCall(ApplyProp, args, this.getUnmet());
};
default:
throw new UnexpectedAccess(property, this.getUnmet());
}
throw new UnexpectedAccess(property, this.getUnmet());
}
}
18 changes: 4 additions & 14 deletions src/weak-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@ import { ExpectationRepository } from './expectation-repository';
*/
export class WeakRepository extends BaseRepository
implements ExpectationRepository {
private static TO_STRING_VALUE = 'weak-mock';

private repeating = new Map<PropertyKey, boolean>();

protected getValueForUnexpectedCall = (): any => null;

protected getValueForUnexpectedAccess(property: PropertyKey): any {
switch (property) {
case 'toString':
return () => WeakRepository.TO_STRING_VALUE;
case '@@toStringTag':
case Symbol.toStringTag:
return WeakRepository.TO_STRING_VALUE;
default:
return (...args: any[]) => {
this.recordExpected(property, args);
return (...args: any[]) => {
this.recordExpected(property, args);

return null;
};
}
return null;
};
}

protected consumeExpectation(expectation: CountableExpectation): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/when.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('when', () => {
});

it('should be stringifiable', () => {
expect(instance(mock<() => void>()).toString()).toEqual('strong-mock');
expect(instance(mock<() => void>()).toString()).toEqual('mock');
});

describe('ignoring arguments', () => {
Expand Down

0 comments on commit 20247b6

Please sign in to comment.