Best JavaScript code snippet using jest-extended
midnight.test.js
Source:midnight.test.js
1import Midnight from './midnight'2import Timecop from './timecop'3expect.extend({4 toBeDate(received, year, month, day){5 const pass = (6 received.getFullYear() === year &&7 received.getMonth() + 1 === month &&8 received.getDate() === day9 )10 return {11 pass,12 message(){13 const expected = `${14 `${year}`.padStart(4, '0')15 }-${16 `${month}`.padStart(2, '0')17 }-${18 `${day}`.padStart(2, '0')19 }`20 if(pass){21 return `${received} and ${expected} are same date`22 }else{23 return `${received} and ${expected} are not same date`24 }25 }26 }27 },28 toInvalidDate(received){29 const pass = received instanceof Date && received.toString() === 'Invalid Date'30 return {31 pass,32 message(){33 if(pass){34 return `${received} is Invalid Date`35 }else{36 return `${received} is not Invalid Date`37 }38 }39 }40 },41})42describe('constructor', () => {43 test('no argument', () => {44 const date = new Midnight()45 const now = new Date()46 const year = now.getFullYear()47 const month = now.getMonth() + 148 const day = now.getDate()49 expect(date).toBeDate(year, month, day)50 })51 test('one argument (number)', () => {52 expect(new Midnight(1)).toBeDate(1, 1, 1)53 expect(new Midnight(99)).toBeDate(99, 1, 1)54 expect(new Midnight(100)).toBeDate(100, 1, 1)55 expect(new Midnight(1900)).toBeDate(1900, 1, 1)56 expect(new Midnight(2000)).toBeDate(2000, 1, 1)57 expect(new Midnight(2100)).toBeDate(2100, 1, 1)58 })59 test('one argument (string)', () => {60 expect(new Midnight('2018/05/15')).toBeDate(2018, 5, 15)61 expect(new Midnight('2018-05-15')).toBeDate(2018, 5, 15)62 expect(new Midnight('2018/05')).toBeDate(2018, 5, 1)63 expect(new Midnight('2018-05')).toBeDate(2018, 5, 1)64 expect(new Midnight('2018')).toBeDate(2018, 1, 1)65 })66 test('one argument (date)', () => {67 const date = new Date('2018-05-15')68 expect(new Midnight(date)).toBeDate(2018, 5, 15)69 })70 test('year and month', () => {71 expect(new Midnight(2018, 0)).toBeDate(2017, 12, 1)72 expect(new Midnight(2018, 1)).toBeDate(2018, 1, 1)73 expect(new Midnight(2018, 5)).toBeDate(2018, 5, 1)74 expect(new Midnight(2018, 12)).toBeDate(2018, 12, 1)75 expect(new Midnight(2018, 13)).toBeDate(2019, 1, 1)76 })77 test('year, month and day', () => {78 expect(new Midnight(2018, 5, 0)).toBeDate(2018, 4, 30)79 expect(new Midnight(2018, 5, 1)).toBeDate(2018, 5, 1)80 expect(new Midnight(2018, 5, 31)).toBeDate(2018, 5, 31)81 expect(new Midnight(2018, 5, 32)).toBeDate(2018, 6, 1)82 expect(new Midnight(2018, 0, 15)).toBeDate(2017, 12, 15)83 expect(new Midnight(2018, 1, 15)).toBeDate(2018, 1, 15)84 expect(new Midnight(2018, 5, 15)).toBeDate(2018, 5, 15)85 expect(new Midnight(2018, 12, 15)).toBeDate(2018, 12, 15)86 expect(new Midnight(2018, 13, 15)).toBeDate(2019, 1, 15)87 })88 test('time is fixed on 00:00:00.000', () => {89 const date = new Midnight()90 expect(date.getHours()).toBe(0)91 expect(date.getMinutes()).toBe(0)92 expect(date.getSeconds()).toBe(0)93 expect(date.getMilliseconds()).toBe(0)94 })95 test('instance of Date', () => {96 expect(new Midnight()).toBeInstanceOf(Date)97 })98 test('1 century', () => {99 expect(new Midnight(1, 5)).toBeDate(1, 5, 1)100 expect(new Midnight(99, 5)).toBeDate(99, 5, 1)101 expect(new Midnight(100, 5)).toBeDate(100, 5, 1)102 expect(new Midnight(1, 5, 15)).toBeDate(1, 5, 15)103 expect(new Midnight(99, 5, 15)).toBeDate(99, 5, 15)104 expect(new Midnight(100, 5, 15)).toBeDate(100, 5, 15)105 expect(new Midnight('0001/05/15')).toBeDate(1, 5, 15)106 expect(new Midnight('0099/05/15')).toBeDate(99, 5, 15)107 expect(new Midnight('0100/05/15')).toBeDate(100, 5, 15)108 })109 test('invalid date', () => {110 expect(new Midnight(null)).toInvalidDate()111 expect(new Midnight(undefined)).toInvalidDate()112 expect(new Midnight(NaN)).toInvalidDate()113 expect(new Midnight('invalid date')).toInvalidDate()114 })115 test('travel', () => {116 Timecop.freeze()117 Timecop.travel(new Date('2015-01-23'))118 expect(new Midnight()).toBeDate(2015, 1, 23)119 Timecop.return()120 })121})122describe('operator', () => {123 test.skip('==', () => {124 expect(new Midnight(2018, 5, 15) == new Midnight(2018, 5, 14)).toBe(false)125 expect(new Midnight(2018, 5, 15) == new Midnight(2018, 5, 15)).toBe(true)126 expect(new Midnight(2018, 5, 15) == new Midnight(2018, 5, 16)).toBe(false)127 })128 test.skip('!=', () => {129 expect(new Midnight(2018, 5, 15) != new Midnight(2018, 5, 14)).toBe(true)130 expect(new Midnight(2018, 5, 15) != new Midnight(2018, 5, 15)).toBe(false)131 expect(new Midnight(2018, 5, 15) != new Midnight(2018, 5, 16)).toBe(true)132 })133 test('<', () => {134 expect(new Midnight(2018, 5, 15) < new Midnight(2018, 5, 14)).toBe(false)135 expect(new Midnight(2018, 5, 15) < new Midnight(2018, 5, 15)).toBe(false)136 expect(new Midnight(2018, 5, 15) < new Midnight(2018, 5, 16)).toBe(true)137 })138 test('<=', () => {139 expect(new Midnight(2018, 5, 15) <= new Midnight(2018, 5, 14)).toBe(false)140 expect(new Midnight(2018, 5, 15) <= new Midnight(2018, 5, 15)).toBe(true)141 expect(new Midnight(2018, 5, 15) <= new Midnight(2018, 5, 16)).toBe(true)142 })143 test('>', () => {144 expect(new Midnight(2018, 5, 15) > new Midnight(2018, 5, 14)).toBe(true)145 expect(new Midnight(2018, 5, 15) > new Midnight(2018, 5, 15)).toBe(false)146 expect(new Midnight(2018, 5, 15) > new Midnight(2018, 5, 16)).toBe(false)147 })148 test('>=', () => {149 expect(new Midnight(2018, 5, 15) >= new Midnight(2018, 5, 14)).toBe(true)150 expect(new Midnight(2018, 5, 15) >= new Midnight(2018, 5, 15)).toBe(true)151 expect(new Midnight(2018, 5, 15) >= new Midnight(2018, 5, 16)).toBe(false)152 })153})154describe('parse', () => {155 test('instance of', () => {156 expect(Midnight.parse('2018-05-15')).toBeInstanceOf(Midnight)157 expect(Midnight.parse('2018-05-15')).toBeInstanceOf(Date)158 })159 test('year', () => {160 expect(Midnight.parse('2018')).toBeDate(2018, 1, 1)161 })162 test('year month', () => {163 expect(Midnight.parse('2018-05')).toBeDate(2018, 5, 1)164 expect(Midnight.parse('2018/05')).toBeDate(2018, 5, 1)165 expect(Midnight.parse('2018.05')).toBeDate(2018, 5, 1)166 expect(Midnight.parse('2018-5')).toBeDate(2018, 5, 1)167 expect(Midnight.parse('2018/5')).toBeDate(2018, 5, 1)168 expect(Midnight.parse('2018.5')).toBeDate(2018, 5, 1)169 expect(Midnight.parse('201805')).toBeDate(2018, 5, 1)170 })171 test('month day', () => {172 const year = Midnight.today().year()173 expect(Midnight.parse('5/1')).toBeDate(year, 5, 1)174 expect(Midnight.parse('05/01')).toBeDate(year, 5, 1)175 expect(Midnight.parse('5-1')).toBeDate(year, 5, 1)176 expect(Midnight.parse('05-01')).toBeDate(year, 5, 1)177 expect(Midnight.parse('5.1')).toBeDate(year, 5, 1)178 expect(Midnight.parse('05.01')).toBeDate(year, 5, 1)179 })180 test('year month day', () => {181 expect(Midnight.parse('2018-05-05')).toBeDate(2018, 5, 5)182 expect(Midnight.parse('2018/05/05')).toBeDate(2018, 5, 5)183 expect(Midnight.parse('2018.05.05')).toBeDate(2018, 5, 5)184 expect(Midnight.parse('2018-5-5')).toBeDate(2018, 5, 5)185 expect(Midnight.parse('2018/5/5')).toBeDate(2018, 5, 5)186 expect(Midnight.parse('2018.5.5')).toBeDate(2018, 5, 5)187 expect(Midnight.parse('20180515')).toBeDate(2018, 5, 15)188 })189 test('before 100 year', () => {190 expect(Midnight.parse('00010101')).toBeDate(1, 1, 1)191 expect(Midnight.parse('00990101')).toBeDate(99, 1, 1)192 expect(Midnight.parse('01000101')).toBeDate(100, 1, 1)193 })194 test('complete: true', () => {195 Timecop.freeze()196 Timecop.travel(new Date('2019-01-01'))197 expect(Midnight.parse('1-1-1', true)).toBeDate(2001, 1, 1)198 expect(Midnight.parse('1/1/1', true)).toBeDate(2001, 1, 1)199 expect(Midnight.parse('1.1.1', true)).toBeDate(2001, 1, 1)200 expect(Midnight.parse('68.1.1', true)).toBeDate(2068, 1, 1)201 expect(Midnight.parse('69.1.1', true)).toBeDate(1969, 1, 1)202 expect(Midnight.parse('99-1-1', true)).toBeDate(1999, 1, 1)203 expect(Midnight.parse('99/1/1', true)).toBeDate(1999, 1, 1)204 expect(Midnight.parse('99.1.1', true)).toBeDate(1999, 1, 1)205 Timecop.return()206 })207 test('complete: false', () => {208 expect(Midnight.parse('1-1-1', false)).toBeDate(1, 1, 1)209 expect(Midnight.parse('1/1/1', false)).toBeDate(1, 1, 1)210 expect(Midnight.parse('1.1.1', false)).toBeDate(1, 1, 1)211 expect(Midnight.parse('99-1-1', false)).toBeDate(99, 1, 1)212 expect(Midnight.parse('99/1/1', false)).toBeDate(99, 1, 1)213 expect(Midnight.parse('99.1.1', false)).toBeDate(99, 1, 1)214 })215 test('invalid value', () => {216 expect(Midnight.parse('invalid date')).toEqual(null)217 expect(Midnight.parse(null)).toEqual(null)218 expect(Midnight.parse(undefined)).toEqual(null)219 expect(Midnight.parse(NaN)).toEqual(null)220 expect(Midnight.parse()).toEqual(null)221 })222})223test('instanceof Date', () => {224 const date = new Midnight()225 expect(date).toBeInstanceOf(Date)226})227test('differenceInDays', () => {228 const date = new Midnight(2017, 5, 22)229 expect(date.differenceInDays(new Date('2017-05-27'))).toBe(5)230 expect(date.differenceInDays(new Midnight(2017, 5, 27))).toBe(5)231 expect(date.differenceInDays('2017/05/27')).toBe(5)232 expect(date.differenceInDays(new Date('2017-04-10'))).toBe(-42)233 expect(date.differenceInDays(new Date('2017-05-22'))).toBe(0)234 expect(date.differenceInDays(new Date('2017-06-10'))).toBe(19)235 expect(date.differenceInDays(null)).toBe(null)236 expect(date.differenceInDays(undefined)).toBe(null)237 expect(date.differenceInDays('invalid date')).toBe(null)238})239test('endOfMonth', () => {240 expect(new Midnight(2016, 8, 1).endOfMonth()).toBeDate(2016, 8, 31)241 expect(new Midnight(2016, 8, 15).endOfMonth()).toBeDate(2016, 8, 31)242 expect(new Midnight(2016, 8, 31).endOfMonth()).toBeDate(2016, 8, 31)243 expect(new Midnight(2016, 2, 25).endOfMonth()).toBeDate(2016, 2, 29)244 expect(new Midnight(2017, 2, 25).endOfMonth()).toBeDate(2017, 2, 28)245})246test('endOfYear', () => {247 expect(new Midnight(2017, 1, 1).endOfYear()).toBeDate(2017, 12, 31)248 expect(new Midnight(2017, 5, 15).endOfYear()).toBeDate(2017, 12, 31)249 expect(new Midnight(2017, 12, 1).endOfYear()).toBeDate(2017, 12, 31)250 expect(new Midnight(2018, 1, 31).endOfYear()).toBeDate(2018, 12, 31)251})252test('endOfDecade', () => {253 expect(new Midnight(2009, 12, 31).endOfDecade()).toBeDate(2009, 12, 31)254 expect(new Midnight(2010, 1, 1).endOfDecade()).toBeDate(2019, 12, 31)255 expect(new Midnight(2015, 5, 15).endOfDecade()).toBeDate(2019, 12, 31)256 expect(new Midnight(2019, 12, 31).endOfDecade()).toBeDate(2019, 12, 31)257 expect(new Midnight(2020, 1, 1).endOfDecade()).toBeDate(2029, 12, 31)258})259test('equals', () => {260 const date = new Midnight(2017, 5, 20)261 expect(date.equals(new Date('2017-05-20'))).toBe(true)262 expect(date.equals('2017/05/20')).toBe(true)263 expect(date.equals('2017/05/21')).toBe(false)264 expect(date.equals(null)).toBe(false)265 expect(date.equals(undefined)).toBe(false)266 expect(date.equals('invalid date')).toBe(false)267})268test('static equals', () => {269 expect(Midnight.equals(new Date('2017-05-20'), new Date('2017-05-20'))).toBe(true)270 expect(Midnight.equals(new Date('2017-05-20 01:23:45'), new Date('2017-05-20 12:34:56'))).toBe(true)271 expect(Midnight.equals(new Date('2017-05-20'), new Date('2017-05-21'))).toBe(false)272 expect(Midnight.equals('2017/05/20', '2017/05/20')).toBe(true)273 expect(Midnight.equals('2017/05/20', '2017/05/21')).toBe(false)274 expect(Midnight.equals('2017/05/20 01:23:45', '2017/05/20 12:34:56')).toBe(true)275})276test('day', () => {277 const date = new Midnight(2017, 5, 15)278 expect(date.day()).toBe(15)279 expect(date.day(0)).toBeDate(2017, 4, 30)280 expect(date.day(1)).toBeDate(2017, 5, 1)281 expect(date.day(31)).toBeDate(2017, 5, 31)282 expect(date.day(32)).toBeDate(2017, 6, 1)283})284test('month', () => {285 let date = new Midnight(2017, 5, 15)286 expect(date.month()).toBe(5)287 expect(date.month(-1)).toBeDate(2016, 11, 15)288 expect(date.month(0)).toBeDate(2016, 12, 15)289 expect(date.month(4)).toBeDate(2017, 4, 15)290 expect(date.month(6)).toBeDate(2017, 6, 15)291 expect(date.month(12)).toBeDate(2017, 12, 15)292 expect(date.month(13)).toBeDate(2018, 1, 15)293 date = new Midnight(2016, 8, 31)294 expect(date.month(9)).toBeDate(2016, 9, 30)295 expect(date.month(2)).toBeDate(2016, 2, 29)296 expect(date.month(6)).toBeDate(2016, 6, 30)297})298test('nextDay', () => {299 const date = new Midnight(2017, 5, 15)300 expect(date.nextDay()).toBeDate(2017, 5, 16)301 expect(date.nextDay(-15)).toBeDate(2017, 4, 30)302 expect(date.nextDay(-1)).toBeDate(2017, 5, 14)303 expect(date.nextDay(0)).toBeDate(2017, 5, 15)304 expect(date.nextDay(1)).toBeDate(2017, 5, 16)305 expect(date.nextDay(17)).toBeDate(2017, 6, 1)306})307test('nextMonth', () => {308 let date = new Midnight(2016, 8, 1)309 expect(date.nextMonth()).toBeDate(2016, 9, 1)310 expect(date.nextMonth(1)).toBeDate(2016, 9, 1)311 expect(date.nextMonth(4)).toBeDate(2016, 12, 1)312 expect(date.nextMonth(5)).toBeDate(2017, 1, 1)313 date = new Midnight(2016, 8, 15)314 expect(date.nextMonth(-1)).toBeDate(2016, 7, 15)315 expect(date.nextMonth(0)).toBeDate(2016, 8, 15)316 expect(date.nextMonth(1)).toBeDate(2016, 9, 15)317 date = new Midnight(2016, 8, 31)318 expect(date.nextMonth(1)).toBeDate(2016, 9, 30)319 expect(date.nextMonth(6)).toBeDate(2017, 2, 28)320 expect(date.nextMonth(-2)).toBeDate(2016, 6, 30)321})322test('nextYear', () => {323 let date = new Midnight(2016, 5, 15)324 expect(date.nextYear()).toBeDate(2017, 5, 15)325 expect(date.nextYear(-1)).toBeDate(2015, 5, 15)326 expect(date.nextYear(0)).toBeDate(2016, 5, 15)327 expect(date.nextYear(1)).toBeDate(2017, 5, 15)328 date = new Midnight(2016, 2, 29)329 expect(date.nextYear(-1)).toBeDate(2015, 2, 28)330 expect(date.nextYear(1)).toBeDate(2017, 2, 28)331 expect(date.nextYear(4)).toBeDate(2020, 2, 29)332})333test('nextSunday', () => {334 expect(new Midnight(2018, 11, 11).nextSunday()).toBeDate(2018, 11, 18)335 expect(new Midnight(2018, 11, 12).nextSunday()).toBeDate(2018, 11, 18)336 expect(new Midnight(2018, 11, 13).nextSunday()).toBeDate(2018, 11, 18)337 expect(new Midnight(2018, 11, 14).nextSunday()).toBeDate(2018, 11, 18)338 expect(new Midnight(2018, 11, 15).nextSunday()).toBeDate(2018, 11, 18)339 expect(new Midnight(2018, 11, 16).nextSunday()).toBeDate(2018, 11, 18)340 expect(new Midnight(2018, 11, 17).nextSunday()).toBeDate(2018, 11, 18)341 expect(new Midnight(2018, 11, 14).nextSunday(1)).toBeDate(2018, 11, 18)342 expect(new Midnight(2018, 11, 14).nextSunday(2)).toBeDate(2018, 11, 25)343 expect(new Midnight(2018, 11, 14).nextSunday(-1)).toBeDate(2018, 11, 11)344 expect(new Midnight(2018, 11, 14).nextSunday(-2)).toBeDate(2018, 11, 4)345 expect(new Midnight(2018, 11, 17).nextSunday(0)).toBeDate(2018, 11, 18)346 expect(new Midnight(2018, 11, 18).nextSunday(0)).toBeDate(2018, 11, 18)347 expect(new Midnight(2018, 11, 18).nextSunday(-0)).toBeDate(2018, 11, 18)348 expect(new Midnight(2018, 11, 19).nextSunday(-0)).toBeDate(2018, 11, 18)349})350test('nextMonday', () => {351 expect(new Midnight(2018, 11, 11).nextMonday()).toBeDate(2018, 11, 12)352 expect(new Midnight(2018, 11, 12).nextMonday()).toBeDate(2018, 11, 19)353 expect(new Midnight(2018, 11, 13).nextMonday()).toBeDate(2018, 11, 19)354 expect(new Midnight(2018, 11, 14).nextMonday()).toBeDate(2018, 11, 19)355 expect(new Midnight(2018, 11, 15).nextMonday()).toBeDate(2018, 11, 19)356 expect(new Midnight(2018, 11, 16).nextMonday()).toBeDate(2018, 11, 19)357 expect(new Midnight(2018, 11, 17).nextMonday()).toBeDate(2018, 11, 19)358 expect(new Midnight(2018, 11, 15).nextMonday(1)).toBeDate(2018, 11, 19)359 expect(new Midnight(2018, 11, 15).nextMonday(2)).toBeDate(2018, 11, 26)360 expect(new Midnight(2018, 11, 15).nextMonday(-1)).toBeDate(2018, 11, 12)361 expect(new Midnight(2018, 11, 15).nextMonday(-2)).toBeDate(2018, 11, 5)362 expect(new Midnight(2018, 11, 18).nextMonday(0)).toBeDate(2018, 11, 19)363 expect(new Midnight(2018, 11, 19).nextMonday(0)).toBeDate(2018, 11, 19)364 expect(new Midnight(2018, 11, 19).nextMonday(-0)).toBeDate(2018, 11, 19)365 expect(new Midnight(2018, 11, 20).nextMonday(-0)).toBeDate(2018, 11, 19)366})367test('nextTuesday', () => {368 expect(new Midnight(2018, 11, 11).nextTuesday()).toBeDate(2018, 11, 13)369 expect(new Midnight(2018, 11, 12).nextTuesday()).toBeDate(2018, 11, 13)370 expect(new Midnight(2018, 11, 13).nextTuesday()).toBeDate(2018, 11, 20)371 expect(new Midnight(2018, 11, 14).nextTuesday()).toBeDate(2018, 11, 20)372 expect(new Midnight(2018, 11, 15).nextTuesday()).toBeDate(2018, 11, 20)373 expect(new Midnight(2018, 11, 16).nextTuesday()).toBeDate(2018, 11, 20)374 expect(new Midnight(2018, 11, 17).nextTuesday()).toBeDate(2018, 11, 20)375 expect(new Midnight(2018, 11, 16).nextTuesday(1)).toBeDate(2018, 11, 20)376 expect(new Midnight(2018, 11, 16).nextTuesday(2)).toBeDate(2018, 11, 27)377 expect(new Midnight(2018, 11, 16).nextTuesday(-1)).toBeDate(2018, 11, 13)378 expect(new Midnight(2018, 11, 16).nextTuesday(-2)).toBeDate(2018, 11, 6)379 expect(new Midnight(2018, 11, 19).nextTuesday(0)).toBeDate(2018, 11, 20)380 expect(new Midnight(2018, 11, 20).nextTuesday(0)).toBeDate(2018, 11, 20)381 expect(new Midnight(2018, 11, 20).nextTuesday(-0)).toBeDate(2018, 11, 20)382 expect(new Midnight(2018, 11, 21).nextTuesday(-0)).toBeDate(2018, 11, 20)383})384test('nextWednesday', () => {385 expect(new Midnight(2018, 11, 11).nextWednesday()).toBeDate(2018, 11, 14)386 expect(new Midnight(2018, 11, 12).nextWednesday()).toBeDate(2018, 11, 14)387 expect(new Midnight(2018, 11, 13).nextWednesday()).toBeDate(2018, 11, 14)388 expect(new Midnight(2018, 11, 14).nextWednesday()).toBeDate(2018, 11, 21)389 expect(new Midnight(2018, 11, 15).nextWednesday()).toBeDate(2018, 11, 21)390 expect(new Midnight(2018, 11, 16).nextWednesday()).toBeDate(2018, 11, 21)391 expect(new Midnight(2018, 11, 17).nextWednesday()).toBeDate(2018, 11, 21)392 expect(new Midnight(2018, 11, 17).nextWednesday(1)).toBeDate(2018, 11, 21)393 expect(new Midnight(2018, 11, 17).nextWednesday(2)).toBeDate(2018, 11, 28)394 expect(new Midnight(2018, 11, 17).nextWednesday(-1)).toBeDate(2018, 11, 14)395 expect(new Midnight(2018, 11, 17).nextWednesday(-2)).toBeDate(2018, 11, 7)396 expect(new Midnight(2018, 11, 20).nextWednesday(0)).toBeDate(2018, 11, 21)397 expect(new Midnight(2018, 11, 21).nextWednesday(0)).toBeDate(2018, 11, 21)398 expect(new Midnight(2018, 11, 21).nextWednesday(-0)).toBeDate(2018, 11, 21)399 expect(new Midnight(2018, 11, 22).nextWednesday(-0)).toBeDate(2018, 11, 21)400})401test('nextThursday', () => {402 expect(new Midnight(2018, 11, 11).nextThursday()).toBeDate(2018, 11, 15)403 expect(new Midnight(2018, 11, 12).nextThursday()).toBeDate(2018, 11, 15)404 expect(new Midnight(2018, 11, 13).nextThursday()).toBeDate(2018, 11, 15)405 expect(new Midnight(2018, 11, 14).nextThursday()).toBeDate(2018, 11, 15)406 expect(new Midnight(2018, 11, 15).nextThursday()).toBeDate(2018, 11, 22)407 expect(new Midnight(2018, 11, 16).nextThursday()).toBeDate(2018, 11, 22)408 expect(new Midnight(2018, 11, 17).nextThursday()).toBeDate(2018, 11, 22)409 expect(new Midnight(2018, 11, 18).nextThursday(1)).toBeDate(2018, 11, 22)410 expect(new Midnight(2018, 11, 18).nextThursday(2)).toBeDate(2018, 11, 29)411 expect(new Midnight(2018, 11, 18).nextThursday(-1)).toBeDate(2018, 11, 15)412 expect(new Midnight(2018, 11, 18).nextThursday(-2)).toBeDate(2018, 11, 8)413 expect(new Midnight(2018, 11, 21).nextThursday(0)).toBeDate(2018, 11, 22)414 expect(new Midnight(2018, 11, 22).nextThursday(0)).toBeDate(2018, 11, 22)415 expect(new Midnight(2018, 11, 22).nextThursday(-0)).toBeDate(2018, 11, 22)416 expect(new Midnight(2018, 11, 23).nextThursday(-0)).toBeDate(2018, 11, 22)417})418test('nextFriday', () => {419 expect(new Midnight(2018, 11, 11).nextFriday()).toBeDate(2018, 11, 16)420 expect(new Midnight(2018, 11, 12).nextFriday()).toBeDate(2018, 11, 16)421 expect(new Midnight(2018, 11, 13).nextFriday()).toBeDate(2018, 11, 16)422 expect(new Midnight(2018, 11, 14).nextFriday()).toBeDate(2018, 11, 16)423 expect(new Midnight(2018, 11, 15).nextFriday()).toBeDate(2018, 11, 16)424 expect(new Midnight(2018, 11, 16).nextFriday()).toBeDate(2018, 11, 23)425 expect(new Midnight(2018, 11, 17).nextFriday()).toBeDate(2018, 11, 23)426 expect(new Midnight(2018, 11, 19).nextFriday(1)).toBeDate(2018, 11, 23)427 expect(new Midnight(2018, 11, 19).nextFriday(2)).toBeDate(2018, 11, 30)428 expect(new Midnight(2018, 11, 19).nextFriday(-1)).toBeDate(2018, 11, 16)429 expect(new Midnight(2018, 11, 19).nextFriday(-2)).toBeDate(2018, 11, 9)430 expect(new Midnight(2018, 11, 22).nextFriday(0)).toBeDate(2018, 11, 23)431 expect(new Midnight(2018, 11, 23).nextFriday(0)).toBeDate(2018, 11, 23)432 expect(new Midnight(2018, 11, 23).nextFriday(-0)).toBeDate(2018, 11, 23)433 expect(new Midnight(2018, 11, 24).nextFriday(-0)).toBeDate(2018, 11, 23)434})435test('nextSaturday', () => {436 expect(new Midnight(2018, 11, 11).nextSaturday()).toBeDate(2018, 11, 17)437 expect(new Midnight(2018, 11, 12).nextSaturday()).toBeDate(2018, 11, 17)438 expect(new Midnight(2018, 11, 13).nextSaturday()).toBeDate(2018, 11, 17)439 expect(new Midnight(2018, 11, 14).nextSaturday()).toBeDate(2018, 11, 17)440 expect(new Midnight(2018, 11, 15).nextSaturday()).toBeDate(2018, 11, 17)441 expect(new Midnight(2018, 11, 16).nextSaturday()).toBeDate(2018, 11, 17)442 expect(new Midnight(2018, 11, 17).nextSaturday()).toBeDate(2018, 11, 24)443 expect(new Midnight(2018, 11, 20).nextSaturday(1)).toBeDate(2018, 11, 24)444 expect(new Midnight(2018, 11, 20).nextSaturday(2)).toBeDate(2018, 12, 1)445 expect(new Midnight(2018, 11, 20).nextSaturday(-1)).toBeDate(2018, 11, 17)446 expect(new Midnight(2018, 11, 20).nextSaturday(-2)).toBeDate(2018, 11, 10)447 expect(new Midnight(2018, 11, 23).nextSaturday(0)).toBeDate(2018, 11, 24)448 expect(new Midnight(2018, 11, 24).nextSaturday(0)).toBeDate(2018, 11, 24)449 expect(new Midnight(2018, 11, 24).nextSaturday(-0)).toBeDate(2018, 11, 24)450 expect(new Midnight(2018, 11, 25).nextSaturday(-0)).toBeDate(2018, 11, 24)451})452describe('isToday', () => {453 test('today', () => {454 expect(new Midnight().isToday()).toBe(true)455 expect(new Midnight().nextDay(-1).isToday()).toBe(false)456 expect(new Midnight().nextDay(1).isToday()).toBe(false)457 })458 test('travel', () => {459 Timecop.freeze()460 Timecop.travel(new Date('2015-01-23'))461 expect(new Midnight().isToday()).toBe(true)462 Timecop.return()463 })464})465test('isSunday', () => {466 expect(new Midnight(2018, 11, 11).isSunday()).toBe(true)467 expect(new Midnight(2018, 11, 12).isSunday()).toBe(false)468 expect(new Midnight(2018, 11, 13).isSunday()).toBe(false)469 expect(new Midnight(2018, 11, 14).isSunday()).toBe(false)470 expect(new Midnight(2018, 11, 15).isSunday()).toBe(false)471 expect(new Midnight(2018, 11, 16).isSunday()).toBe(false)472 expect(new Midnight(2018, 11, 17).isSunday()).toBe(false)473})474test('isMonday', () => {475 expect(new Midnight(2018, 11, 11).isMonday()).toBe(false)476 expect(new Midnight(2018, 11, 12).isMonday()).toBe(true)477 expect(new Midnight(2018, 11, 13).isMonday()).toBe(false)478 expect(new Midnight(2018, 11, 14).isMonday()).toBe(false)479 expect(new Midnight(2018, 11, 15).isMonday()).toBe(false)480 expect(new Midnight(2018, 11, 16).isMonday()).toBe(false)481 expect(new Midnight(2018, 11, 17).isMonday()).toBe(false)482})483test('isTuesday', () => {484 expect(new Midnight(2018, 11, 11).isTuesday()).toBe(false)485 expect(new Midnight(2018, 11, 12).isTuesday()).toBe(false)486 expect(new Midnight(2018, 11, 13).isTuesday()).toBe(true)487 expect(new Midnight(2018, 11, 14).isTuesday()).toBe(false)488 expect(new Midnight(2018, 11, 15).isTuesday()).toBe(false)489 expect(new Midnight(2018, 11, 16).isTuesday()).toBe(false)490 expect(new Midnight(2018, 11, 17).isTuesday()).toBe(false)491})492test('isWednesday', () => {493 expect(new Midnight(2018, 11, 11).isWednesday()).toBe(false)494 expect(new Midnight(2018, 11, 12).isWednesday()).toBe(false)495 expect(new Midnight(2018, 11, 13).isWednesday()).toBe(false)496 expect(new Midnight(2018, 11, 14).isWednesday()).toBe(true)497 expect(new Midnight(2018, 11, 15).isWednesday()).toBe(false)498 expect(new Midnight(2018, 11, 16).isWednesday()).toBe(false)499 expect(new Midnight(2018, 11, 17).isWednesday()).toBe(false)500})501test('isThursday', () => {502 expect(new Midnight(2018, 11, 11).isThursday()).toBe(false)503 expect(new Midnight(2018, 11, 12).isThursday()).toBe(false)504 expect(new Midnight(2018, 11, 13).isThursday()).toBe(false)505 expect(new Midnight(2018, 11, 14).isThursday()).toBe(false)506 expect(new Midnight(2018, 11, 15).isThursday()).toBe(true)507 expect(new Midnight(2018, 11, 16).isThursday()).toBe(false)508 expect(new Midnight(2018, 11, 17).isThursday()).toBe(false)509})510test('isFriday', () => {511 expect(new Midnight(2018, 11, 11).isFriday()).toBe(false)512 expect(new Midnight(2018, 11, 12).isFriday()).toBe(false)513 expect(new Midnight(2018, 11, 13).isFriday()).toBe(false)514 expect(new Midnight(2018, 11, 14).isFriday()).toBe(false)515 expect(new Midnight(2018, 11, 15).isFriday()).toBe(false)516 expect(new Midnight(2018, 11, 16).isFriday()).toBe(true)517 expect(new Midnight(2018, 11, 17).isFriday()).toBe(false)518})519test('isSaturday', () => {520 expect(new Midnight(2018, 11, 11).isSaturday()).toBe(false)521 expect(new Midnight(2018, 11, 12).isSaturday()).toBe(false)522 expect(new Midnight(2018, 11, 13).isSaturday()).toBe(false)523 expect(new Midnight(2018, 11, 14).isSaturday()).toBe(false)524 expect(new Midnight(2018, 11, 15).isSaturday()).toBe(false)525 expect(new Midnight(2018, 11, 16).isSaturday()).toBe(false)526 expect(new Midnight(2018, 11, 17).isSaturday()).toBe(true)527})528test('isValid', () => {529 expect(new Midnight().isValid()).toBe(true)530 expect(new Midnight('invalid date').isValid()).toBe(false)531})532test('setHours', () => {533 const date = new Midnight(2016, 5, 20)534 const value = date.valueOf()535 expect(date.setHours(1)).toBe(value)536 expect(date.getHours(1)).toBe(0)537 date.setHours(-1)538 expect(date.getHours()).toBe(0)539 expect(date.getDate()).toBe(19)540})541test('setMinutes', () => {542 const date = new Midnight(2016, 5, 20)543 const value = date.valueOf()544 expect(date.setMinutes(1)).toBe(value)545 expect(date.getMinutes()).toBe(0)546})547test('setSeconds', () => {548 const date = new Midnight(2016, 5, 20)549 const value = date.valueOf()550 expect(date.setSeconds(1)).toBe(value)551 expect(date.getSeconds()).toBe(0)552})553test('setMilliseconds', () => {554 const date = new Midnight(2016, 5, 20)555 const value = date.valueOf()556 expect(date.setMilliseconds(1)).toBe(value)557 expect(date.getMilliseconds()).toBe(0)558})559test('setTime', () => {560 const d = new Date()561 const date = new Midnight(d)562 const time = date.getTime()563 date.setTime(d.getTime())564 expect(date.getTime()).toBe(time)565})566test('startOfMonth', () => {567 expect(new Midnight(2017, 5, 1).startOfMonth()).toBeDate(2017, 5, 1)568 expect(new Midnight(2017, 5, 15).startOfMonth()).toBeDate(2017, 5, 1)569 expect(new Midnight(2017, 5, 31).startOfMonth()).toBeDate(2017, 5, 1)570})571test('startOfYear', () => {572 expect(new Midnight(2017, 1, 1).startOfYear()).toBeDate(2017, 1, 1)573 expect(new Midnight(2017, 5, 15).startOfYear()).toBeDate(2017, 1, 1)574 expect(new Midnight(2017, 12, 31).startOfYear()).toBeDate(2017, 1, 1)575})576test('startOfDecade', () => {577 expect(new Midnight(2009, 12, 31).startOfDecade()).toBeDate(2000, 1, 1)578 expect(new Midnight(2010, 1, 1).startOfDecade()).toBeDate(2010, 1, 1)579 expect(new Midnight(2015, 5, 15).startOfDecade()).toBeDate(2010, 1, 1)580 expect(new Midnight(2019, 12, 31).startOfDecade()).toBeDate(2010, 1, 1)581 expect(new Midnight(2020, 1, 1).startOfDecade()).toBeDate(2020, 1, 1)582})583test('toObject', () => {584 const date = new Midnight(2017, 5, 20)585 expect(date.toObject()).toEqual({year: 2017, month: 5, day: 20})586})587test('year', () => {588 let date = new Midnight(2017, 5, 20)589 expect(date.year()).toBe(2017)590 expect(date.year(2016)).toBeDate(2016, 5, 20)591 expect(date.year(2018)).toBeDate(2018, 5, 20)592 date = new Midnight(2016, 2, 29)593 expect(date.year(2017)).toBeDate(2017, 2, 28)594})595test('immutable', () => {596 const value = '2017-05-20'597 const ymd = [2017, 5, 20]598 const date = new Midnight(value)599 date.day(1)600 expect(date).toBeDate(...ymd)601 date.month(1)602 expect(date).toBeDate(...ymd)603 date.year(2000)604 expect(date).toBeDate(...ymd)605 date.nextDay(1)606 expect(date).toBeDate(...ymd)607 date.nextMonth(1)608 expect(date).toBeDate(...ymd)609 date.nextYear(1)610 expect(date).toBeDate(...ymd)611 date.startOfMonth()612 expect(date).toBeDate(...ymd)613 date.startOfYear()614 expect(date).toBeDate(...ymd)615 date.startOfDecade()616 expect(date).toBeDate(...ymd)617 date.endOfMonth()618 expect(date).toBeDate(...ymd)619 date.endOfYear()620 expect(date).toBeDate(...ymd)621 date.endOfDecade()622 expect(date).toBeDate(...ymd)...
ruby-date.test.ts
Source:ruby-date.test.ts
1import RubyDate from './ruby-date'2declare global{3 namespace jest{4 interface Matchers<R>{5 toBeDate(year: number, month: number, day:number): R6 }7 }8}9expect.extend({10 toBeDate(received, year, month, day){11 let date = received12 if(date instanceof RubyDate){13 date = date.toDate()14 }15 const pass = (16 date.getFullYear() === year &&17 date.getMonth() + 1 === month &&18 date.getDate() === day19 )20 return {21 pass,22 message(){23 const expected = `${24 `${year}`.padStart(4, '0')25 }-${26 `${month}`.padStart(2, '0')27 }-${28 `${day}`.padStart(2, '0')29 }`30 if(pass){31 return `${received} and ${expected} are same date`32 }else{33 return `${received} and ${expected} are not same date`34 }35 }36 }37 }38})39describe('constructor', () => {40 test('no argument', () => {41 expect(new RubyDate()).toBeDate(-4712, 1, 1)42 })43 test('year', () => {44 expect(new RubyDate(1)).toBeDate(1, 1, 1)45 expect(new RubyDate(99)).toBeDate(99, 1, 1)46 expect(new RubyDate(100)).toBeDate(100, 1, 1)47 expect(new RubyDate(1900)).toBeDate(1900, 1, 1)48 expect(new RubyDate(2000)).toBeDate(2000, 1, 1)49 expect(new RubyDate(2100)).toBeDate(2100, 1, 1)50 })51 test('year and month', () => {52 expect(new RubyDate(2018, 0)).toBeDate(2017, 12, 1)53 expect(new RubyDate(2018, 1)).toBeDate(2018, 1, 1)54 expect(new RubyDate(2018, 5)).toBeDate(2018, 5, 1)55 expect(new RubyDate(2018, 12)).toBeDate(2018, 12, 1)56 expect(new RubyDate(2018, 13)).toBeDate(2019, 1, 1)57 expect(new RubyDate(1, 5)).toBeDate(1, 5, 1)58 expect(new RubyDate(50, 0)).toBeDate(49, 12, 1)59 expect(new RubyDate(50, 13)).toBeDate(51, 1, 1)60 expect(new RubyDate(99, 5)).toBeDate(99, 5, 1)61 expect(new RubyDate(100, 5)).toBeDate(100, 5, 1)62 })63 test('year, month and day', () => {64 expect(new RubyDate(2018, 5, 0)).toBeDate(2018, 4, 30)65 expect(new RubyDate(2018, 5, 1)).toBeDate(2018, 5, 1)66 expect(new RubyDate(2018, 5, 31)).toBeDate(2018, 5, 31)67 expect(new RubyDate(2018, 5, 32)).toBeDate(2018, 6, 1)68 expect(new RubyDate(2018, 0, 15)).toBeDate(2017, 12, 15)69 expect(new RubyDate(2018, 1, 15)).toBeDate(2018, 1, 15)70 expect(new RubyDate(2018, 5, 15)).toBeDate(2018, 5, 15)71 expect(new RubyDate(2018, 12, 15)).toBeDate(2018, 12, 15)72 expect(new RubyDate(2018, 13, 15)).toBeDate(2019, 1, 15)73 expect(new RubyDate(1, 5, 15)).toBeDate(1, 5, 15)74 expect(new RubyDate(99, 5, 15)).toBeDate(99, 5, 15)75 expect(new RubyDate(100, 5, 15)).toBeDate(100, 5, 15)76 })77 test('invalid date', () => {78 // @ts-expect-error79 expect(() => new RubyDate('foo')).toThrow()80 })81})82describe('operator', () => {83 // test.skip('==', () => {84 // expect(new RubyDate(2021, 3, 15) == new RubyDate(2021, 3, 14)).toBe(false)85 // expect(new RubyDate(2021, 3, 15) == new RubyDate(2021, 3, 15)).toBe(true)86 // expect(new RubyDate(2021, 3, 15) == new RubyDate(2021, 3, 16)).toBe(false)87 // })88 // test.skip('!=', () => {89 // expect(new RubyDate(2021, 3, 15) != new RubyDate(2021, 3, 14)).toBe(true)90 // expect(new RubyDate(2021, 3, 15) != new RubyDate(2021, 3, 15)).toBe(false)91 // expect(new RubyDate(2021, 3, 15) != new RubyDate(2021, 3, 16)).toBe(true)92 // })93 test('<', () => {94 expect(new RubyDate(2021, 3, 15) < new RubyDate(2021, 3, 14)).toEqual(false)95 expect(new RubyDate(2021, 3, 15) < new RubyDate(2021, 3, 15)).toEqual(false)96 expect(new RubyDate(2021, 3, 15) < new RubyDate(2021, 3, 16)).toEqual(true)97 })98 test('<=', () => {99 expect(new RubyDate(2021, 3, 15) <= new RubyDate(2021, 3, 14)).toEqual(false)100 expect(new RubyDate(2021, 3, 15) <= new RubyDate(2021, 3, 15)).toEqual(true)101 expect(new RubyDate(2021, 3, 15) <= new RubyDate(2021, 3, 16)).toEqual(true)102 })103 test('>', () => {104 expect(new RubyDate(2021, 3, 15) > new RubyDate(2021, 3, 14)).toEqual(true)105 expect(new RubyDate(2021, 3, 15) > new RubyDate(2021, 3, 15)).toEqual(false)106 expect(new RubyDate(2021, 3, 15) > new RubyDate(2021, 3, 16)).toEqual(false)107 })108 test('>=', () => {109 expect(new RubyDate(2021, 3, 15) >= new RubyDate(2021, 3, 14)).toEqual(true)110 expect(new RubyDate(2021, 3, 15) >= new RubyDate(2021, 3, 15)).toEqual(true)111 expect(new RubyDate(2021, 3, 15) >= new RubyDate(2021, 3, 16)).toEqual(false)112 })113})114describe('parse', () => {115 test('year month day', () => {116 expect(RubyDate.parse('2021-01-05')).toBeDate(2021, 1, 5)117 expect(RubyDate.parse('2021/01/05')).toBeDate(2021, 1, 5)118 expect(RubyDate.parse('2021.01.05')).toBeDate(2021, 1, 5)119 expect(RubyDate.parse('2021-1-5')).toBeDate(2021, 1, 5)120 expect(RubyDate.parse('2021/1/5')).toBeDate(2021, 1, 5)121 expect(RubyDate.parse('2021.1.5')).toBeDate(2021, 1, 5)122 })123 test('ISO 8601', () => {124 expect(RubyDate.parse('2021-04-27T02:20:12+00:00')).toBeDate(2021, 4, 27)125 expect(RubyDate.parse('2021-04-27T00:00:00.000Z')).toBeDate(2021, 4, 27)126 })127 test('invalid date', () => {128 expect(RubyDate.parse('foo')).toBeNull()129 // @ts-expect-error130 expect(RubyDate.parse(123)).toBeNull()131 // @ts-expect-error132 expect(RubyDate.parse({})).toBeNull()133 // @ts-expect-error134 expect(RubyDate.parse([])).toBeNull()135 // @ts-ignore136 expect(RubyDate.parse(null)).toBeNull()137 // @ts-ignore138 expect(RubyDate.parse(undefined)).toBeNull()139 })140})141test('today', () => {142 const now = new Date()143 expect(RubyDate.today()).toBeDate(now.getFullYear(), now.getMonth() + 1, now.getDate())144})145test('beginningOfMonth', () => {146 expect(new RubyDate(2017, 5, 1).beginningOfMonth()).toBeDate(2017, 5, 1)147 expect(new RubyDate(2017, 5, 15).beginningOfMonth()).toBeDate(2017, 5, 1)148 expect(new RubyDate(2017, 5, 31).beginningOfMonth()).toBeDate(2017, 5, 1)149})150test('beginningOfYear', () => {151 expect(new RubyDate(2017, 1, 1).beginningOfYear()).toBeDate(2017, 1, 1)152 expect(new RubyDate(2017, 5, 15).beginningOfYear()).toBeDate(2017, 1, 1)153 expect(new RubyDate(2017, 12, 31).beginningOfYear()).toBeDate(2017, 1, 1)154})155test('day', () => {156 const date = new RubyDate(2021, 5, 20)157 expect(date.day()).toEqual(20)158})159test('endOfMonth', () => {160 expect(new RubyDate(2016, 8, 1).endOfMonth()).toBeDate(2016, 8, 31)161 expect(new RubyDate(2016, 8, 15).endOfMonth()).toBeDate(2016, 8, 31)162 expect(new RubyDate(2016, 8, 31).endOfMonth()).toBeDate(2016, 8, 31)163 expect(new RubyDate(2016, 2, 25).endOfMonth()).toBeDate(2016, 2, 29)164 expect(new RubyDate(2017, 2, 25).endOfMonth()).toBeDate(2017, 2, 28)165})166test('endOfYear', () => {167 expect(new RubyDate(2017, 1, 1).endOfYear()).toBeDate(2017, 12, 31)168 expect(new RubyDate(2017, 5, 15).endOfYear()).toBeDate(2017, 12, 31)169 expect(new RubyDate(2017, 12, 1).endOfYear()).toBeDate(2017, 12, 31)170 expect(new RubyDate(2018, 1, 31).endOfYear()).toBeDate(2018, 12, 31)171})172test('month', () => {173 const date = new RubyDate(2021, 5, 20)174 expect(date.month()).toEqual(5)175})176describe('nextDay', () => {177 test('basic', () => {178 const date = new RubyDate(2017, 5, 15)179 expect(date.nextDay()).toBeDate(2017, 5, 16)180 expect(date.nextDay(-15)).toBeDate(2017, 4, 30)181 expect(date.nextDay(-1)).toBeDate(2017, 5, 14)182 expect(date.nextDay(0)).toBeDate(2017, 5, 15)183 expect(date.nextDay(1)).toBeDate(2017, 5, 16)184 expect(date.nextDay(17)).toBeDate(2017, 6, 1)185 })186 test('skip 1582-10-05 ... 1582-10-14', () => {187 expect(new RubyDate(1582, 10, 4).nextDay(1)).toBeDate(1582, 10, 15)188 expect(new RubyDate(1582, 10, 15).nextDay(-1)).toBeDate(1582, 10, 4)189 })190})191describe('nextMonth', () => {192 test('basic', () => {193 let date = new RubyDate(2016, 8, 1)194 expect(date.nextMonth()).toBeDate(2016, 9, 1)195 expect(date.nextMonth(1)).toBeDate(2016, 9, 1)196 expect(date.nextMonth(4)).toBeDate(2016, 12, 1)197 expect(date.nextMonth(5)).toBeDate(2017, 1, 1)198 date = new RubyDate(2016, 8, 15)199 expect(date.nextMonth(-1)).toBeDate(2016, 7, 15)200 expect(date.nextMonth(0)).toBeDate(2016, 8, 15)201 expect(date.nextMonth(1)).toBeDate(2016, 9, 15)202 date = new RubyDate(2016, 8, 31)203 expect(date.nextMonth(1)).toBeDate(2016, 9, 30)204 expect(date.nextMonth(6)).toBeDate(2017, 2, 28)205 expect(date.nextMonth(-2)).toBeDate(2016, 6, 30)206 })207 test('skip 1582-10-05 ... 1582-10-14', () => {208 expect(new RubyDate(1582, 9, 3).nextMonth(1)).toBeDate(1582, 10, 3)209 expect(new RubyDate(1582, 9, 4).nextMonth(1)).toBeDate(1582, 10, 4)210 expect(new RubyDate(1582, 9, 14).nextMonth(1)).toBeDate(1582, 10, 4)211 expect(new RubyDate(1582, 9, 15).nextMonth(1)).toBeDate(1582, 10, 15)212 expect(new RubyDate(1582, 11, 3).nextMonth(-1)).toBeDate(1582, 10, 3)213 expect(new RubyDate(1582, 11, 4).nextMonth(-1)).toBeDate(1582, 10, 4)214 expect(new RubyDate(1582, 11, 14).nextMonth(-1)).toBeDate(1582, 10, 4)215 expect(new RubyDate(1582, 11, 15).nextMonth(-1)).toBeDate(1582, 10, 15)216 })217})218describe('nextYear', () => {219 test('basic', () => {220 let date = new RubyDate(2016, 5, 15)221 expect(date.nextYear()).toBeDate(2017, 5, 15)222 expect(date.nextYear(-1)).toBeDate(2015, 5, 15)223 expect(date.nextYear(0)).toBeDate(2016, 5, 15)224 expect(date.nextYear(1)).toBeDate(2017, 5, 15)225 date = new RubyDate(2016, 2, 29)226 expect(date.nextYear(-1)).toBeDate(2015, 2, 28)227 expect(date.nextYear(1)).toBeDate(2017, 2, 28)228 expect(date.nextYear(4)).toBeDate(2020, 2, 29)229 })230 test('skip 1582-10-05 ... 1582-10-14', () => {231 expect(new RubyDate(1581, 10, 3).nextYear(1)).toBeDate(1582, 10, 3)232 expect(new RubyDate(1581, 10, 4).nextYear(1)).toBeDate(1582, 10, 4)233 expect(new RubyDate(1581, 10, 14).nextYear(1)).toBeDate(1582, 10, 4)234 expect(new RubyDate(1581, 10, 15).nextYear(1)).toBeDate(1582, 10, 15)235 expect(new RubyDate(1583, 10, 3).nextYear(-1)).toBeDate(1582, 10, 3)236 expect(new RubyDate(1583, 10, 4).nextYear(-1)).toBeDate(1582, 10, 4)237 expect(new RubyDate(1583, 10, 14).nextYear(-1)).toBeDate(1582, 10, 4)238 expect(new RubyDate(1583, 10, 15).nextYear(-1)).toBeDate(1582, 10, 15)239 })240})241test('strftime', () => {242 expect(new RubyDate(1).strftime('%Y')).toEqual('0001')243 expect(new RubyDate(1).strftime('%-Y')).toEqual('1')244 expect(new RubyDate(-1).strftime('%Y')).toEqual('-0001')245 expect(new RubyDate(-1).strftime('%-Y')).toEqual('-1')246 expect(new RubyDate(2005).strftime('%y')).toEqual('05')247 expect(new RubyDate(2005).strftime('%-y')).toEqual('5')248 expect(new RubyDate(2021, 3).strftime('%m')).toEqual('03')249 expect(new RubyDate(2021, 3).strftime('%-m')).toEqual('3')250 expect(new RubyDate(2021, 3, 5).strftime('%d')).toEqual('05')251 expect(new RubyDate(2021, 3, 5).strftime('%-d')).toEqual('5')252 expect(new RubyDate(2021, 3, 5).strftime('%Y/%m/%d')).toEqual('2021/03/05')...
comments-repository.unit.test.js
Source:comments-repository.unit.test.js
...71 expect(result.id).toBeString()72 expect(result.text).toBeString()73 expect(result.post).toBeString()74 expect(result.author).toBeString()75 expect(result.createdAt).toBeDate()76 expect(result.updatedAt).toBeDate()77 expect(result.version).toBeNumber()78 // Value Checks79 expect(result.text).toBe(data.text)80 expect(result.post).toBe(data.post)81 expect(result.author).toBe(data.author)82 })83 it('should return the updated db entry on update', async () => {84 const data = await generateMockData(true)85 const updateData = await generateMockData(true)86 let comment = await repo.create({87 req: data,88 response: {}89 })90 comment = comment.toJSON()91 let result = await repo.update({92 req: {93 id: comment.id,94 data: Object.assign(comment, updateData)95 },96 response: {}97 })98 result = result.toJSON()99 expect(result).not.toBeNil()100 // Stucture check/s101 expect(result).toContainAllKeys([102 'id',103 'text',104 'post',105 'author',106 'createdAt',107 'updatedAt',108 'version'109 ])110 // Type check/s111 expect(result.id).toBeString()112 expect(result.text).toBeString()113 expect(result.post).toBeString()114 expect(result.author).toBeString()115 expect(result.createdAt).toBeDate()116 expect(result.updatedAt).toBeDate()117 expect(result.version).toBeNumber()118 // Value Checks119 expect(result.id).toBe(comment.id)120 expect(result.text).toBe(updateData.text)121 expect(result.post).toBe(updateData.post)122 expect(result.author).toBe(updateData.author)123 })124 it('#should return all rows on blank query', async () => {125 const data = await generateMockData()126 await Promise.all(map(data, async (entry) => {127 const comment = await repo.create({128 req: entry,129 response: {}130 })131 return comment.toJSON()132 }))133 const { list } = await repo.findAll({134 req: {135 query: JSON.stringify({})136 },137 response: {}138 })139 expect(list).not.toBeNil()140 // Stucture check/s141 expect(list[0]).toContainAllKeys([142 'id',143 'text',144 'post',145 'author',146 'createdAt',147 'updatedAt',148 'version'149 ])150 // Type check/s151 expect(list).toBeArray()152 expect(list[0].id).toBeString()153 expect(list[0].text).toBeString()154 expect(list[0].post).toBeString()155 expect(list[0].author).toBeString()156 expect(list[0].createdAt).toBeDate()157 expect(list[0].updatedAt).toBeDate()158 expect(list[0].version).toBeNumber()159 // Value checks160 expect(list).toBeArrayOfSize(3)161 return true162 })163 it('#should return all rows matching a query', async () => {164 const data = await generateMockData()165 const entries = await Promise.all(map(data, async (entry) => {166 const comment = await repo.create({167 req: entry,168 response: {}169 })170 return comment.toJSON()171 }))172 const { list } = await repo.findAll({173 req: {174 query: JSON.stringify({175 where: {176 id: { $in: map(entries, entry => entry.id) }177 }178 })179 },180 response: {}181 })182 expect(list).not.toBeNil()183 // Stucture check/s184 expect(list[0]).toContainAllKeys([185 'id',186 'text',187 'post',188 'author',189 'createdAt',190 'updatedAt',191 'version'192 ])193 // Type check/s194 expect(list).toBeArray()195 expect(list[0].id).toBeString()196 expect(list[0].text).toBeString()197 expect(list[0].post).toBeString()198 expect(list[0].author).toBeString()199 expect(list[0].createdAt).toBeDate()200 expect(list[0].updatedAt).toBeDate()201 expect(list[0].version).toBeNumber()202 // Value checks203 expect(list).toBeArrayOfSize(3)204 expect(list.map(entry => entry.text)).toIncludeSameMembers(data.map(entry => entry.text))205 return true206 })207 it('should return a single row that matches a query', async () => {208 const data = await generateMockData(true)209 let comment = await repo.create({210 req: data,211 response: {}212 })213 comment = comment.toJSON()214 const result = await repo.findOne({215 req: {216 query: JSON.stringify({217 where: {218 id: comment.id219 }220 })221 },222 response: {}223 })224 expect(result).not.toBeNil()225 // Stucture check/s226 expect(result).toContainAllKeys([227 'id',228 'text',229 'post',230 'author',231 'createdAt',232 'updatedAt',233 'version'234 ])235 // Type check/s236 expect(result.id).toBeString()237 expect(result.text).toBeString()238 expect(result.post).toBeString()239 expect(result.author).toBeString()240 expect(result.createdAt).toBeDate()241 expect(result.updatedAt).toBeDate()242 expect(result.version).toBeNumber()243 // Value Checks244 expect(result.text).toBe(data.text)245 expect(result.post).toBe(data.post)246 expect(result.author).toBe(data.author)247 })248 it('should return the correct count that matches a query', async () => {249 const data = await generateMockData(true)250 let comment = await repo.create({251 req: data,252 response: {}253 })254 comment = comment.toJSON()255 const result = await repo.count({...
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({ toBeDate });3test('passes when value is a valid Date', () => {4 expect(new Date()).toBeDate();5});6test('fails when value is not a valid Date', () => {7 expect('2019-10-10').not.toBeDate();8});9{10 "scripts": {11 },12 "repository": {
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({ toBeDate });3test('passes when given a date', () => {4 expect(new Date()).toBeDate();5});6test('fails when not given a date', () => {7 expect('2018-01-01').not.toBeDate();8});9const { toBeEmpty } = require('jest-extended');10expect.extend({ toBeEmpty });11test('passes when given an empty string', () => {12 expect('').toBeEmpty();13});14test('fails when not given an empty string', () => {15 expect(' ').not.toBeEmpty();16});17const { toBeEmptyArray } = require('jest-extended');18expect.extend({ toBeEmptyArray });19test('passes when given an empty array', () => {20 expect([]).toBeEmptyArray();21});22test('fails when not given an empty array', () => {23 expect([1]).not.toBeEmptyArray();24});25const { toBeEmptyObject } = require('jest-extended');26expect.extend({ toBeEmptyObject });27test('passes when given an empty object', () => {28 expect({}).toBeEmptyObject();29});30test('fails when not given an empty object', () => {31 expect({ foo: 'bar' }).not.toBeEmptyObject();32});33const { toBeEvenNumber } = require('jest-extended');34expect.extend({ toBeEvenNumber });35test('passes when given an even number', () => {36 expect(2).toBeEvenNumber();37});38test('fails when not given an even number', () => {39 expect(1).not.toBeEvenNumber();40});41const { toBeFalse } = require('jest-extended');42expect.extend({ toBeFalse });43test('passes when given false', () => {44 expect(false).toBeFalse();45});46test('fails when not given false', () => {47 expect(true).not.toBeFalse();48});
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({ toBeDate });3test('passes when given a Date object', () => {4 expect(new Date()).toBeDate();5});6test('fails when given a non-Date object', () => {7 expect('2018-01-01').not.toBeDate();8});9const { toBeDateString } = require('jest-extended');10expect.extend({ toBeDateString });11test('passes when given a valid Date string', () => {12 expect('2018-01-01').toBeDateString();13});14test('fails when given an invalid Date string', () => {15 expect('2018-01-01T00:00:00.000Z').not.toBeDateString();16});17const { toBeEmptyArray } = require('jest-extended');18expect.extend({ toBeEmptyArray });19test('passes when given an empty array', () => {20 expect([]).toBeEmptyArray();21});22test('fails when given a non-empty array', () => {23 expect([1, 2, 3]).not.toBeEmptyArray();24});25const { toBeEmptyObject } = require('jest-extended');26expect.extend({ toBeEmptyObject });27test('passes when given an empty object', () => {28 expect({}).toBeEmptyObject();29});30test('fails when given a non-empty object', () => {31 expect({ foo: 'bar' }).not.toBeEmptyObject();32});33const { toBeEmptyString } = require('jest-extended');34expect.extend({ toBeEmptyString });35test('passes when given an empty string', () => {36 expect('').toBeEmptyString();37});38test('fails when given a non-empty string', () => {39 expect('foo').not.toBeEmptyString();40});41const { toBeEvenNumber } = require('jest-extended');42expect.extend({ toBeEvenNumber });43test('passes when given an even number', () => {
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({toBeDate});3const { toBeDate } = require('jest-extended');4expect.extend({toBeDate});5const { toBeDate } = require('jest-extended');6expect.extend({toBeDate});7const { toBeDate } = require('jest-extended');8expect.extend({toBeDate});9const { toBeDate } = require('jest-extended');10expect.extend({toBeDate});11const { toBeDate } = require('jest-extended');12expect.extend({toBeDate});13const { toBeDate } = require('jest-extended');14expect.extend({toBeDate});15const { toBeDate } = require('jest-extended');16expect.extend({toBeDate});17const { toBeDate } = require('jest-extended');18expect.extend({toBeDate});19const { toBeDate } = require('jest-extended');20expect.extend({toBeDate});
Using AI Code Generation
1test('toBeDate', () => {2 expect(new Date()).toBeDate();3});4test('toBeDate', () => {5 expect(new Date()).toBeDate();6});7test('toBeDate', () => {8 expect(new Date()).toBeDate();9});10test('toBeDate', () => {11 expect(new Date()).toBeDate();12});13test('toBeDate', () => {14 expect(new Date()).toBeDate();15});16test('toBeDate', () => {17 expect(new Date()).toBeDate();18});19test('toBeDate', () => {20 expect(new Date()).toBeDate();21});22test('toBeDate', () => {23 expect(new Date()).toBeDate();24});25test('toBeDate', () => {26 expect(new Date()).toBeDate();27});28test('toBeDate', () => {29 expect(new Date()).toBeDate();30});31test('toBeDate', () => {32 expect(new Date()).toBeDate();33});34test('toBeDate', () => {35 expect(new Date()).toBeDate();36});37test('toBeDate', () => {38 expect(new Date()).toBeDate();39});40test('toBeDate', () => {41 expect(new Date()).toBeDate();42});
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({ toBeDate });3test('test toBeDate', () => {4 expect(new Date()).toBeDate();5});6const { toBeDate } = require('jest-extended');7expect.extend({ toBeDate });8test('test toBeDate', () => {9 expect(new Date()).toBeDate();10});11const { toBeDate } = require('jest-extended');12expect.extend({ toBeDate });13test('test toBeDate', () => {14 expect(new Date()).toBeDate();15});16const { toBeDate } = require('jest-extended');17expect.extend({ toBeDate });18test('test toBeDate', () => {19 expect(new Date()).toBeDate();20});21const { toBeDate } = require('jest-extended');22expect.extend({ toBeDate });23test('test toBeDate', () => {24 expect(new Date()).toBeDate();25});26const { toBeDate } = require('jest-extended');27expect.extend({ toBeDate });28test('test toBeDate', () => {29 expect(new Date()).toBeDate();30});31const { toBeDate } = require('jest-extended');32expect.extend({ toBeDate });33test('test toBeDate', () => {34 expect(new Date()).toBeDate();35});36const { toBeDate } = require('jest-extended');37expect.extend({ toBeDate });38test('test toBeDate', () => {39 expect(new Date()).toBeDate();40});41const { toBeDate } = require('jest-extended');42expect.extend({ toBeDate });43test('test toBeDate', () => {44 expect(new Date()).toBeDate();45});46const { toBeDate } = require('jest-extended');47expect.extend({ toBeDate });48test('test toBeDate', () => {
Using AI Code Generation
1const { toBeDate } = require('jest-extended');2expect.extend({ toBeDate });3test('checks if the value is a valid date', () => {4 expect(new Date()).toBeDate();5});6const { toBeDate } = require('jest-extended');7expect.extend({ toBeDate });8test('checks if the value is a valid date', () => {9 expect(new Date()).toBeDate();10});11const { toBeDate } = require('jest-extended');12expect.extend({ toBeDate });13test('checks if the value is a valid date', () => {14 expect(new Date()).toBeDate();15});16const { toBeDate } = require('jest-extended');17expect.extend({ toBeDate });18test('checks if the value is a valid date', () => {19 expect(new Date()).toBeDate();20});21const { toBeDate } = require('jest-extended');22expect.extend({ toBeDate });23test('checks if the value is a valid date', () => {24 expect(new Date()).toBeDate();25});26const { toBeDate } = require('jest-extended');27expect.extend({ toBeDate });28test('checks if the value is a valid date', () => {29 expect(new Date()).toBeDate();30});31const { toBeDate } = require('jest-extended');32expect.extend({ toBeDate });33test('checks if the value is a valid date', () => {34 expect(new Date()).toBeDate();35});36const { toBeDate } = require('jest-extended');37expect.extend({ toBeDate });38test('checks if the value is a valid date', () => {39 expect(new Date()).toBeDate();40});41const { toBeDate } = require('jest-extended');42expect.extend({ toBeDate });43test('checks if the value is a valid date', () => {44 expect(new Date()).toBeDate();45});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!