-
Notifications
You must be signed in to change notification settings - Fork 0
/
index8.ts
40 lines (33 loc) · 1.39 KB
/
index8.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
describe("create generic type for test builders", () => {
type CreditCardNumber = string
type AccountNumber = string
type SortCode = string
type CreditCardMethod = {
type: 'CREDITCARD'
cardNumber: CreditCardNumber
}
type ChequeMethod = {
type: 'CHEQUE'
account: AccountNumber
sortCode: SortCode
}
type BuilderType<T, K extends keyof T> = Partial<Omit<T, K>>
type BuilderCreditCardMethod = BuilderType<CreditCardMethod, 'type'>
type BuilderChequeMethod = BuilderType<ChequeMethod, 'type'>
function buildChequeMethod({ account, sortCode }: BuilderChequeMethod = {}): ChequeMethod {
return {
type: 'CHEQUE',
account: (account || '00000000') as AccountNumber,
sortCode: (sortCode || '000000') as SortCode
}
}
test("with defaults", () =>
expect(buildChequeMethod())
.toEqual({ type: 'CHEQUE', account: '00000000', sortCode: '000000' }))
test("override defaults with sort code", () =>
expect(buildChequeMethod({ sortCode: '123456' }))
.toEqual({ type: 'CHEQUE', account: '00000000', sortCode: '123456' }))
test("override defaults with account and sort code", () =>
expect(buildChequeMethod({ account: '12345678', sortCode: '101010' }))
.toEqual({ type: 'CHEQUE', account: '12345678', sortCode: '101010' }))
})