From d9eec02027feb1db99ad8b8b94b472ae5cf0ba67 Mon Sep 17 00:00:00 2001 From: "Rong Sen Ng (motss)" Date: Sat, 18 Sep 2021 21:28:09 +0800 Subject: [PATCH] test: add tests for toMultiCalendars --- src/tests/helpers/to-multi-calendars.test.ts | 155 +++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/tests/helpers/to-multi-calendars.test.ts diff --git a/src/tests/helpers/to-multi-calendars.test.ts b/src/tests/helpers/to-multi-calendars.test.ts new file mode 100644 index 00000000..51f4ab7c --- /dev/null +++ b/src/tests/helpers/to-multi-calendars.test.ts @@ -0,0 +1,155 @@ +import { expect } from '@open-wc/testing'; + +import { toFormatters } from '../../helpers/to-formatters'; +import { toMultiCalendars } from '../../helpers/to-multi-calendars'; +import type { ToMultiCalendarsInit } from '../../helpers/typings'; +import { messageFormatter } from '../test-utils/message-formatter'; + +describe(toMultiCalendars.name, () => { + const locale = 'en-US'; + const { + dayFormat, + fullDateFormat, + longWeekdayFormat, + narrowWeekdayFormat, + } = toFormatters(locale); + const defaultInit: ToMultiCalendarsInit = { + currentDate: new Date('2020-02-02'), + dayFormat, + fullDateFormat, + locale, + longWeekdayFormat, + narrowWeekdayFormat, + }; + + type A = [Partial, number]; + + const cases: A[] = [ + [ + {}, + 1, + ], + [ + { + count: 2, + }, + 3, + ], + ]; + + cases.forEach((a) => { + const [testPartialInit, expected] = a; + + it( + messageFormatter('returns calendar with optional count (%j)', a), + () => { + const result = toMultiCalendars({ + ...defaultInit, + ...testPartialInit, + }); + + expect(result.calendars).have.length(expected); + expect(result.disabledDatesSet.size).equal(0); + expect(result.disabledDaysSet.size).equal(0); + } + ); + }); + + type A1 = [Partial, number, number[]]; + + const cases1: A1[] = [ + [ + { + min: new Date('2020-01-01'), + }, + 1, + [6], + ], + [ + { + max: new Date('2020-03-03'), + }, + 1, + [6], + ], + [ + { + min: new Date('2020-01-01'), + max: new Date('2020-03-03'), + }, + 1, + [6], + ], + [ + { + min: new Date('2020-03-03'), + }, + 1, + [0], + ], + [ + { + max: new Date('2020-01-01'), + }, + 1, + [0], + ], + ]; + + cases1.forEach((a) => { + const [testPartialInit, expectedCalendars, expectedMonthRows] = a; + + it( + messageFormatter('returns calendar with optional min, max (%j)', a), + () => { + const result = toMultiCalendars({ + ...defaultInit, + ...testPartialInit, + }); + + expect(result.calendars).have.length(expectedCalendars); + expect( + result.calendars.map(n => n.calendar.length) + ).deep.equal(expectedMonthRows); + expect(result.disabledDatesSet.size).equal(0); + expect(result.disabledDaysSet.size).equal(0); + } + ); + }); + + type A2 = [Partial, number, number]; + + const cases2: A2[] = [ + [ + { + disabledDates: [new Date('2020-02-01'), new Date('2020-02-10')], + }, + 2, + 0, + ], + [ + { + disabledDays: [0, 2], + }, + 8, + 2, + ], + ]; + + cases2.forEach((a) => { + const [testPartialInit, expectedDisabledDates, expectedDisabledDays] = a; + + it( + messageFormatter('returns calendar with optional disabled dates, days (partialInit=%j)', a), + () => { + const result = toMultiCalendars({ + ...defaultInit, + ...testPartialInit, + }); + + expect(result.disabledDatesSet.size).equal(expectedDisabledDates); + expect(result.disabledDaysSet.size).equal(expectedDisabledDays); + } + ); + }); +});