Use custom equality testers in Spy#withArgs

Fixes #1836.
This commit is contained in:
Steve Gravrock
2021-11-15 18:30:43 -08:00
parent 8e74529631
commit 2a049015b0
6 changed files with 152 additions and 61 deletions
+52
View File
@@ -3413,4 +3413,56 @@ describe('Env integration', function() {
done();
});
});
it('uses custom equality testers in Spy#withArgs', async function() {
env.it('a spec', function() {
const createSpySpy = env.createSpy('via createSpy');
const spiedOn = { foo: function() {} };
env.spyOn(spiedOn, 'foo');
const spyObj = env.createSpyObj('spyObj', ['foo']);
const spiedOnAllFuncs = { foo: function() {} };
env.spyOnAllFunctions(spiedOnAllFuncs);
for (spy of [
createSpySpy,
spiedOn.foo,
spyObj.foo,
spiedOnAllFuncs.foo
]) {
spy.and.returnValue('default strategy');
spy.withArgs(42).and.returnValue('custom strategy');
}
env.addCustomEqualityTester(function(a, b) {
if ((a === 'x' && b === 42) || (a === 42 && b === 'x')) {
return true;
}
});
env
.expect(createSpySpy('x'))
.withContext('createSpy')
.toEqual('custom strategy');
env
.expect(spiedOn.foo('x'))
.withContext('spyOn')
.toEqual('custom strategy');
env
.expect(spyObj.foo('x'))
.withContext('createSpyObj')
.toEqual('custom strategy');
env
.expect(spiedOnAllFuncs.foo('x'))
.withContext('spyOnAllFunctions')
.toEqual('custom strategy');
});
let failedExpectations;
env.addReporter({
specDone: r => (failedExpectations = r.failedExpectations)
});
await env.execute();
expect(failedExpectations).toEqual([]);
});
});