Add specs for the new toHaveSpyInteractions matcher
This commit is contained in:
22
spec/core/integration/MatchersSpec.js
Normal file → Executable file
22
spec/core/integration/MatchersSpec.js
Normal file → Executable file
@@ -625,6 +625,28 @@ describe('Matchers (Integration)', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('toHaveSpyInteractions', function() {
|
||||
let spyObj;
|
||||
beforeEach(function() {
|
||||
spyObj = env.createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||
spyObj.otherMethod = function() {};
|
||||
});
|
||||
|
||||
verifyPasses(function(env) {
|
||||
spyObj.spyA();
|
||||
env.expect(spyObj).toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
verifyFails(function(env) {
|
||||
env.expect(spyObj).toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
verifyFails(function(env) {
|
||||
spyObj.otherMethod();
|
||||
env.expect(spyObj).toHaveSpyInteractions();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toMatch', function() {
|
||||
verifyPasses(function(env) {
|
||||
env.expect('foo').toMatch(/oo$/);
|
||||
|
||||
57
spec/core/matchers/toHaveSpyInteractions.js
Executable file
57
spec/core/matchers/toHaveSpyInteractions.js
Executable file
@@ -0,0 +1,57 @@
|
||||
describe('toHaveSpyInteractions', () => {
|
||||
let spyObj;
|
||||
beforeEach(() => {
|
||||
spyObj = jasmineUnderTest.createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||
spyObj.otherMethod = function() {};
|
||||
});
|
||||
|
||||
it('detects spy interactions', () => {
|
||||
spyObj.spyA();
|
||||
expect(spyObj).toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
it('detects multiple spy interactions', () => {
|
||||
spyObj.spyA();
|
||||
spyObj.spyB();
|
||||
spyObj.spyA();
|
||||
expect(spyObj).toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
it('detects no spy interactions', () => {
|
||||
expect(spyObj).not.toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
it('ignores non-observed spy object interactions', () => {
|
||||
spyObj.otherMethod();
|
||||
expect(spyObj).not.toHaveSpyInteractions();
|
||||
});
|
||||
|
||||
[true, 123, 'string'].forEach(testValue => {
|
||||
it(`throws error if a non-object (${testValue}) is passed`, () => {
|
||||
expect(() => {
|
||||
expect(true).toHaveSpyInteractions();
|
||||
}).toThrowError(Error, /Expected a spy object, but got/);
|
||||
});
|
||||
});
|
||||
|
||||
[['argument'], [false, 0]].forEach(testValue => {
|
||||
it(`throws error if arguments (${testValue}) are passed`, () => {
|
||||
expect(() => {
|
||||
expect(spyObj).toHaveSpyInteractions(...testValue);
|
||||
}).toThrowError(Error, /Does not take arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
it('throws error if spy object has no spies', () => {
|
||||
const newSpyObj = jasmine.createSpyObj('OtherClass', ['method']);
|
||||
// Removing spy since spy objects cannot be created without spies.
|
||||
newSpyObj.method = function() {};
|
||||
|
||||
expect(() => {
|
||||
expect(newSpyObj).toHaveSpyInteractions();
|
||||
}).toThrowError(
|
||||
Error,
|
||||
/Expected a spy object with spies, but object has no spies/
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user