Skip to content

Commit

Permalink
feat: verify and verifyAll now respect the strictness option
Browse files Browse the repository at this point in the history
  • Loading branch information
NiGhTTraX committed Aug 2, 2022
1 parent 5718a0a commit c93c7be
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
78 changes: 48 additions & 30 deletions src/expectation/repository/flexible-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,6 @@ describe('FlexibleRepository', () => {

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

it('should record calls for unexpected access', () => {
const repo = new FlexibleRepository();
try {
repo.get('foo');
// eslint-disable-next-line no-empty
} catch (e) {}

const callStats: CallStats = {
expected: new Map(),
unexpected: new Map([['foo', [{ arguments: undefined }]]]),
};
expect(repo.getCallStats()).toEqual(callStats);
});

it('should record calls for unexpected call', () => {
const repo = new FlexibleRepository();
repo.add(new NotMatchingExpectation('foo', { value: 23 }));

try {
repo.get('foo').value(1, 2, 3);
// eslint-disable-next-line no-empty
} catch (e) {}

const callStats: CallStats = {
expected: new Map([['foo', [{ arguments: undefined }]]]),
unexpected: new Map([['foo', [{ arguments: [1, 2, 3] }]]]),
};
expect(repo.getCallStats()).toEqual(callStats);
});
});

describe('clearing', () => {
Expand Down Expand Up @@ -339,6 +309,36 @@ describe('FlexibleRepository', () => {

expect(() => repo.get('foo')).toThrow(UnexpectedAccess);
});

it('should record calls for unexpected access', () => {
const repo = new FlexibleRepository();
try {
repo.get('foo');
// eslint-disable-next-line no-empty
} catch (e) {}

const callStats: CallStats = {
expected: new Map(),
unexpected: new Map([['foo', [{ arguments: undefined }]]]),
};
expect(repo.getCallStats()).toEqual(callStats);
});

it('should record calls for unexpected call', () => {
const repo = new FlexibleRepository();
repo.add(new NotMatchingExpectation('foo', { value: 23 }));

try {
repo.get('foo').value(1, 2, 3);
// eslint-disable-next-line no-empty
} catch (e) {}

const callStats: CallStats = {
expected: new Map([['foo', [{ arguments: undefined }]]]),
unexpected: new Map([['foo', [{ arguments: [1, 2, 3] }]]]),
};
expect(repo.getCallStats()).toEqual(callStats);
});
});

describe('medium strict', () => {
Expand All @@ -358,5 +358,23 @@ describe('FlexibleRepository', () => {
expect(value).toBeInstanceOf(Function);
expect(() => value(1, 2, 3)).toThrow(UnexpectedCall);
});

it('should not record the unexpected property access', () => {
const repo = new FlexibleRepository(Strictness.STRICT);

repo.get('foo');

expect(repo.getCallStats().unexpected.size).toEqual(0);
});

it('should record the unexpected call', () => {
const repo = new FlexibleRepository(Strictness.STRICT);

expect(() => repo.get('foo').value(1, 2, 3)).toThrow();

expect(repo.getCallStats().unexpected.get('foo')).toEqual([
{ arguments: [1, 2, 3] },
]);
});
});
});
6 changes: 4 additions & 2 deletions src/expectation/repository/flexible-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ export class FlexibleRepository implements ExpectationRepository {
}

private getValueForUnexpectedAccess(property: Property): ReturnValue {
this.recordUnexpected(property, undefined);

if (this.strictness === Strictness.SUPER_STRICT) {
this.recordUnexpected(property, undefined);

throw new UnexpectedAccess(property, this.getUnmet());
}

return {
value: (...args: unknown[]) => {
this.recordUnexpected(property, args);

throw new UnexpectedCall(property, args, this.getUnmet());
},
};
Expand Down

0 comments on commit c93c7be

Please sign in to comment.