| 1 | describe('parseInterval unit tests', function() { |
| 2 | |
| 3 | it('parses milliseconds', function () { |
| 4 | assert.equal(htmx.parseInterval('100ms'), 100) |
| 5 | assert.equal(htmx.parseInterval('1ms'), 1) |
| 6 | assert.equal(htmx.parseInterval('5000ms'), 5000) |
| 7 | }) |
| 8 | |
| 9 | it('parses seconds', function () { |
| 10 | assert.equal(htmx.parseInterval('1s'), 1000) |
| 11 | assert.equal(htmx.parseInterval('2s'), 2000) |
| 12 | assert.equal(htmx.parseInterval('10s'), 10000) |
| 13 | }) |
| 14 | |
| 15 | it('parses minutes', function () { |
| 16 | assert.equal(htmx.parseInterval('1m'), 60000) |
| 17 | assert.equal(htmx.parseInterval('2m'), 120000) |
| 18 | assert.equal(htmx.parseInterval('5m'), 300000) |
| 19 | }) |
| 20 | |
| 21 | it('parses decimal values', function () { |
| 22 | assert.equal(htmx.parseInterval('1.5s'), 1500) |
| 23 | assert.equal(htmx.parseInterval('0.5s'), 500) |
| 24 | assert.equal(htmx.parseInterval('2.5m'), 150000) |
| 25 | }) |
| 26 | |
| 27 | it('defaults to milliseconds when no unit specified', function () { |
| 28 | assert.equal(htmx.parseInterval('100'), 100) |
| 29 | assert.equal(htmx.parseInterval('1000'), 1000) |
| 30 | assert.equal(htmx.parseInterval('5'), 5) |
| 31 | }) |
| 32 | |
| 33 | it('handles decimal without unit', function () { |
| 34 | assert.equal(htmx.parseInterval('1.5'), 1.5) |
| 35 | assert.equal(htmx.parseInterval('100.25'), 100.25) |
| 36 | }) |
| 37 | |
| 38 | it('returns undefined for invalid input', function () { |
| 39 | assert.equal(htmx.parseInterval('abc'), undefined) |
| 40 | assert.equal(htmx.parseInterval('invalid'), undefined) |
| 41 | assert.equal(htmx.parseInterval(''), undefined) |
| 42 | }) |
| 43 | |
| 44 | it('returns undefined for null', function () { |
| 45 | assert.equal(htmx.parseInterval(null), undefined) |
| 46 | }) |
| 47 | |
| 48 | it('returns undefined for undefined', function () { |
| 49 | assert.equal(htmx.parseInterval(undefined), undefined) |
| 50 | }) |
| 51 | |
| 52 | it('returns undefined for invalid unit', function () { |
| 53 | assert.equal(htmx.parseInterval('100h'), undefined) |
| 54 | assert.equal(htmx.parseInterval('50d'), undefined) |
| 55 | }) |
| 56 | |
| 57 | it('returns undefined for negative values', function () { |
| 58 | assert.equal(htmx.parseInterval('-100ms'), undefined) |
| 59 | assert.equal(htmx.parseInterval('-1s'), undefined) |
| 60 | }) |
| 61 | |
| 62 | it('handles zero', function () { |
| 63 | assert.equal(htmx.parseInterval('0'), 0) |
| 64 | assert.equal(htmx.parseInterval('0ms'), 0) |
| 65 | assert.equal(htmx.parseInterval('0s'), 0) |
| 66 | }) |
| 67 | |
| 68 | it('handles very large numbers', function () { |
| 69 | assert.equal(htmx.parseInterval('999999ms'), 999999) |
| 70 | assert.equal(htmx.parseInterval('1000s'), 1000000) |
| 71 | }) |
| 72 | |
| 73 | it('returns undefined for strings with spaces', function () { |
| 74 | assert.equal(htmx.parseInterval('100 ms'), undefined) |
| 75 | assert.equal(htmx.parseInterval(' 100ms'), undefined) |
| 76 | assert.equal(htmx.parseInterval('100ms '), undefined) |
| 77 | }) |
| 78 | |
| 79 | }); |