Skip to content

Commit

Permalink
chore(kit): PrimitiveCalendarRange add unit tests for `defaultViewe…
Browse files Browse the repository at this point in the history
…dMonth` updating
  • Loading branch information
mdlufy committed Oct 24, 2024
1 parent 363d164 commit f9ba83a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit, OnChanges {
ngOnChanges({
defaultViewedMonthFirst,
defaultViewedMonthSecond,
value,
}: SimpleChanges): void {
if (!value) {
if (!this.value) {
this.updateViewedMonths(
defaultViewedMonthFirst?.currentValue,
defaultViewedMonthSecond?.currentValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Component, ViewChild} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {TuiDay, TuiMonth} from '@taiga-ui/cdk';
import {TuiDay, TuiDayRange, TuiMonth} from '@taiga-ui/cdk';
import {TUI_DEFAULT_MARKER_HANDLER, TuiMarkerHandler} from '@taiga-ui/core';
import {
TuiPrimitiveCalendarRangeComponent,
TuiPrimitiveCalendarRangeModule,
Expand All @@ -10,12 +11,24 @@ import {
describe('PrimitiveRangeCalendar component', () => {
@Component({
template: `
<tui-primitive-calendar-range></tui-primitive-calendar-range>
<tui-primitive-calendar-range
[defaultViewedMonthFirst]="defaultViewedMonthFirst"
[defaultViewedMonthSecond]="defaultViewedMonthSecond"
[markerHandler]="markerHandler"
[value]="value"
></tui-primitive-calendar-range>
`,
})
class TestComponent {
@ViewChild(TuiPrimitiveCalendarRangeComponent, {static: true})
component!: TuiPrimitiveCalendarRangeComponent;

defaultViewedMonthFirst = TuiMonth.currentLocal();
defaultViewedMonthSecond = TuiMonth.currentLocal().append({month: 1});

value: TuiDayRange | null = null;

markerHandler: TuiMarkerHandler = TUI_DEFAULT_MARKER_HANDLER;
}

let fixture: ComponentFixture<TestComponent>;
Expand Down Expand Up @@ -142,19 +155,65 @@ describe('PrimitiveRangeCalendar component', () => {
});
});

it('When handle any changes, current viewed month do not updates', () => {
const date = TuiMonth.currentLocal().append({month: 3});
describe('defaultViewedMonths updating', () => {
const defaultMonth = TuiMonth.currentLocal();
const updatedMonth = TuiMonth.currentLocal().append({
year: 1,
});

it('If other input updates after defaultViewedMonth was updated, new viewed months do not change', () => {
testComponent.defaultViewedMonthFirst = updatedMonth;
testComponent.defaultViewedMonthSecond = updatedMonth.append({
month: 1,
});
fixture.detectChanges();

testComponent.markerHandler = (day: TuiDay) =>
day.day % 2 === 0 ? ['first'] : ['second'];
fixture.detectChanges();

expect(component.userViewedMonthFirst.toString()).toBe(
updatedMonth.toString(),
);
expect(component.userViewedMonthSecond.toString()).toBe(
updatedMonth.append({month: 1}).toString(),
);
});

it('If value not selected, updating defaultViewedMonth change viewed months', () => {
testComponent.defaultViewedMonthFirst = updatedMonth;
testComponent.defaultViewedMonthSecond = updatedMonth.append({
month: 1,
});
fixture.detectChanges();

component.userViewedMonthFirst = date;
component.userViewedMonthSecond = date.append({month: 1});
expect(component.userViewedMonthFirst.toString()).toBe(
updatedMonth.toString(),
);
expect(component.userViewedMonthSecond.toString()).toBe(
updatedMonth.append({month: 1}).toString(),
);
});

component.markerHandler = (day: TuiDay) =>
day.day % 2 === 0 ? ['first'] : ['second'];
component.ngOnChanges({});
it('If value selected, updating defaultViewedMonth do not change viewed month', () => {
testComponent.value = new TuiDayRange(
new TuiDay(2024, 9, 25),
new TuiDay(2024, 9, 25),
);
fixture.detectChanges();

expect(component.userViewedMonthFirst.toString()).toBe(date.toString());
expect(component.userViewedMonthSecond.toString()).toBe(
date.append({month: 1}).toString(),
);
testComponent.defaultViewedMonthFirst = updatedMonth;
testComponent.defaultViewedMonthSecond = updatedMonth.append({
month: 1,
});
fixture.detectChanges();

expect(component.userViewedMonthFirst.toString()).toBe(
defaultMonth.toString(),
);
expect(component.userViewedMonthSecond.toString()).toBe(
defaultMonth.append({month: 1}).toString(),
);
});
});
});

0 comments on commit f9ba83a

Please sign in to comment.