Skip to content

Commit

Permalink
add test & fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
xrkffgg committed Nov 19, 2020
1 parent 85cdaf7 commit 3f17f62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/generate/dateFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getYear,
getMonth,
getDate,
endOfDay,
endOfMonth,
getHours,
getMinutes,
getSeconds,
Expand Down Expand Up @@ -42,7 +42,7 @@ const generateConfig: GenerateConfig<Date> = {
// get
getNow: () => new Date(),
getFixedDate: string => new Date(string),
getEndDate: date => endOfDay(date),
getEndDate: date => endOfMonth(date),
getWeekDay: date => getDay(date),
getYear: date => getYear(date),
getMonth: date => getMonth(date),
Expand Down
3 changes: 1 addition & 2 deletions src/utils/timeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export function getLowerBoundTime(
export function getLastDay<DateType>(generateConfig: GenerateConfig<DateType>, date: DateType) {
const year = generateConfig.getYear(date);
const month = generateConfig.getMonth(date) + 1;
let monthShow: string;
const endDate = generateConfig.getEndDate(generateConfig.getFixedDate(`${year}-${month}-01`));
const lastDay = generateConfig.getDate(endDate);
monthShow = month < 10 ? `0${month}` : `${month}`;
const monthShow = month < 10 ? `0${month}` : `${month}`;
return `${year}-${monthShow}-${lastDay}`;
}
8 changes: 8 additions & 0 deletions tests/generate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ describe('Picker.Generate', () => {
describe(name, () => {
it('get', () => {
const now = generateConfig.getNow();
const fixedDate = generateConfig.getFixedDate('1990-09-03');
const endDate = generateConfig.getEndDate(fixedDate);
expect(generateConfig.getWeekDay(now)).toEqual(1);
expect(generateConfig.getSecond(now)).toEqual(3);
expect(generateConfig.getMinute(now)).toEqual(2);
expect(generateConfig.getHour(now)).toEqual(1);
expect(generateConfig.getDate(now)).toEqual(3);
expect(generateConfig.getDate(fixedDate)).toEqual(3);
expect(generateConfig.getDate(endDate)).toEqual(30);
expect(generateConfig.getMonth(now)).toEqual(8);
expect(generateConfig.getMonth(fixedDate)).toEqual(8);
expect(generateConfig.getMonth(endDate)).toEqual(8);
expect(generateConfig.getYear(now)).toEqual(1990);
expect(generateConfig.getYear(fixedDate)).toEqual(1990);
expect(generateConfig.getYear(endDate)).toEqual(1990);
});

it('set', () => {
Expand Down
11 changes: 10 additions & 1 deletion tests/util.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import momentGenerateConfig from '../src/generate/moment';
import { getLowerBoundTime, setTime } from '../src/utils/timeUtil';
import { getLowerBoundTime, setTime, getLastDay } from '../src/utils/timeUtil';
import { toArray } from '../src/utils/miscUtil';
import { isSameTime, isSameDecade } from '../src/utils/dateUtil';
import { getMoment } from './util/commonUtil';
Expand Down Expand Up @@ -64,4 +64,13 @@ describe('Picker.Util', () => {
),
).toBeTruthy();
});

describe('getLastDay', () => {
expect(
getLastDay(
momentGenerateConfig,
getMoment('2020-10-01'),
),
).toEqual('2020-10-31');
});
});

0 comments on commit 3f17f62

Please sign in to comment.