-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add new
dateProp
prop type (#774)
1 parent
6ca7ac2
commit 5008238
Showing
5 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { PropOptionsGenerator, PropType } from '../types'; | ||
import { propOptionsGenerator } from '../util'; | ||
import type { Validator } from '../validators'; | ||
|
||
/** | ||
* Allows any `Date` object (validated at runtime and compile time). | ||
* | ||
* @param validator - Optional function for further runtime validation; should return `undefined` if valid, or an error string if invalid. | ||
*/ | ||
export const dateProp = (validator?: Validator): PropOptionsGenerator<Date> => | ||
propOptionsGenerator(Date as unknown as PropType<Date>, validator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { dateProp } from '../../src/prop-types/date'; | ||
|
||
describe('dateProp().optional', () => { | ||
it('creates the correct prop options', () => { | ||
expect(dateProp().optional).toStrictEqual({ | ||
type: Date, | ||
required: false, | ||
default: undefined, | ||
validator: undefined, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('dateProp().nullable', () => { | ||
it('creates the correct prop options', () => { | ||
expect(dateProp().nullable).toStrictEqual({ | ||
type: Date, | ||
required: false, | ||
default: null, | ||
validator: undefined, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('dateProp().withDefault', () => { | ||
it('creates the correct prop options', () => { | ||
expect(dateProp().withDefault(() => new Date())).toStrictEqual({ | ||
type: Date, | ||
required: false, | ||
default: expect.any(Function), | ||
validator: undefined, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('dateProp().required', () => { | ||
it('creates the correct prop options', () => { | ||
expect(dateProp().required).toStrictEqual({ | ||
type: Date, | ||
required: true, | ||
validator: undefined, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { describe, expect, test } from 'tstyche'; | ||
import type * as Vue2_6 from 'vue2-6/types/options'; | ||
import type * as Vue2_7 from 'vue2-7/types/options'; | ||
import type * as Vue3 from '@vue/runtime-core/dist/runtime-core'; | ||
import { dateProp } from '../../src/prop-types/date'; | ||
import { createVue2Component } from '../utils'; | ||
import type { Vue2ComponentWithProp } from '../utils'; | ||
|
||
describe('dateProp().optional', () => { | ||
test('Vue 2.6', () => { | ||
expect<Vue2_6.PropOptions<Date | undefined>>().type.toBeAssignableWith( | ||
dateProp().optional, | ||
); | ||
expect<Vue2_6.PropOptions<Date>>().type.not.toBeAssignableWith( | ||
dateProp().optional, | ||
); | ||
|
||
expect(createVue2Component(dateProp().optional)).type.toBe< | ||
Vue2ComponentWithProp<Date | undefined> | ||
>(); | ||
}); | ||
|
||
test('Vue 2.7', () => { | ||
expect<Vue2_7.PropOptions<Date | undefined>>().type.toBeAssignableWith( | ||
dateProp().optional, | ||
); | ||
expect<Vue2_7.PropOptions<Date>>().type.not.toBeAssignableWith( | ||
dateProp().optional, | ||
); | ||
}); | ||
|
||
test('Vue 3', () => { | ||
expect<Vue3.Prop<Date | undefined>>().type.toBeAssignableWith( | ||
dateProp().optional, | ||
); | ||
expect<Vue3.Prop<Date>>().type.not.toBeAssignableWith(dateProp().optional); | ||
}); | ||
}); | ||
|
||
describe('dateProp().nullable', () => { | ||
test('Vue 2.6', () => { | ||
expect<Vue2_6.PropOptions<Date | null>>().type.toBeAssignableWith( | ||
dateProp().nullable, | ||
); | ||
expect<Vue2_6.PropOptions<Date>>().type.not.toBeAssignableWith( | ||
dateProp().nullable, | ||
); | ||
|
||
expect(createVue2Component(dateProp().nullable)).type.toBe< | ||
Vue2ComponentWithProp<Date | null> | ||
>(); | ||
}); | ||
|
||
test('Vue 2.7', () => { | ||
expect<Vue2_7.PropOptions<Date | null>>().type.toBeAssignableWith( | ||
dateProp().nullable, | ||
); | ||
expect<Vue2_7.PropOptions<Date>>().type.not.toBeAssignableWith( | ||
dateProp().nullable, | ||
); | ||
}); | ||
|
||
test('Vue 3', () => { | ||
expect<Vue3.Prop<Date | null>>().type.toBeAssignableWith( | ||
dateProp().nullable, | ||
); | ||
expect<Vue3.Prop<Date>>().type.not.toBeAssignableWith(dateProp().nullable); | ||
}); | ||
}); | ||
|
||
describe('dateProp().withDefault(false)', () => { | ||
test('Vue 2.6', () => { | ||
expect<Vue2_6.PropOptions<Date>>().type.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
expect<Vue2_6.PropOptions<string>>().type.not.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
|
||
expect( | ||
createVue2Component(dateProp().withDefault(() => new Date())), | ||
).type.toBe<Vue2ComponentWithProp<Date>>(); | ||
}); | ||
|
||
test('Vue 2.7', () => { | ||
expect<Vue2_7.PropOptions<Date>>().type.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
expect<Vue2_7.PropOptions<string>>().type.not.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
}); | ||
|
||
test('Vue 3', () => { | ||
expect<Vue3.Prop<Date>>().type.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
expect<Vue3.Prop<string>>().type.not.toBeAssignableWith( | ||
dateProp().withDefault(() => new Date()), | ||
); | ||
}); | ||
}); | ||
|
||
describe('dateProp().required', () => { | ||
test('Vue 2.6', () => { | ||
expect<Vue2_6.PropOptions<Date>>().type.toBeAssignableWith( | ||
dateProp().required, | ||
); | ||
expect<Vue2_6.PropOptions<string>>().type.not.toBeAssignableWith( | ||
dateProp().required, | ||
); | ||
|
||
expect(createVue2Component(dateProp().required)).type.toBe< | ||
Vue2ComponentWithProp<Date> | ||
>(); | ||
}); | ||
|
||
test('Vue 2.7', () => { | ||
expect<Vue2_7.PropOptions<Date>>().type.toBeAssignableWith( | ||
dateProp().required, | ||
); | ||
expect<Vue2_7.PropOptions<string>>().type.not.toBeAssignableWith( | ||
dateProp().required, | ||
); | ||
}); | ||
|
||
test('Vue 3', () => { | ||
expect<Vue3.Prop<Date>>().type.toBeAssignableWith(dateProp().required); | ||
expect<Vue3.Prop<string>>().type.not.toBeAssignableWith( | ||
dateProp().required, | ||
); | ||
}); | ||
}); |