all files / src/events/__tests__/ silenceEvents.spec.js

97.78% Statements 44/45
100% Branches 4/4
87.5% Functions 7/8
97.62% Lines 41/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                        
import expect, {createSpy} from 'expect';
import silenceEvents from '../silenceEvents';
 
describe('silenceEvents', () => {
  it('should return a function', () => {
    expect(silenceEvents())
      .toExist()
      .toBeA('function');
  });
 
  it('should return pass all args if first arg is not event', () => {
    const spy = createSpy();
    const silenced = silenceEvents(spy);
 
    silenced(1, 2, 3);
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith(1, 2, 3);
    spy.restore();
 
    silenced('foo', 'bar');
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('foo', 'bar');
    spy.restore();
 
    silenced({value: 10}, false);
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith({value: 10}, false);
    spy.restore();
  });
 
  it('should return pass other args if first arg is event', () => {
    const spy = createSpy();
    const silenced = silenceEvents(spy);
    const event = {
      preventDefault: () => null,
      stopPropagation: () => null
    };
 
    silenced(event, 1, 2, 3);
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith(1, 2, 3);
    spy.restore();
 
    silenced(event, 'foo', 'bar');
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('foo', 'bar');
    spy.restore();
 
    silenced(event, {value: 10}, false);
    expect(spy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith({value: 10}, false);
    spy.restore();
  });
 
  it('should silence event', () => {
    const spy = createSpy();
    const preventDefault = createSpy();
    const stopPropagation = createSpy();
    const event = {
      preventDefault,
      stopPropagation
    };
 
    silenceEvents(spy)(event);
    expect(preventDefault).toHaveBeenCalled();
    expect(stopPropagation).toNotHaveBeenCalled();
    expect(spy).toHaveBeenCalled();
  });
});