/
index.test.ts
147 lines (133 loc) · 3.88 KB
/
index.test.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { factory } from 'typescript'
import {createTypeAlias, print, createEventType, createType } from './'
/**
* type ConvertStart = {
* action: 'convert_start'
* category: 'convert'
* }
*/
const createConvertStart = () => factory.createTypeAliasDeclaration(
undefined,
undefined,
factory.createIdentifier("ConvertStart"),
undefined,
factory.createTypeLiteralNode([
factory.createPropertySignature(
undefined,
factory.createIdentifier("action"),
undefined,
factory.createLiteralTypeNode(factory.createStringLiteral("convert_start"))
),
factory.createPropertySignature(
undefined,
factory.createIdentifier("category"),
undefined,
factory.createLiteralTypeNode(factory.createStringLiteral("convert"))
)
])
)
describe('createTypeAlias で型作成のための値から type alias を作成する', () => {
test('型名 ConvertStart 、プロパティ・値が与えられたとき、ConvertStart 型の AST を作成する', () => {
const actual = createTypeAlias(
'ConvertStart',
{
action: 'convert_start',
category: 'convert',
}
)
const expected = createConvertStart()
expect(actual).toStrictEqual(expected)
})
test('型名 ConvertEnd 、プロパティ・値が与えられたとき、ConvertEnd 型の AST を作成する', () => {
const actual = createTypeAlias(
'ConvertEnd',
{
action: 'convert_end',
category: 'convert',
}
)
/**
* type ConvertEnd = {
* action: 'convert_end'
* category: 'convert'
* }
*/
const expected = factory.createTypeAliasDeclaration(
undefined,
undefined,
factory.createIdentifier("ConvertEnd"),
undefined,
factory.createTypeLiteralNode([
factory.createPropertySignature(
undefined,
factory.createIdentifier("action"),
undefined,
factory.createLiteralTypeNode(factory.createStringLiteral("convert_end"))
),
factory.createPropertySignature(
undefined,
factory.createIdentifier("category"),
undefined,
factory.createLiteralTypeNode(factory.createStringLiteral("convert"))
)
])
)
expect(actual).toStrictEqual(expected)
})
})
describe('print で AST を文字列に変換する', () => {
test('ConvertStart の AST を与えたとき、ConvertStart 型を出力する', () => {
const arg = createConvertStart()
const actual = print(arg)
const expected = `type ConvertStart = {
action: "convert_start";
category: "convert";
};`
expect(actual).toBe(expected)
})
})
describe('createEventType', () => {
test('ConvertStart と ConvertEnd を受け取ったとき、その二つを持った Event 型を生成する', () => {
const arg = ['ConvertStart', 'ConvertEnd']
const actual = createEventType(arg)
const expected = `export type Event = (ConvertStart | ConvertEnd) & {
label?: Record<string, string | number | boolean>;
value?: string;
};`
expect(actual).toBe(expected)
})
})
describe('createType', () => {
test('型名と property を与えたとき、引数の型との交差型 Event 型を生成する', () => {
const arg = [
{
name: 'ConvertStart',
property: {
action: 'convert_start',
category: 'convert',
}
},
{
name: 'ConvertEnd',
property: {
action: 'convert_end',
category: 'convert',
},
},
]
const actual = createType(arg)
const expected = `type ConvertStart = {
action: "convert_start";
category: "convert";
};
type ConvertEnd = {
action: "convert_end";
category: "convert";
};
export type Event = (ConvertStart | ConvertEnd) & {
label?: Record<string, string | number | boolean>;
value?: string;
};`
expect(actual).toBe(expected)
})
})