|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +/** |
| 3 | + * Dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +if (typeof require !== 'undefined') { |
| 7 | + expect = require('expect.js'); |
| 8 | + ms = require('./'); |
| 9 | +} |
| 10 | + |
| 11 | +// strings |
| 12 | + |
| 13 | +describe('ms(string)', function() { |
| 14 | + it('should not throw an error', function() { |
| 15 | + expect(function() { |
| 16 | + ms('1m'); |
| 17 | + }).to.not.throwError(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should preserve ms', function() { |
| 21 | + expect(ms('100')).to.be(100); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should convert from m to ms', function() { |
| 25 | + expect(ms('1m')).to.be(60000); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should convert from h to ms', function() { |
| 29 | + expect(ms('1h')).to.be(3600000); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should convert d to ms', function() { |
| 33 | + expect(ms('2d')).to.be(172800000); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should convert s to ms', function() { |
| 37 | + expect(ms('1s')).to.be(1000); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should convert ms to ms', function() { |
| 41 | + expect(ms('100ms')).to.be(100); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should work with decimals', function() { |
| 45 | + expect(ms('1.5h')).to.be(5400000); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should work with multiple spaces', function() { |
| 49 | + expect(ms('1 s')).to.be(1000); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return NaN if invalid', function() { |
| 53 | + expect(isNaN(ms('☃'))).to.be(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should be case-insensitive', function() { |
| 57 | + expect(ms('1.5H')).to.be(5400000); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should work with numbers starting with .', function() { |
| 61 | + expect(ms('.5ms')).to.be(0.5); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +// long strings |
| 66 | + |
| 67 | +describe('ms(long string)', function() { |
| 68 | + it('should not throw an error', function() { |
| 69 | + expect(function() { |
| 70 | + ms('53 milliseconds'); |
| 71 | + }).to.not.throwError(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should convert milliseconds to ms', function() { |
| 75 | + expect(ms('53 milliseconds')).to.be(53); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should convert msecs to ms', function() { |
| 79 | + expect(ms('17 msecs')).to.be(17); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should convert sec to ms', function() { |
| 83 | + expect(ms('1 sec')).to.be(1000); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should convert from min to ms', function() { |
| 87 | + expect(ms('1 min')).to.be(60000); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should convert from hr to ms', function() { |
| 91 | + expect(ms('1 hr')).to.be(3600000); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should convert days to ms', function() { |
| 95 | + expect(ms('2 days')).to.be(172800000); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should work with decimals', function() { |
| 99 | + expect(ms('1.5 hours')).to.be(5400000); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +// numbers |
| 104 | + |
| 105 | +describe('ms(number, { long: true })', function() { |
| 106 | + it('should not throw an error', function() { |
| 107 | + expect(function() { |
| 108 | + ms(500, { long: true }); |
| 109 | + }).to.not.throwError(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should support milliseconds', function() { |
| 113 | + expect(ms(500, { long: true })).to.be('500 ms'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should support seconds', function() { |
| 117 | + expect(ms(1000, { long: true })).to.be('1 second'); |
| 118 | + expect(ms(1200, { long: true })).to.be('1 second'); |
| 119 | + expect(ms(10000, { long: true })).to.be('10 seconds'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should support minutes', function() { |
| 123 | + expect(ms(60 * 1000, { long: true })).to.be('1 minute'); |
| 124 | + expect(ms(60 * 1200, { long: true })).to.be('1 minute'); |
| 125 | + expect(ms(60 * 10000, { long: true })).to.be('10 minutes'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should support hours', function() { |
| 129 | + expect(ms(60 * 60 * 1000, { long: true })).to.be('1 hour'); |
| 130 | + expect(ms(60 * 60 * 1200, { long: true })).to.be('1 hour'); |
| 131 | + expect(ms(60 * 60 * 10000, { long: true })).to.be('10 hours'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should support days', function() { |
| 135 | + expect(ms(24 * 60 * 60 * 1000, { long: true })).to.be('1 day'); |
| 136 | + expect(ms(24 * 60 * 60 * 1200, { long: true })).to.be('1 day'); |
| 137 | + expect(ms(24 * 60 * 60 * 10000, { long: true })).to.be('10 days'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should round', function() { |
| 141 | + expect(ms(234234234, { long: true })).to.be('3 days'); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +// numbers |
| 146 | + |
| 147 | +describe('ms(number)', function() { |
| 148 | + it('should not throw an error', function() { |
| 149 | + expect(function() { |
| 150 | + ms(500); |
| 151 | + }).to.not.throwError(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should support milliseconds', function() { |
| 155 | + expect(ms(500)).to.be('500ms'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should support seconds', function() { |
| 159 | + expect(ms(1000)).to.be('1s'); |
| 160 | + expect(ms(10000)).to.be('10s'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should support minutes', function() { |
| 164 | + expect(ms(60 * 1000)).to.be('1m'); |
| 165 | + expect(ms(60 * 10000)).to.be('10m'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should support hours', function() { |
| 169 | + expect(ms(60 * 60 * 1000)).to.be('1h'); |
| 170 | + expect(ms(60 * 60 * 10000)).to.be('10h'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should support days', function() { |
| 174 | + expect(ms(24 * 60 * 60 * 1000)).to.be('1d'); |
| 175 | + expect(ms(24 * 60 * 60 * 10000)).to.be('10d'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should round', function() { |
| 179 | + expect(ms(234234234)).to.be('3d'); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +// invalid inputs |
| 184 | + |
| 185 | +describe('ms(invalid inputs)', function() { |
| 186 | + it('should throw an error, when ms("")', function() { |
| 187 | + expect(function() { |
| 188 | + ms(''); |
| 189 | + }).to.throwError(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should throw an error, when ms(undefined)', function() { |
| 193 | + expect(function() { |
| 194 | + ms(undefined); |
| 195 | + }).to.throwError(); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should throw an error, when ms(null)', function() { |
| 199 | + expect(function() { |
| 200 | + ms(null); |
| 201 | + }).to.throwError(); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should throw an error, when ms([])', function() { |
| 205 | + expect(function() { |
| 206 | + ms([]); |
| 207 | + }).to.throwError(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should throw an error, when ms({})', function() { |
| 211 | + expect(function() { |
| 212 | + ms({}); |
| 213 | + }).to.throwError(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should throw an error, when ms(NaN)', function() { |
| 217 | + expect(function() { |
| 218 | + ms(NaN); |
| 219 | + }).to.throwError(); |
| 220 | + }); |
| 221 | +}); |
0 commit comments