test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var every = require('./index').every;
  2. var should = require('should');
  3. var sinon = require('sinon');
  4. describe('Timers', function() {
  5. it('string parse', function() {
  6. every('10ms').time.should.be.eql(10);
  7. every('2s').time.should.be.eql(2000);
  8. every('2.5m').time.should.be.eql(150000);
  9. every('2h').time.should.be.eql(7200000);
  10. every('2d').time.should.be.eql(172800000);
  11. });
  12. it('string parse format', function() {
  13. every('2hour').time.should.be.eql(7200000);
  14. every('2 hour').time.should.be.eql(7200000);
  15. every('2 hours').time.should.be.eql(7200000);
  16. every(' 2 day ').time.should.be.eql(172800000);
  17. every('2days ').time.should.be.eql(172800000);
  18. should.not.exist(every(' 2 unknown').time);
  19. });
  20. it('every', function(done) {
  21. var spy = sinon.spy();
  22. var e = every('50ms').do(spy);
  23. setTimeout(function() {
  24. spy.calledOn(e);
  25. spy.callCount.should.be.eql(1);
  26. }, 55);
  27. setTimeout(function() {
  28. spy.callCount.should.be.eql(2);
  29. }, 105);
  30. setTimeout(function() {
  31. spy.callCount.should.be.eql(3);
  32. done();
  33. }, 155);
  34. });
  35. it('every stop', function(done) {
  36. var spy = sinon.spy();
  37. var e = every('50ms').do(spy);
  38. setTimeout(function() {
  39. spy.calledOn(e);
  40. spy.callCount.should.be.eql(1);
  41. e.stop();
  42. }, 55);
  43. setTimeout(function() {
  44. spy.callCount.should.be.eql(1);
  45. done();
  46. }, 105);
  47. });
  48. });