-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrender.test.ts
229 lines (202 loc) · 6.03 KB
/
render.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import render from "../../src/renderers/render"
import { ContentType, Sys, Locale } from "contentful"
describe("render()", () => {
const contentTypes: ContentType[] = [
{
sys: {
id: "myContentType",
} as Sys,
fields: [
{
id: "arrayField",
name: "Array field",
required: true,
validations: [{}],
items: {
type: "Symbol",
validations: [
{
in: ["one", "of", "the", "above"],
},
],
},
disabled: false,
omitted: false,
localized: false,
type: "Array",
},
],
description: "",
displayField: "",
name: "",
toPlainObject: () => ({} as ContentType),
},
]
const locales: Locale[] = [
{
name: "English (US)",
fallbackCode: null,
code: "en-US",
default: true,
sys: {} as Locale["sys"],
},
{
name: "Brazilian Portuguese",
fallbackCode: "en-US",
code: "pt-BR",
default: false,
sys: {} as Locale["sys"],
},
]
it("renders a given content type", async () => {
expect(await render(contentTypes, locales)).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"
export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}
export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string
type: string
createdAt: string
updatedAt: string
locale: string
contentType: {
sys: {
id: \\"myContentType\\"
linkType: \\"ContentType\\"
type: \\"Link\\"
}
}
}
}
export type CONTENT_TYPE = \\"myContentType\\"
export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
"
`)
})
it("renders a given localized content type", async () => {
const contentTypes: ContentType[] = [
{
sys: {
id: "myContentType",
} as Sys,
fields: [
{
id: "arrayField",
name: "Array field",
required: true,
validations: [{}],
items: {
type: "Symbol",
validations: [
{
in: ["one", "of", "the", "above"],
},
],
},
disabled: false,
omitted: false,
localized: false,
type: "Array",
},
],
description: "",
displayField: "",
name: "",
toPlainObject: () => ({} as ContentType),
},
]
expect(await render(contentTypes, locales, { localization: true })).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
import { Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"
export interface IMyContentTypeFields {
/** Array field */
arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
}
export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string
type: string
createdAt: string
updatedAt: string
locale: string
contentType: {
sys: {
id: \\"myContentType\\"
linkType: \\"ContentType\\"
type: \\"Link\\"
}
}
}
}
export type CONTENT_TYPE = \\"myContentType\\"
export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
export type DefaultLocalizedField<T> = Record<CONTENTFUL_DEFAULT_LOCALE_CODE, T>
export type LocalizedField<T> = DefaultLocalizedField<T> & Partial<Record<LOCALE_CODE, T>>
// We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208
export interface Asset {
sys: Sys
fields: {
title: LocalizedField<string>
description: LocalizedField<string>
file: LocalizedField<{
url: string
details: {
size: number
image?: {
width: number
height: number
}
}
fileName: string
contentType: string
}>
}
toPlainObject(): object
}
"
`)
})
it("renders given a content type inside a namespace", async () => {
expect(await render(contentTypes, locales, { namespace: "Codegen" })).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"
declare namespace Codegen {
export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}
export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string
type: string
createdAt: string
updatedAt: string
locale: string
contentType: {
sys: {
id: \\"myContentType\\"
linkType: \\"ContentType\\"
type: \\"Link\\"
}
}
}
}
export type CONTENT_TYPE = \\"myContentType\\"
export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
}
export as namespace Codegen
export = Codegen
"
`)
})
})