Skip to content

Commit

Permalink
test: refactor tests of toNextSelectableDate
Browse files Browse the repository at this point in the history
Signed-off-by: Rong Sen Ng (motss) <[email protected]>
  • Loading branch information
motss committed Jan 6, 2024
1 parent b6526b3 commit 21876ae
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 100 deletions.
187 changes: 87 additions & 100 deletions src/__tests__/helpers/to-next-selectable-date.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from '@open-wc/testing';
import { describe, expect, it } from 'vitest';

import { toNextSelectableDate } from '../../helpers/to-next-selectable-date';
import type { ToNextSelectableDateInit } from '../../helpers/typings';
import { messageFormatter } from '../test-utils/message-formatter';

describe(toNextSelectableDate.name, () => {
const defaultInit: ToNextSelectableDateInit = {
Expand All @@ -14,165 +13,159 @@ describe(toNextSelectableDate.name, () => {
minTime: +new Date('2020-01-01'),
};

type CaseNextSelectableDateFOrNonDisabledDate = [
partialInit: Partial<ToNextSelectableDateInit>,
expected: Date
];
const casesNextSelectableDateForNonDisabledDate: CaseNextSelectableDateFOrNonDisabledDate[] = [
[
{},
new Date('2020-02-02'),
],
[
{
it.each<{
$_value: Date;
partialInit: Partial<ToNextSelectableDateInit>;
}>([
{
$_value: new Date('2020-02-02'),
partialInit: {},
},
{
$_value: new Date('2020-02-02'),
partialInit: {
maxTime: +new Date('2020-02-02'),
minTime: +new Date('2020-02-02'),
},
new Date('2020-02-02'),
],
];
casesNextSelectableDateForNonDisabledDate.forEach((a) => {
const [partialInit, expected] = a;
},
])('returns next selectable date for non-disabled date ($partialInit)', ({
$_value,
partialInit,
}) => {
const result = toNextSelectableDate({
...defaultInit,
...partialInit,
});

it(
messageFormatter('returns next selectable date for non-disabled date (%j)', a),
() => {
const result = toNextSelectableDate({
...defaultInit,
...partialInit,
});

expect(result).deep.equal(expected);
}
);
expect(result).toEqual($_value);
});

type CaseNextSelectableDateForDisabledDate = [
partialInit: Partial<ToNextSelectableDateInit>,
expected: Date
];
const casesNextSelectableDateForDisabledDate: CaseNextSelectableDateForDisabledDate[] = [
it.each<{
$_value: Date;
partialInit: Partial<ToNextSelectableDateInit>;
}>([
// disabled dates to non-disabled dates
[
{
{
$_value: new Date('2020-02-01'),
partialInit: {
disabledDatesSet: new Set([+new Date('2020-02-02')]),
key: 'ArrowLeft',
},
new Date('2020-02-01'),
],
[
{
},
{
$_value: new Date('2020-02-03'),
partialInit: {
disabledDatesSet: new Set([+new Date('2020-02-02')]),
key: 'ArrowRight',
},
new Date('2020-02-03'),
],
},

// > 1 disabled dates to non-disabled dates
[
{
{
$_value: new Date('2020-01-31'),
partialInit: {
disabledDatesSet: new Set([
+new Date('2020-02-01'),
+new Date('2020-02-02'),
]),
key: 'ArrowLeft',
},
new Date('2020-01-31'),
],
[
{
},
{
$_value: new Date('2020-02-04'),
partialInit: {
disabledDatesSet: new Set([
+new Date('2020-02-02'),
+new Date('2020-02-03'),
]),
key: 'ArrowRight',
},
new Date('2020-02-04'),
],
},

// > 1 disabled dates to non-disabled dates with min/ max dates
[
{
{
$_value: new Date('2020-01-31'),
partialInit: {
disabledDatesSet: new Set([
+new Date('2020-02-01'),
+new Date('2020-02-02'),
]),
key: 'ArrowLeft',
minTime: +new Date('2020-01-31'),
},
new Date('2020-01-31'),
],
[
{
},
{
$_value: new Date('2020-02-04'),
partialInit: {
disabledDatesSet: new Set([
+new Date('2020-02-02'),
+new Date('2020-02-03'),
]),
key: 'ArrowRight',
maxTime: +new Date('2020-02-04'),
},
new Date('2020-02-04'),
],
},

// disabled date before min to min
[
{
{
$_value: new Date(defaultInit.minTime),
partialInit: {
date: new Date('2019-02-02'),
key: 'ArrowLeft',
},
new Date(defaultInit.minTime),
],
[
{
},
{
$_value: new Date(defaultInit.minTime),
partialInit: {
date: new Date('2019-02-02'),
key: 'ArrowRight',
},
new Date(defaultInit.minTime),
],
},

// disabled date after max to max
[
{
{
$_value: new Date(defaultInit.maxTime),
partialInit: {
date: new Date('2020-04-03'),
key: 'ArrowRight',
},
new Date(defaultInit.maxTime),
],
[
{
},
{
$_value: new Date(defaultInit.maxTime),
partialInit: {
date: new Date('2020-04-03'),
key: 'ArrowLeft',
},
new Date(defaultInit.maxTime),
],
},

// disabled min/ max to non-disabled date after/ before min/ max
[
{
{
$_value: new Date('2020-02-02'),
partialInit: {
date: new Date('2020-02-01'),
disabledDatesSet: new Set([
+new Date('2020-02-01'),
]),
key: 'ArrowLeft',
minTime: +new Date('2020-02-01'),
},
new Date('2020-02-02'),
],
[
{
},
{
$_value: new Date('2020-04-02'),
partialInit: {
date: new Date('2020-04-03'),
disabledDatesSet: new Set([
+new Date('2020-04-03'),
]),
key: 'ArrowRight',
maxTime: +new Date('2020-04-03'),
},
new Date('2020-04-02'),
],
},

// disabled max to non-disabled min
[
{
{
$_value: new Date('2020-04-02'),
partialInit: {
date: new Date('2020-04-03'),
disabledDatesSet: new Set([
+new Date('2020-04-03'),
Expand All @@ -181,23 +174,17 @@ describe(toNextSelectableDate.name, () => {
maxTime: +new Date('2020-04-03'),
minTime: +new Date('2020-04-02'),
},
new Date('2020-04-02'),
],
];
casesNextSelectableDateForDisabledDate.forEach((a) => {
const [partialInit, expected] = a;

it(
messageFormatter('returns next selectable date for disabled date (%j)', a),
() => {
const result = toNextSelectableDate({
...defaultInit,
...partialInit,
});
},
])('returns next selectable date for disabled date ($partialInit)', ({
$_value,
partialInit,
}) => {
const result = toNextSelectableDate({
...defaultInit,
...partialInit,
});

expect(result).deep.equal(expected);
}
);
expect(result).toEqual($_value);
});

});
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default defineConfig({
'**/*test*/helpers/to-day-diff-inclusive.test.ts',
'**/*test*/helpers/to-formatters.test.ts',
'**/*test*/helpers/to-multi-calendars.test.ts',
'**/*test*/helpers/to-next-selectable-date.test.ts',
// '**/*test*/date-picker-input/**.test.ts',
],
clearMocks: true,
Expand Down

0 comments on commit 21876ae

Please sign in to comment.