// only spies on the function but does not change its behavior

// SPY: basic anonymous spy example

function testMe(callback) {
  callback();
}

// testing function
describe('testMe funciton', () => {
  it('should call the callback', ()=> {
    let callbackSpy = sinon.spy(); // anonymous spy
    testMe(callbackSpy);
    expect(callbackSpy).to.have.been.calledOnce;
    
    callbackSpy.restore();
  })
})

// SPY: wrap an existing method

const user = {
  // ...
  setName: function(name) {
    this.name = name;
  }
}

// testing function

describe('setName function', () => {
  it('should be called with name', () => {
    let setNameSpy = sinon.spy(user, 'setName');
    
    user.setName('Harry Potter');
    
    expect(setNameSpy).to.have.been.calledOnce;
    expect(setNameSpy).to.have.been.calledWith('Harry Potter');
  
    // important! Remove the spy at end to prevent future errors
    setNameSpy.restore();
  })
})