@prenta/admin 2.16.1 → 2.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.d.ts +2 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.d.ts.map +1 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.js +21 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.js.map +1 -0
- package/dist/__tests__/lib/post-custom-fields.test.d.ts +2 -0
- package/dist/__tests__/lib/post-custom-fields.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-custom-fields.test.js +158 -0
- package/dist/__tests__/lib/post-custom-fields.test.js.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts +2 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.js +104 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.js.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts +2 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js +128 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js.map +1 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts +2 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts.map +1 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.js +164 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.js.map +1 -0
- package/dist/fields/FieldRenderer.d.ts.map +1 -1
- package/dist/fields/FieldRenderer.js +7 -1
- package/dist/fields/FieldRenderer.js.map +1 -1
- package/dist/lib/post-custom-fields.d.ts +28 -0
- package/dist/lib/post-custom-fields.d.ts.map +1 -0
- package/dist/lib/post-custom-fields.js +173 -0
- package/dist/lib/post-custom-fields.js.map +1 -0
- package/dist/lib/post-editor-service.d.ts +29 -0
- package/dist/lib/post-editor-service.d.ts.map +1 -1
- package/dist/lib/post-editor-service.js +53 -0
- package/dist/lib/post-editor-service.js.map +1 -1
- package/dist/prenta-admin.css +1 -1
- package/dist/views/post-editor/PostRowsField.d.ts +17 -0
- package/dist/views/post-editor/PostRowsField.d.ts.map +1 -0
- package/dist/views/post-editor/PostRowsField.js +29 -0
- package/dist/views/post-editor/PostRowsField.js.map +1 -0
- package/dist/views/post-editor/PostSectionEditor.d.ts.map +1 -1
- package/dist/views/post-editor/PostSectionEditor.js +16 -5
- package/dist/views/post-editor/PostSectionEditor.js.map +1 -1
- package/dist/views/post-editor/PostTab.d.ts +8 -5
- package/dist/views/post-editor/PostTab.d.ts.map +1 -1
- package/dist/views/post-editor/PostTab.js +53 -15
- package/dist/views/post-editor/PostTab.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/fields/field-renderer-null.render.test.tsx +34 -0
- package/src/__tests__/lib/post-custom-fields.test.ts +170 -0
- package/src/__tests__/lib/post-editor-custom-fields-api.test.ts +129 -0
- package/src/__tests__/lib/post-editor-custom-fields-undo.test.ts +152 -0
- package/src/__tests__/views/post-tab-custom-fields.render.test.tsx +239 -0
- package/src/fields/FieldRenderer.tsx +6 -1
- package/src/lib/post-custom-fields.ts +191 -0
- package/src/lib/post-editor-service.ts +71 -0
- package/src/views/post-editor/PostRowsField.tsx +110 -0
- package/src/views/post-editor/PostSectionEditor.tsx +26 -7
- package/src/views/post-editor/PostTab.tsx +72 -6
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import { customPostFields } from '../../lib/post-custom-fields.js'
|
|
4
|
+
|
|
5
|
+
/** The shape of a case-study post type: shared post fields plus its own. */
|
|
6
|
+
const caseStudyFields = {
|
|
7
|
+
title: { type: 'text', label: 'Case study title', required: true },
|
|
8
|
+
slug: { type: 'slug', label: 'Slug' },
|
|
9
|
+
sections: { type: 'json', label: 'Sections' },
|
|
10
|
+
excerpt: { type: 'text', label: 'Summary' },
|
|
11
|
+
featuredImage: { type: 'media', label: 'Hero image' },
|
|
12
|
+
body: { type: 'richText', label: 'Body' },
|
|
13
|
+
category: { type: 'text', label: 'Category' },
|
|
14
|
+
tags: { type: 'array', label: 'Tags', fields: { tag: { type: 'text', label: 'Tag' } } },
|
|
15
|
+
publishDate: { type: 'date', label: 'Publish date' },
|
|
16
|
+
metaTitle: { type: 'text', label: 'Meta title' },
|
|
17
|
+
metaDescription: { type: 'text', label: 'Meta description' },
|
|
18
|
+
ogImage: { type: 'media', label: 'OG image' },
|
|
19
|
+
canonical: { type: 'url', label: 'Canonical' },
|
|
20
|
+
noIndex: { type: 'boolean', label: 'No index' },
|
|
21
|
+
clientName: { type: 'text', label: 'Client name', required: true, maxLength: 120 },
|
|
22
|
+
industry: { type: 'text' },
|
|
23
|
+
challenge: { type: 'richText', label: 'Challenge' },
|
|
24
|
+
keyMetrics: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
label: 'Key metrics',
|
|
27
|
+
maxRows: 6,
|
|
28
|
+
fields: {
|
|
29
|
+
value: { type: 'text', label: 'Value', required: true, maxLength: 32 },
|
|
30
|
+
label: { type: 'text', label: 'Label', required: true },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
publishedDate: { type: 'date', label: 'Published' },
|
|
34
|
+
ctaUrl: { type: 'url', label: 'CTA link', admin: { description: 'Where the button goes' } },
|
|
35
|
+
featured: { type: 'boolean', label: 'Featured' },
|
|
36
|
+
services: { type: 'relationship', label: 'Services', relationTo: 'services', hasMany: true },
|
|
37
|
+
logo: { type: 'media', label: 'Client logo' },
|
|
38
|
+
gallery: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
label: 'Gallery',
|
|
41
|
+
fields: { image: { type: 'media', label: 'Image' } },
|
|
42
|
+
},
|
|
43
|
+
budget: { type: 'number', label: 'Budget' },
|
|
44
|
+
designPayload: { type: 'json', label: 'Design payload' },
|
|
45
|
+
internalNotes: { type: 'text', label: 'Internal notes', hidden: true },
|
|
46
|
+
importedFrom: { type: 'text', label: 'Imported from', admin: { readOnly: true } },
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const names = () => customPostFields(caseStudyFields).map((f) => f.name)
|
|
50
|
+
|
|
51
|
+
describe('customPostFields', () => {
|
|
52
|
+
it("returns the post type's own editable fields in config order", () => {
|
|
53
|
+
expect(names()).toEqual([
|
|
54
|
+
'clientName',
|
|
55
|
+
'industry',
|
|
56
|
+
'challenge',
|
|
57
|
+
'keyMetrics',
|
|
58
|
+
'publishedDate',
|
|
59
|
+
'ctaUrl',
|
|
60
|
+
'featured',
|
|
61
|
+
])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('leaves out what the Post tab, the canvas and the SEO tab already edit', () => {
|
|
65
|
+
for (const owned of [
|
|
66
|
+
'title',
|
|
67
|
+
'slug',
|
|
68
|
+
'body',
|
|
69
|
+
'sections',
|
|
70
|
+
'tags',
|
|
71
|
+
'metaTitle',
|
|
72
|
+
'canonical',
|
|
73
|
+
'noIndex',
|
|
74
|
+
'ogImage',
|
|
75
|
+
]) {
|
|
76
|
+
expect(names()).not.toContain(owned)
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('leaves out hidden and read-only fields', () => {
|
|
81
|
+
expect(names()).not.toContain('internalNotes')
|
|
82
|
+
expect(names()).not.toContain('importedFrom')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('leaves out types whose control would store the wrong thing', () => {
|
|
86
|
+
// media writes a placeholder, relationship searches another collection,
|
|
87
|
+
// number turns an empty box into 0, json has no control.
|
|
88
|
+
for (const unsafe of ['services', 'logo', 'gallery', 'budget', 'designPayload']) {
|
|
89
|
+
expect(names()).not.toContain(unsafe)
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('carries label, required, max length and help text, and names an unlabelled field', () => {
|
|
94
|
+
const byName = Object.fromEntries(customPostFields(caseStudyFields).map((f) => [f.name, f]))
|
|
95
|
+
expect(byName.clientName).toMatchObject({
|
|
96
|
+
label: 'Client name',
|
|
97
|
+
required: true,
|
|
98
|
+
type: 'text',
|
|
99
|
+
maxLength: 120,
|
|
100
|
+
})
|
|
101
|
+
expect(byName.industry?.label).toBe('Industry')
|
|
102
|
+
expect(byName.ctaUrl?.helpText).toBe('Where the button goes')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('turns an array of text columns into editable rows', () => {
|
|
106
|
+
const metrics = customPostFields(caseStudyFields).find((f) => f.name === 'keyMetrics')
|
|
107
|
+
expect(metrics).toMatchObject({
|
|
108
|
+
type: 'rows',
|
|
109
|
+
maxRows: 6,
|
|
110
|
+
rowFields: [
|
|
111
|
+
{ name: 'value', label: 'Value', required: true, maxLength: 32 },
|
|
112
|
+
{ name: 'label', label: 'Label', required: true },
|
|
113
|
+
],
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('flattens labelled select options and skips a multi-value select', () => {
|
|
118
|
+
const out = customPostFields({
|
|
119
|
+
region: {
|
|
120
|
+
type: 'select',
|
|
121
|
+
label: 'Region',
|
|
122
|
+
options: [{ label: 'Seattle', value: 'seattle' }, 'tampa'],
|
|
123
|
+
},
|
|
124
|
+
channels: { type: 'select', label: 'Channels', multiple: true, options: ['seo', 'ppc'] },
|
|
125
|
+
})
|
|
126
|
+
expect(out.map((f) => f.name)).toEqual(['region'])
|
|
127
|
+
expect(out[0]?.options).toEqual(['seattle', 'tampa'])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('skips a date that stores a time, which the date-only control would drop', () => {
|
|
131
|
+
const out = customPostFields({
|
|
132
|
+
eventDay: { type: 'date', label: 'Event day' },
|
|
133
|
+
publishedDate: { type: 'date', label: 'Published', includeTime: true },
|
|
134
|
+
})
|
|
135
|
+
expect(out.map((f) => f.name)).toEqual(['eventDay'])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('skips an array with a hidden or read-only column', () => {
|
|
139
|
+
const column = { type: 'text', label: 'Value' }
|
|
140
|
+
const out = customPostFields({
|
|
141
|
+
open: { type: 'array', label: 'Open', fields: { value: column } },
|
|
142
|
+
withHidden: {
|
|
143
|
+
type: 'array',
|
|
144
|
+
label: 'With hidden',
|
|
145
|
+
fields: { value: column, secret: { type: 'text', label: 'Secret', hidden: true } },
|
|
146
|
+
},
|
|
147
|
+
withReadOnly: {
|
|
148
|
+
type: 'array',
|
|
149
|
+
label: 'With read-only',
|
|
150
|
+
fields: { value: column, source: { type: 'text', admin: { readOnly: true } } },
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
expect(out.map((f) => f.name)).toEqual(['open'])
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('skips names the server reads as status or page-builder envelope', () => {
|
|
157
|
+
const out = customPostFields({
|
|
158
|
+
status: { type: 'text', label: 'Status' },
|
|
159
|
+
layout: { type: 'text', label: 'Layout' },
|
|
160
|
+
data: { type: 'text', label: 'Data' },
|
|
161
|
+
})
|
|
162
|
+
expect(out).toEqual([])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('returns nothing for a post type with only the shared fields, or no config', () => {
|
|
166
|
+
const shared = { title: caseStudyFields.title, body: caseStudyFields.body }
|
|
167
|
+
expect(customPostFields(shared)).toEqual([])
|
|
168
|
+
expect(customPostFields(undefined)).toEqual([])
|
|
169
|
+
})
|
|
170
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
const cmsApi = vi.fn()
|
|
5
|
+
vi.mock('../../lib/api.js', () => ({
|
|
6
|
+
cmsApi: (...args: unknown[]) => cmsApi(...args),
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
const { fetchPostForEditor, savePostDraft, customFieldValue } =
|
|
10
|
+
await import('../../lib/post-editor-service.js')
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
cmsApi.mockReset()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
const storedRow = {
|
|
17
|
+
id: 'cs1',
|
|
18
|
+
title: 'Mary Frances',
|
|
19
|
+
slug: 'mary-frances',
|
|
20
|
+
status: 'DRAFT',
|
|
21
|
+
workflowStage: 'DRAFT',
|
|
22
|
+
reviewNote: null,
|
|
23
|
+
publishedAt: null,
|
|
24
|
+
updatedAt: '2026-11-26T15:41:39.442Z',
|
|
25
|
+
data: {
|
|
26
|
+
title: 'Mary Frances',
|
|
27
|
+
slug: 'mary-frances',
|
|
28
|
+
body: '<p>Body</p>',
|
|
29
|
+
excerpt: 'Summary',
|
|
30
|
+
metaTitle: 'Meta',
|
|
31
|
+
focusKeyword: 'ecommerce growth',
|
|
32
|
+
clientName: 'Mary Frances',
|
|
33
|
+
keyMetrics: [{ label: 'Revenue', value: '+60%' }],
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function sentBody(call: number): Record<string, unknown> {
|
|
38
|
+
const init = cmsApi.mock.calls[call]?.[1] as { body: string }
|
|
39
|
+
return JSON.parse(init.body) as Record<string, unknown>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('post editor custom fields', () => {
|
|
43
|
+
it('loads the stored values of fields the editor does not manage itself', async () => {
|
|
44
|
+
cmsApi.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
45
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
46
|
+
|
|
47
|
+
expect(customFieldValue(post, 'clientName')).toBe('Mary Frances')
|
|
48
|
+
expect(customFieldValue(post, 'keyMetrics')).toEqual([{ label: 'Revenue', value: '+60%' }])
|
|
49
|
+
expect(customFieldValue(post, 'challenge')).toBeUndefined()
|
|
50
|
+
expect(post.customFields).not.toHaveProperty('body')
|
|
51
|
+
expect(post.customFields).not.toHaveProperty('title')
|
|
52
|
+
expect(post.customFieldEdits).toEqual({})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('shows a pending edit ahead of the stored value, including a cleared one', async () => {
|
|
56
|
+
cmsApi.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
57
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
58
|
+
|
|
59
|
+
expect(
|
|
60
|
+
customFieldValue({ ...post, customFieldEdits: { clientName: 'MF' } }, 'clientName'),
|
|
61
|
+
).toBe('MF')
|
|
62
|
+
expect(customFieldValue({ ...post, customFieldEdits: { clientName: '' } }, 'clientName')).toBe(
|
|
63
|
+
'',
|
|
64
|
+
)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('saves edited custom fields with the draft', async () => {
|
|
68
|
+
cmsApi
|
|
69
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
70
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
71
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
72
|
+
|
|
73
|
+
await savePostDraft({
|
|
74
|
+
...post,
|
|
75
|
+
customFieldEdits: { challenge: '<p>Growth had leveled off.</p>', clientName: 'MF' },
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const [url, init] = cmsApi.mock.calls[1] as [string, { method: string }]
|
|
79
|
+
expect(url).toBe('/collections/case-studies/cs1')
|
|
80
|
+
expect(init.method).toBe('PUT')
|
|
81
|
+
expect(sentBody(1)).toMatchObject({
|
|
82
|
+
challenge: '<p>Growth had leveled off.</p>',
|
|
83
|
+
clientName: 'MF',
|
|
84
|
+
title: 'Mary Frances',
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('does not re-send values nobody edited', async () => {
|
|
89
|
+
cmsApi
|
|
90
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
91
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
92
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
93
|
+
|
|
94
|
+
await savePostDraft(post)
|
|
95
|
+
|
|
96
|
+
const body = sentBody(1)
|
|
97
|
+
// The server merges a save onto the stored document. Re-sending a loaded
|
|
98
|
+
// copy would overwrite what the SEO tab saved after this post was opened.
|
|
99
|
+
expect(body).not.toHaveProperty('focusKeyword')
|
|
100
|
+
expect(body).not.toHaveProperty('clientName')
|
|
101
|
+
expect(body).not.toHaveProperty('keyMetrics')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('never lets a custom field edit replace a field the editor owns', async () => {
|
|
105
|
+
cmsApi
|
|
106
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
107
|
+
.mockResolvedValueOnce({ data: storedRow, status: 200 })
|
|
108
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
109
|
+
|
|
110
|
+
await savePostDraft({ ...post, customFieldEdits: { title: 'Hijacked', sections: 'nope' } })
|
|
111
|
+
|
|
112
|
+
const body = sentBody(1)
|
|
113
|
+
expect(body.title).toBe('Mary Frances')
|
|
114
|
+
expect(Array.isArray(body.sections)).toBe(true)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('starts clean again after a save', async () => {
|
|
118
|
+
cmsApi.mockResolvedValueOnce({ data: storedRow, status: 200 }).mockResolvedValueOnce({
|
|
119
|
+
data: { ...storedRow, data: { ...storedRow.data, clientName: 'MF' } },
|
|
120
|
+
status: 200,
|
|
121
|
+
})
|
|
122
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
123
|
+
|
|
124
|
+
const saved = await savePostDraft({ ...post, customFieldEdits: { clientName: 'MF' } })
|
|
125
|
+
|
|
126
|
+
expect(saved.customFieldEdits).toEqual({})
|
|
127
|
+
expect(customFieldValue(saved, 'clientName')).toBe('MF')
|
|
128
|
+
})
|
|
129
|
+
})
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
const cmsApi = vi.fn()
|
|
5
|
+
vi.mock('../../lib/api.js', () => ({
|
|
6
|
+
cmsApi: (...args: unknown[]) => cmsApi(...args),
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
const { fetchPostForEditor, savePostDraft, rebaseCustomFieldEdits, customFieldValue } =
|
|
10
|
+
await import('../../lib/post-editor-service.js')
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
cmsApi.mockReset()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
/** The fields the Post tab edits for this post type. */
|
|
17
|
+
const NAMES = ['clientName', 'industry', 'keyMetrics']
|
|
18
|
+
|
|
19
|
+
function row(data: Record<string, unknown>) {
|
|
20
|
+
return {
|
|
21
|
+
id: 'cs1',
|
|
22
|
+
title: 'Mary Frances',
|
|
23
|
+
slug: 'mary-frances',
|
|
24
|
+
status: 'DRAFT',
|
|
25
|
+
workflowStage: 'DRAFT',
|
|
26
|
+
reviewNote: null,
|
|
27
|
+
publishedAt: null,
|
|
28
|
+
updatedAt: null,
|
|
29
|
+
data: { title: 'Mary Frances', slug: 'mary-frances', ...data },
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sentBody(call: number): Record<string, unknown> {
|
|
34
|
+
const init = cmsApi.mock.calls[call]?.[1] as { body: string }
|
|
35
|
+
return JSON.parse(init.body) as Record<string, unknown>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('rebaseCustomFieldEdits (undo and redo)', () => {
|
|
39
|
+
it('turns an undone, already-saved change back into a pending edit', async () => {
|
|
40
|
+
// Open with A, change to B, save. The history snapshot still says A.
|
|
41
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'A' }), status: 200 })
|
|
42
|
+
const beforeEdit = await fetchPostForEditor('case-studies', 'cs1')
|
|
43
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'B' }), status: 200 })
|
|
44
|
+
const saved = await savePostDraft({ ...beforeEdit, customFieldEdits: { clientName: 'B' } })
|
|
45
|
+
|
|
46
|
+
const undone = rebaseCustomFieldEdits(beforeEdit, saved, NAMES)
|
|
47
|
+
|
|
48
|
+
expect(customFieldValue(undone, 'clientName')).toBe('A')
|
|
49
|
+
expect(undone.customFieldEdits).toEqual({ clientName: 'A' })
|
|
50
|
+
|
|
51
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'A' }), status: 200 })
|
|
52
|
+
await savePostDraft(undone)
|
|
53
|
+
expect(sentBody(2).clientName).toBe('A')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('clears a value that did not exist in the restored snapshot', async () => {
|
|
57
|
+
cmsApi.mockResolvedValueOnce({ data: row({}), status: 200 })
|
|
58
|
+
const beforeEdit = await fetchPostForEditor('case-studies', 'cs1')
|
|
59
|
+
cmsApi.mockResolvedValueOnce({ data: row({ industry: 'Fashion' }), status: 200 })
|
|
60
|
+
const saved = await savePostDraft({ ...beforeEdit, customFieldEdits: { industry: 'Fashion' } })
|
|
61
|
+
|
|
62
|
+
const undone = rebaseCustomFieldEdits(beforeEdit, saved, NAMES)
|
|
63
|
+
|
|
64
|
+
// null reaches the server; undefined would be dropped from the request.
|
|
65
|
+
expect(undone.customFieldEdits).toEqual({ industry: null })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('leaves nothing pending when the snapshot matches what is stored', async () => {
|
|
69
|
+
cmsApi.mockResolvedValueOnce({
|
|
70
|
+
data: row({ clientName: 'A', keyMetrics: [{ value: '+60%', label: 'Revenue' }] }),
|
|
71
|
+
status: 200,
|
|
72
|
+
})
|
|
73
|
+
const post = await fetchPostForEditor('case-studies', 'cs1')
|
|
74
|
+
|
|
75
|
+
expect(rebaseCustomFieldEdits(post, post, NAMES).customFieldEdits).toEqual({})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('never turns an old copy of a key another tab owns into an edit', async () => {
|
|
79
|
+
// Open, change the client name, save; then the SEO tab saves a new focus
|
|
80
|
+
// keyword and the editor reloads. Undo restores the first snapshot.
|
|
81
|
+
cmsApi.mockResolvedValueOnce({
|
|
82
|
+
data: row({ clientName: 'A', focusKeyword: 'old keyword' }),
|
|
83
|
+
status: 200,
|
|
84
|
+
})
|
|
85
|
+
const beforeEdit = await fetchPostForEditor('case-studies', 'cs1')
|
|
86
|
+
cmsApi.mockResolvedValueOnce({
|
|
87
|
+
data: row({ clientName: 'B', focusKeyword: 'new keyword' }),
|
|
88
|
+
status: 200,
|
|
89
|
+
})
|
|
90
|
+
const afterSeoSave = await fetchPostForEditor('case-studies', 'cs1')
|
|
91
|
+
|
|
92
|
+
const undone = rebaseCustomFieldEdits(beforeEdit, afterSeoSave, NAMES)
|
|
93
|
+
|
|
94
|
+
expect(undone.customFieldEdits).toEqual({ clientName: 'A' })
|
|
95
|
+
expect(undone.customFields?.focusKeyword).toBe('new keyword')
|
|
96
|
+
|
|
97
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'A' }), status: 200 })
|
|
98
|
+
await savePostDraft(undone)
|
|
99
|
+
expect(sentBody(2)).not.toHaveProperty('focusKeyword')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('keeps an edit that was pending and never saved', async () => {
|
|
103
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'A' }), status: 200 })
|
|
104
|
+
const saved = await fetchPostForEditor('case-studies', 'cs1')
|
|
105
|
+
const snapshot = { ...saved, customFieldEdits: { clientName: 'B' } }
|
|
106
|
+
|
|
107
|
+
expect(rebaseCustomFieldEdits(snapshot, saved, NAMES).customFieldEdits).toEqual({
|
|
108
|
+
clientName: 'B',
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
describe('creating a post with custom fields', () => {
|
|
114
|
+
it('sends custom field edits with the first save', async () => {
|
|
115
|
+
cmsApi.mockResolvedValueOnce({ data: row({ clientName: 'Mary Frances' }), status: 200 })
|
|
116
|
+
|
|
117
|
+
const created = await savePostDraft({
|
|
118
|
+
id: null,
|
|
119
|
+
postType: 'case-studies',
|
|
120
|
+
title: 'Mary Frances',
|
|
121
|
+
slug: 'mary-frances',
|
|
122
|
+
excerpt: '',
|
|
123
|
+
featuredImage: '',
|
|
124
|
+
featuredImageUrl: '',
|
|
125
|
+
body: '',
|
|
126
|
+
category: '',
|
|
127
|
+
authorName: '',
|
|
128
|
+
canvasRelatedPosts: null,
|
|
129
|
+
tags: [],
|
|
130
|
+
status: 'DRAFT',
|
|
131
|
+
workflowStage: 'DRAFT',
|
|
132
|
+
reviewNote: null,
|
|
133
|
+
publishDate: null,
|
|
134
|
+
scheduledAt: null,
|
|
135
|
+
scheduledUnpublishAt: null,
|
|
136
|
+
sections: [],
|
|
137
|
+
seoTitle: '',
|
|
138
|
+
seoDescription: '',
|
|
139
|
+
publishedAt: null,
|
|
140
|
+
updatedAt: null,
|
|
141
|
+
customFields: {},
|
|
142
|
+
customFieldEdits: { clientName: 'Mary Frances' },
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
const [url, init] = cmsApi.mock.calls[0] as [string, { method: string }]
|
|
146
|
+
expect(url).toBe('/collections/case-studies')
|
|
147
|
+
expect(init.method).toBe('POST')
|
|
148
|
+
expect(sentBody(0).clientName).toBe('Mary Frances')
|
|
149
|
+
expect(created.customFieldEdits).toEqual({})
|
|
150
|
+
expect(customFieldValue(created, 'clientName')).toBe('Mary Frances')
|
|
151
|
+
})
|
|
152
|
+
})
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { cleanup, fireEvent, render } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
// The identity form has its own tests; keep this one on the custom fields.
|
|
6
|
+
vi.mock('../../views/post-editor/PostFieldsForm.js', () => ({
|
|
7
|
+
PostFieldsForm: () => null,
|
|
8
|
+
}))
|
|
9
|
+
|
|
10
|
+
const { PostTab } = await import('../../views/post-editor/PostTab.js')
|
|
11
|
+
|
|
12
|
+
afterEach(cleanup)
|
|
13
|
+
|
|
14
|
+
function post(overrides: Record<string, unknown> = {}) {
|
|
15
|
+
return {
|
|
16
|
+
id: 'cs1',
|
|
17
|
+
postType: 'case-studies',
|
|
18
|
+
title: 'Mary Frances',
|
|
19
|
+
slug: 'mary-frances',
|
|
20
|
+
excerpt: '',
|
|
21
|
+
featuredImage: '',
|
|
22
|
+
featuredImageUrl: '',
|
|
23
|
+
body: '',
|
|
24
|
+
category: '',
|
|
25
|
+
authorName: '',
|
|
26
|
+
canvasRelatedPosts: null,
|
|
27
|
+
tags: [],
|
|
28
|
+
status: 'DRAFT' as const,
|
|
29
|
+
workflowStage: 'DRAFT' as const,
|
|
30
|
+
reviewNote: null,
|
|
31
|
+
publishDate: null,
|
|
32
|
+
scheduledAt: null,
|
|
33
|
+
scheduledUnpublishAt: null,
|
|
34
|
+
sections: [],
|
|
35
|
+
seoTitle: '',
|
|
36
|
+
seoDescription: '',
|
|
37
|
+
publishedAt: null,
|
|
38
|
+
updatedAt: null,
|
|
39
|
+
customFields: { clientName: 'Mary Frances' },
|
|
40
|
+
customFieldEdits: {},
|
|
41
|
+
...overrides,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const fields = {
|
|
46
|
+
title: { type: 'text', label: 'Title' },
|
|
47
|
+
body: { type: 'richText', label: 'Body' },
|
|
48
|
+
sections: { type: 'json', label: 'Sections' },
|
|
49
|
+
clientName: { type: 'text', label: 'Client name', required: true },
|
|
50
|
+
industry: { type: 'text', label: 'Industry' },
|
|
51
|
+
ctaUrl: { type: 'url', label: 'CTA link' },
|
|
52
|
+
contact: { type: 'email', label: 'Contact email' },
|
|
53
|
+
featured: { type: 'boolean', label: 'Featured' },
|
|
54
|
+
publishedDate: { type: 'date', label: 'Published' },
|
|
55
|
+
keyMetrics: {
|
|
56
|
+
type: 'array',
|
|
57
|
+
label: 'Key metrics',
|
|
58
|
+
maxRows: 2,
|
|
59
|
+
fields: {
|
|
60
|
+
value: { type: 'text', label: 'Value', required: true },
|
|
61
|
+
label: { type: 'text', label: 'Label', required: true },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
logo: { type: 'media', label: 'Client logo' },
|
|
65
|
+
services: { type: 'relationship', label: 'Services', relationTo: 'services' },
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function inputFor(container: HTMLElement, label: string): HTMLInputElement {
|
|
69
|
+
const labels = [...container.querySelectorAll('label')]
|
|
70
|
+
const match = labels.find((l) => l.textContent?.includes(label))
|
|
71
|
+
const input = match?.parentElement?.querySelector('input')
|
|
72
|
+
if (!input) throw new Error(`no input for ${label}`)
|
|
73
|
+
return input
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe('PostTab custom fields', () => {
|
|
77
|
+
it("renders the post type's own fields with their stored values", () => {
|
|
78
|
+
const { container, getByTestId } = render(
|
|
79
|
+
<PostTab post={post()} canEdit fields={fields} onChange={() => {}} />,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
const group = getByTestId('post-custom-fields')
|
|
83
|
+
for (const label of ['Client name', 'Industry', 'CTA link', 'Contact email', 'Featured']) {
|
|
84
|
+
expect(group.textContent).toContain(label)
|
|
85
|
+
}
|
|
86
|
+
expect(group.textContent).not.toContain('Unknown field type')
|
|
87
|
+
expect(inputFor(container, 'Client name').value).toBe('Mary Frances')
|
|
88
|
+
expect(inputFor(container, 'Industry').value).toBe('')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('does not offer controls that would store a placeholder or a wrong reference', () => {
|
|
92
|
+
const { getByTestId } = render(
|
|
93
|
+
<PostTab post={post()} canEdit fields={fields} onChange={() => {}} />,
|
|
94
|
+
)
|
|
95
|
+
const text = getByTestId('post-custom-fields').textContent
|
|
96
|
+
expect(text).not.toContain('Client logo')
|
|
97
|
+
expect(text).not.toContain('Services')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('reports an edit as that one field', () => {
|
|
101
|
+
const onChange = vi.fn()
|
|
102
|
+
const { container } = render(
|
|
103
|
+
<PostTab
|
|
104
|
+
post={post({ customFieldEdits: { industry: 'Fashion' } })}
|
|
105
|
+
canEdit
|
|
106
|
+
fields={fields}
|
|
107
|
+
onChange={onChange}
|
|
108
|
+
/>,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
fireEvent.change(inputFor(container, 'Client name'), { target: { value: 'MF' } })
|
|
112
|
+
|
|
113
|
+
// The editor merges this into the pending edits, so `industry` survives.
|
|
114
|
+
expect(onChange).toHaveBeenCalledWith({ customFieldEdits: { clientName: 'MF' } })
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('renders stored null values as empty instead of crashing', () => {
|
|
118
|
+
const { container } = render(
|
|
119
|
+
<PostTab
|
|
120
|
+
post={post({ customFields: { clientName: null, ctaUrl: null, contact: null } })}
|
|
121
|
+
canEdit
|
|
122
|
+
fields={fields}
|
|
123
|
+
onChange={() => {}}
|
|
124
|
+
/>,
|
|
125
|
+
)
|
|
126
|
+
expect(inputFor(container, 'Client name').value).toBe('')
|
|
127
|
+
expect(inputFor(container, 'CTA link').value).toBe('')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('shows the date part of a stored timestamp', () => {
|
|
131
|
+
const { container } = render(
|
|
132
|
+
<PostTab
|
|
133
|
+
post={post({ customFields: { publishedDate: '2026-11-26T15:41:39.442Z' } })}
|
|
134
|
+
canEdit
|
|
135
|
+
fields={fields}
|
|
136
|
+
onChange={() => {}}
|
|
137
|
+
/>,
|
|
138
|
+
)
|
|
139
|
+
expect(inputFor(container, 'Published').value).toBe('2026-11-26')
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('edits key metrics as flat rows and keeps the other rows', () => {
|
|
143
|
+
const onChange = vi.fn()
|
|
144
|
+
const { container } = render(
|
|
145
|
+
<PostTab
|
|
146
|
+
post={post({
|
|
147
|
+
customFields: {
|
|
148
|
+
keyMetrics: [
|
|
149
|
+
{ value: '+60%', label: 'Revenue', source: 'GA4' },
|
|
150
|
+
{ value: '+153%', label: 'Organic' },
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
})}
|
|
154
|
+
canEdit
|
|
155
|
+
fields={fields}
|
|
156
|
+
onChange={onChange}
|
|
157
|
+
/>,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
const first = container.querySelector<HTMLInputElement>('#post-rows-keyMetrics-0-value')
|
|
161
|
+
expect(first?.value).toBe('+60%')
|
|
162
|
+
fireEvent.change(first!, { target: { value: '+61%' } })
|
|
163
|
+
|
|
164
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
165
|
+
customFieldEdits: {
|
|
166
|
+
keyMetrics: [
|
|
167
|
+
{ value: '+61%', label: 'Revenue', source: 'GA4' },
|
|
168
|
+
{ value: '+153%', label: 'Organic' },
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('adds an empty row with the declared columns and stops at the row limit', () => {
|
|
175
|
+
const onChange = vi.fn()
|
|
176
|
+
const { getByText, rerender } = render(
|
|
177
|
+
<PostTab
|
|
178
|
+
post={post({ customFields: { keyMetrics: [{ value: '+60%', label: 'Revenue' }] } })}
|
|
179
|
+
canEdit
|
|
180
|
+
fields={fields}
|
|
181
|
+
onChange={onChange}
|
|
182
|
+
/>,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
fireEvent.click(getByText('Add row'))
|
|
186
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
187
|
+
customFieldEdits: {
|
|
188
|
+
keyMetrics: [
|
|
189
|
+
{ value: '+60%', label: 'Revenue' },
|
|
190
|
+
{ value: '', label: '' },
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
rerender(
|
|
196
|
+
<PostTab
|
|
197
|
+
post={post({
|
|
198
|
+
customFields: {
|
|
199
|
+
keyMetrics: [
|
|
200
|
+
{ value: '+60%', label: 'Revenue' },
|
|
201
|
+
{ value: '+153%', label: 'Organic' },
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
})}
|
|
205
|
+
canEdit
|
|
206
|
+
fields={fields}
|
|
207
|
+
onChange={onChange}
|
|
208
|
+
/>,
|
|
209
|
+
)
|
|
210
|
+
expect((getByText('Add row').closest('button') as HTMLButtonElement).disabled).toBe(true)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('shows nothing extra for a post type with only the shared fields', () => {
|
|
214
|
+
const { queryByTestId } = render(
|
|
215
|
+
<PostTab
|
|
216
|
+
post={post()}
|
|
217
|
+
canEdit
|
|
218
|
+
fields={{ title: fields.title, body: fields.body }}
|
|
219
|
+
onChange={() => {}}
|
|
220
|
+
/>,
|
|
221
|
+
)
|
|
222
|
+
expect(queryByTestId('post-custom-fields')).toBeNull()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('disables the fields for a viewer who cannot edit, and ignores any edit that gets through', () => {
|
|
226
|
+
const onChange = vi.fn()
|
|
227
|
+
const { container, getByTestId } = render(
|
|
228
|
+
<PostTab post={post()} canEdit={false} fields={fields} onChange={onChange} />,
|
|
229
|
+
)
|
|
230
|
+
const group = getByTestId('post-custom-fields') as HTMLFieldSetElement
|
|
231
|
+
expect(group.disabled).toBe(true)
|
|
232
|
+
// inert keeps keyboard focus out of a rich text editor, which a disabled
|
|
233
|
+
// fieldset does not.
|
|
234
|
+
expect(group.hasAttribute('inert')).toBe(true)
|
|
235
|
+
|
|
236
|
+
fireEvent.change(inputFor(container, 'Client name'), { target: { value: 'MF' } })
|
|
237
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
238
|
+
})
|
|
239
|
+
})
|