Skip to content

Commit

Permalink
fixing #340
Browse files Browse the repository at this point in the history
  • Loading branch information
vlio20 committed Jan 18, 2018
1 parent 4ec6aa9 commit da3c72d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/day-time-calendar/day-time-calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue
}

dateSelected(day: IDate) {
this.selected = this.dayTimeCalendarService.updateDay(this.selected, day.date);
this.selected = this.dayTimeCalendarService.updateDay(this.selected, day.date, this.config);
this.emitChange();
}

Expand Down
44 changes: 40 additions & 4 deletions src/app/day-time-calendar/day-time-calendar.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as moment from 'moment';
import {UtilsService} from '../common/services/utils/utils.service';
import {DayCalendarService} from '../day-calendar/day-calendar.service';
import {TimeSelectService} from '../time-select/time-select.service';
import {IDayCalendarConfigInternal} from '../day-calendar/day-calendar-config.model';

const DAY_FORMAT = 'YYYYMMDD';
const TIME_FORMAT = 'HH:mm:ss';
Expand All @@ -20,13 +21,48 @@ describe('Service: DayTimeCalendarService', () => {
(service: DayTimeCalendarService) => {
const daytime = moment('2011091313:12:11', COMBINED_FORMAT);
const day = moment('10110203', DAY_FORMAT);
expect(service.updateDay(daytime, day).format(COMBINED_FORMAT)).toEqual('1011020313:12:11');
}));
expect(service.updateDay(daytime, day, {}).format(COMBINED_FORMAT)).toEqual('1011020313:12:11');
})
);

it('should check the updateTime method', inject([DayTimeCalendarService],
it('should check the updateTime method when time is before min', inject([DayTimeCalendarService],
(service: DayTimeCalendarService) => {
const daytime = moment('2011091313:12:11', COMBINED_FORMAT);
const config: IDayCalendarConfigInternal = {
min: daytime.clone().add(10, 'm'),
max: daytime.clone().add(50, 'm')
};

const time = daytime.clone();
expect(service.updateDay(daytime, time, config).format(COMBINED_FORMAT))
.toEqual(daytime.clone().add(10, 'm').format(COMBINED_FORMAT));

expect(service.updateDay(daytime, time, {}).format(COMBINED_FORMAT))
.toEqual(daytime.format(COMBINED_FORMAT));
})
);

it('should check the updateTime method when time is before max', inject([DayTimeCalendarService],
(service: DayTimeCalendarService) => {
const daytime = moment('2011091313:12:11', COMBINED_FORMAT);
const config: IDayCalendarConfigInternal = {
min: daytime.clone().subtract(50, 'm'),
max: daytime.clone().subtract(10, 'm')
};

const time = daytime.clone();
expect(service.updateDay(daytime, time, config).format(COMBINED_FORMAT))
.toEqual(daytime.clone().subtract(10, 'm').format(COMBINED_FORMAT));

expect(service.updateDay(daytime, time, {}).format(COMBINED_FORMAT))
.toEqual(daytime.format(COMBINED_FORMAT));
})
);

it('should check the updateTime method', inject([DayTimeCalendarService], (service: DayTimeCalendarService) => {
const daytime = moment('2011091313:12:11', COMBINED_FORMAT);
const time = moment('03:11:10', TIME_FORMAT);
expect(service.updateTime(daytime, time).format(COMBINED_FORMAT)).toEqual('2011091303:11:10');
}));
})
);
});
17 changes: 15 additions & 2 deletions src/app/day-time-calendar/day-time-calendar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,26 @@ export class DayTimeCalendarService {
return _config;
}

updateDay(current: Moment, day: Moment): Moment {
updateDay(current: Moment, day: Moment, config: IDayTimeCalendarConfig): Moment {
const time = current ? current : moment();
return moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT);
let updated = moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT)

if (config.min) {
const min = <Moment>config.min;
updated = min.isAfter(updated) ? min : updated;
}

if (config.max) {
const max = <Moment>config.max;
updated = max.isBefore(updated) ? max : updated;
}

return updated;
}

updateTime(current: Moment, time: Moment): Moment {
const day = current ? current : moment();

return moment(day.format(DAY_FORMAT) + time.format(TIME_FORMAT), COMBINED_FORMAT);
}
}

0 comments on commit da3c72d

Please sign in to comment.