Skip to content

Commit dcb7396

Browse files
committed
feat: add initialPeriod
1 parent 3beb67f commit dcb7396

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

core/src/components/__tests__/cron-core.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22

3+
import { nextTick } from 'vue'
34
import { useCron, type CronFormat } from '../cron-core'
45

56
type UseCronReturn = ReturnType<typeof useCron>
@@ -19,10 +20,11 @@ function cronToString({ selected: { value: selected }, period }: UseCronReturn):
1920
}
2021

2122
describe('useCron', () => {
22-
it('renders properly', () => {
23+
it('renders properly', async () => {
2324
const tests: {
2425
format: CronFormat
2526
value: string
27+
period?: string
2628
expected: string
2729
}[] = [
2830
{
@@ -34,11 +36,36 @@ describe('useCron', () => {
3436
format: 'quartz',
3537
value: '* * * * * *',
3638
expected: `Every Year in every month on every day and every day of the week at every hour : every minute : every second`
39+
},
40+
{
41+
format: 'crontab',
42+
value: '* * * * *',
43+
period: 'hour',
44+
expected: `Every Hour at every minute(s)`
45+
},
46+
{
47+
format: 'quartz',
48+
value: '* * * * * *',
49+
period: 'q-hour',
50+
expected: `Every Hour at every minute : every second`
51+
},
52+
{
53+
format: 'quartz',
54+
value: '0 15 10 * * ?',
55+
period: 'month',
56+
expected: `Every Month on every day and no specific day of the week at 10 : 15 : 00`
57+
},
58+
{
59+
format: 'quartz',
60+
value: '0 15 10 ? * *',
61+
period: 'month',
62+
expected: `Every Month on no specific day and every day of the week at 10 : 15 : 00`
3763
}
3864
]
3965

4066
for (const t of tests) {
41-
const cron = useCron({ format: t.format, initialValue: t.value })
67+
const cron = useCron({ format: t.format, initialValue: t.value, initialPeriod: t.period })
68+
await nextTick()
4269
expect(cronToString(cron)).toEqual(t.expected)
4370
}
4471
})

core/src/components/cron-core.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type CronFormat = 'crontab' | 'quartz'
1010

1111
export interface CronOptions {
1212
initialValue?: string
13+
initialPeriod?: string
1314
locale?: string
1415
fields?: Field[]
1516
periods?: Period[]
@@ -125,10 +126,12 @@ export function useCron(options: CronOptions) {
125126
text: l10n.getLocaleStr(p.id, TextPosition.Text)
126127
}
127128
})
129+
const initialPeriod =
130+
periods.find((p) => p.id == options.initialPeriod) ?? periods[periods.length - 1]
128131

129132
const cron = ref(initialValue)
130133
const error = ref('')
131-
const period = ref(periods[periods.length - 1])
134+
const period = ref(initialPeriod)
132135
const periodPrefix = ref('')
133136
const periodSuffix = ref('')
134137
const segments = fields.map((f) => {
@@ -223,6 +226,9 @@ export const cronProps = {
223226
modelValue: {
224227
type: String
225228
},
229+
initialPeriod: {
230+
type: String
231+
},
226232
format: {
227233
type: String as PropType<CronFormat>
228234
},

0 commit comments

Comments
 (0)