Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinsingh30 committed Jul 8, 2023
1 parent 3048e82 commit 4aaaf89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 12 additions & 8 deletions src/index.ts
Expand Up @@ -3,8 +3,8 @@ import type { CalendarConfig, RenderedMonths } from './types';
import { generateMonth } from './utils';

export class Calendar {
static #currentDate: Date = new Date();
#weekStartIndex: number;
#currentDate: Date = new Date();
#calendarConfig: CalendarConfig;
#renderedMonths: RenderedMonths = [];

Expand All @@ -20,7 +20,7 @@ export class Calendar {
);
}

getDate(offset = 0): Date {
static getDate(offset = 0): Date {
const currentDay = new Date(
this.#currentDate.getFullYear(),
this.#currentDate.getMonth(),
Expand All @@ -34,16 +34,20 @@ export class Calendar {
return currentDay;
}

today() {
static today() {
return this.getDate();
}

isToday(compareDate: Date) {
static isToday(compareDate: Date) {
const dt = this.today();
return this.compare(dt, compareDate);
}

static compare(inputDate: Date, compareDate: Date) {
return (
dt.getFullYear() === compareDate.getFullYear() &&
dt.getMonth() === compareDate.getMonth() &&
dt.getDate() === compareDate.getDate()
inputDate.getFullYear() === compareDate.getFullYear() &&
inputDate.getMonth() === compareDate.getMonth() &&
inputDate.getDate() === compareDate.getDate()
);
}

Expand All @@ -53,7 +57,7 @@ export class Calendar {

create(offset = 0) {
const { visibleMonthCount, visibleWeekCount } = this.#calendarConfig;
const currentDate = this.today();
const currentDate = Calendar.today();
const monthToRender = currentDate.getMonth() + offset;
this.reset();
for (let i = 0; i < visibleMonthCount; i += 1) {
Expand Down
18 changes: 13 additions & 5 deletions tests/index.spec.ts
Expand Up @@ -6,7 +6,7 @@ describe('calendar instance', () => {
let dayOffset: number;
let calendarStart: Date;
beforeEach(() => {
today = calendar.today();
today = Calendar.today();
dt = new Date(today.getFullYear(), today.getMonth());
dayOffset = dt.getDay() - WEEKS.findIndex((wk) => wk === Week.SUN);
if (dayOffset < 0) {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('calendar instance', () => {
expect(today.getDate()).toBe(new Date().getDate());
});
test('should get offset date', () => {
expect(calendar.getDate(+1).getDate()).toBe(
expect(Calendar.getDate(+1).getDate()).toBe(
new Date(
today.getFullYear(),
today.getMonth(),
Expand All @@ -81,7 +81,7 @@ describe('Calendar class', () => {
let today: Date;
let dt: Date;
beforeEach(() => {
today = calendar.today();
today = Calendar.today();
dt = new Date(today.getFullYear(), today.getMonth());
});
test('should create new instance of calendar', () => {
Expand Down Expand Up @@ -129,7 +129,15 @@ describe('Calendar class', () => {
expect(WEEKS[calendarOutput[0].dates[0].getDay()]).toBe(Week.FRI);
});
test('should validate input date as todays date', () => {
expect(calendar.isToday(today)).toBeTruthy();
expect(calendar.isToday(new Date(2022, 10))).toBeFalsy();
expect(Calendar.isToday(today)).toBeTruthy();
expect(Calendar.isToday(new Date(2022, 10))).toBeFalsy();
});
test('should validate input date with comparison date', () => {
expect(
Calendar.compare(new Date(2022, 10), new Date(2022, 10))
).toBeTruthy();
expect(
Calendar.compare(new Date(2022, 10), new Date(2022, 11))
).toBeFalsy();
});
});

0 comments on commit 4aaaf89

Please sign in to comment.