daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Nov 18th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Timer, Clock } = require('../client/clockLogic.js');
  2.  
  3. jest.useFakeTimers();
  4.  
  5. describe('clockLogic', () => {
  6.   describe('Timer', () => {
  7.     it('adds a callback to the callbacks array with the addCallback method', () => {
  8.       let timer = new Timer();
  9.       let func = jest.fn();
  10.  
  11.       timer.addCallback(func);
  12.       expect(timer.callbacks).toEqual(expect.arrayContaining([func]));
  13.     });
  14.     it('calls callbacks once every second', () => {
  15.       let timer = new Timer();
  16.       let func = jest.fn();
  17.  
  18.       timer.addCallback(func);
  19.       jest.runTimersToTime(2000);
  20.       expect(func.mock.calls.length).toBe(2);
  21.     });
  22.   });
  23.   describe('Clock', () => {
  24.     it('allows the user to set the time', () => {
  25.       let clock = new Clock()
  26.       clock.setTime('00:00');
  27.  
  28.       expect(clock.time.hour).toBe('00');
  29.       expect(clock.time.minute).toBe('00');
  30.       expect(clock.time.second).toBe('00');
  31.     });
  32.     it('sets the time to the current time if no input is given', () => {
  33.       let clock = new Clock();
  34.       let now = new Date().toString().split(' ')[4];
  35.      
  36.       clock.setTime();
  37.       expect(clock.time.hour).toBe(now.split(':')[0]);
  38.       expect(clock.time.minute).toBe(now.split(':')[1]);
  39.       expect(clock.time.second).toBe(now.split(':')[2]);
  40.     });
  41.     it('adds an alarm to the alarms array with the setAlarm method', () => {
  42.       let clock = new Clock();
  43.      
  44.       expect(clock.alarms.length).toBe(0);
  45.       clock.setAlarm('12:00');
  46.       expect(clock.alarms.length).toBe(1);
  47.     });
  48.     it('calls updateTime every second', () => {
  49.       let updateTimeHolder = Clock.prototype.updateTime;
  50.       Clock.prototype.updateTime = jest.fn();
  51.       let renderClockHolder = Clock.prototype.renderClock;
  52.       Clock.prototype.renderClock = jest.fn();
  53.       let clock = new Clock();
  54.      
  55.       clock.setTime('00:00');
  56.       jest.runTimersToTime(2000);
  57.       expect(clock.updateTime.mock.calls.length).toBe(2);
  58.  
  59.       Clock.prototype.updateTime = updateTimeHolder;
  60.       Clock.prototype.renderClock = renderClockHolder;
  61.     });
  62.     it('Clock.time maintains the correct time', () => {
  63.       let renderClockHolder = Clock.prototype.renderClock;
  64.       Clock.prototype.renderClock = jest.fn();
  65.       let clock = new Clock();
  66.  
  67.       clock.setTime('00:00');
  68.       jest.runTimersToTime(1000);
  69.       expect(clock.time.second).toEqual('01');
  70.       jest.runTimersToTime(60000);
  71.       expect(clock.time.minute).toEqual('01');
  72.       jest.runTimersToTime(3600000);
  73.       expect(clock.time.hour).toEqual('01');
  74.  
  75.       Clock.prototype.renderClock = renderClockHolder;
  76.     });
  77.     it('executes an alarm at the given time', () => {
  78.       let triggerAlarmHolder = Clock.prototype.triggerAlarm;
  79.       Clock.prototype.triggerAlarm = jest.fn();
  80.       let renderClockHolder = Clock.prototype.renderClock;
  81.       Clock.prototype.renderClock = jest.fn();
  82.       let clock = new Clock();
  83.  
  84.       clock.setTime('00:00');
  85.       clock.setAlarm('00:01');
  86.       jest.runTimersToTime(59000);
  87.       expect(clock.triggerAlarm).not.toBeCalled();
  88.       jest.runTimersToTime(60000);
  89.       expect(clock.triggerAlarm).toBeCalled();
  90.  
  91.       Clock.prototype.triggerAlarm = triggerAlarmHolder;
  92.       Clock.prototype.renderClock = renderClockHolder;
  93.     })
  94.   });
  95. });
RAW Paste Data
Top