Skip to content

Commit 0ed299c

Browse files
committed
test(content): trim schema starter coverage
1 parent bb33668 commit 0ed299c

3 files changed

Lines changed: 231 additions & 843 deletions

File tree

src/app/test/integration/actions.test.ts

Lines changed: 0 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
1-
import type { CollectionInfo, Draft07 } from '@nuxt/content'
21
import { describe, it, expect, beforeEach, vi } from 'vitest'
32
import { joinURL } from 'ufo'
43
import { DraftStatus, StudioItemActionId, type TreeItem, type DatabaseItem } from '../../src/types'
5-
import { generateInitialContentForPath } from '../../src/utils/schema'
64
import { normalizeKey, generateUniqueDocumentFsPath, generateUniqueMediaFsPath } from '../utils'
75
import { fsPathToId } from '../mocks/host'
86
import { createMockFile, createMockMedia, setupMediaMocks } from '../mocks/media'
97
import { findItemFromFsPath } from '../../src/utils/tree'
108
import { mockStorageDraft, mockHost, mockGit, routeState, cleanAndSetupContext } from '../utils/context'
119

12-
function createCollection(name: string, type: 'page' | 'data', schema: Draft07) {
13-
return {
14-
name,
15-
type,
16-
schema,
17-
} as CollectionInfo
18-
}
19-
20-
function generateDocumentFsPathWithExtension(filename: string, extension: string) {
21-
const uniqueId = Math.random().toString(36).substr(2, 9)
22-
return `${filename}-${uniqueId}.${extension}`
23-
}
24-
2510
describe('Document - Action Chains Integration Tests', () => {
2611
let filename: string
2712
let documentFsPath: string
@@ -34,8 +19,6 @@ describe('Document - Action Chains Integration Tests', () => {
3419
documentFsPath = generateUniqueDocumentFsPath(filename)
3520
documentId = fsPathToId(documentFsPath, 'document')
3621
context = await cleanAndSetupContext(mockHost, mockGit)
37-
vi.mocked(mockHost.collection.getByFsPath).mockReturnValue(undefined)
38-
vi.mocked(mockHost.collection.list).mockReturnValue([])
3922
})
4023

4124
it('Create > Revert', async () => {
@@ -86,236 +69,6 @@ describe('Document - Action Chains Integration Tests', () => {
8669
expect(consoleInfoSpy).toHaveBeenCalledWith('studio:draft:document:updated have been called by', 'useDraftBase.create')
8770
})
8871

89-
it('Create uses schema-derived markdown frontmatter for page collections', async () => {
90-
const postCollection = createCollection('posts', 'page', {
91-
$schema: 'http://json-schema.org/draft-07/schema#',
92-
$ref: '#/definitions/posts',
93-
definitions: {
94-
posts: {
95-
type: 'object',
96-
required: ['authors'],
97-
properties: {
98-
title: {
99-
type: 'string',
100-
default: 'Untitled',
101-
},
102-
authors: {
103-
type: 'array',
104-
items: {
105-
type: 'string',
106-
},
107-
},
108-
},
109-
},
110-
},
111-
})
112-
vi.mocked(mockHost.collection.getByFsPath).mockReturnValue(postCollection)
113-
const content = generateInitialContentForPath(documentFsPath, 'md', '# Schema draft', mockHost.collection.getByFsPath, {
114-
title: 'Schema draft',
115-
})
116-
117-
await context.itemActionHandler[StudioItemActionId.CreateDocument]({
118-
fsPath: documentFsPath,
119-
content,
120-
})
121-
122-
expect(mockHost.document.db.create).toHaveBeenCalledWith(documentFsPath, content)
123-
124-
const storedDraft = JSON.parse(mockStorageDraft.get(normalizeKey(documentFsPath))!)
125-
expect(storedDraft.modified.title).toBe('Untitled')
126-
expect(storedDraft.modified.authors).toStrictEqual([])
127-
expect(JSON.stringify(storedDraft.modified.body)).toContain('Schema draft')
128-
})
129-
130-
it('Create uses schema-derived yaml content for data collections', async () => {
131-
const yamlFsPath = generateDocumentFsPathWithExtension('author', 'yml')
132-
const yamlCollection = createCollection('authors', 'data', {
133-
$schema: 'http://json-schema.org/draft-07/schema#',
134-
$ref: '#/definitions/authors',
135-
definitions: {
136-
authors: {
137-
type: 'object',
138-
required: ['name', 'username', 'birthDate', 'lastCommitAt', 'modules', 'profile', 'role'],
139-
properties: {
140-
name: {
141-
type: 'string',
142-
},
143-
username: {
144-
type: 'string',
145-
},
146-
birthDate: {
147-
type: 'string',
148-
format: 'date',
149-
},
150-
lastCommitAt: {
151-
type: 'string',
152-
format: 'date-time',
153-
},
154-
modules: {
155-
type: 'array',
156-
items: {
157-
type: 'string',
158-
},
159-
},
160-
role: {
161-
type: 'string',
162-
enum: ['maintainer', 'author'],
163-
},
164-
profile: {
165-
type: 'object',
166-
required: ['avatar'],
167-
properties: {
168-
avatar: {
169-
type: 'object',
170-
required: ['src'],
171-
properties: {
172-
src: {
173-
type: 'string',
174-
default: '/avatar.png',
175-
},
176-
},
177-
},
178-
},
179-
},
180-
},
181-
},
182-
},
183-
})
184-
vi.mocked(mockHost.collection.getByFsPath).mockReturnValue(yamlCollection)
185-
const content = generateInitialContentForPath(yamlFsPath, 'yml', '# ignored', mockHost.collection.getByFsPath, {
186-
title: 'Author',
187-
})
188-
189-
await context.itemActionHandler[StudioItemActionId.CreateDocument]({
190-
fsPath: yamlFsPath,
191-
content,
192-
})
193-
194-
expect(mockHost.document.db.create).toHaveBeenCalledWith(yamlFsPath, content)
195-
196-
const storedDraft = JSON.parse(mockStorageDraft.get(normalizeKey(yamlFsPath))!)
197-
expect(storedDraft.modified.extension).toBe('yml')
198-
expect(storedDraft.modified.name).toBe('Author')
199-
expect(storedDraft.modified.username).toBeUndefined()
200-
expect(storedDraft.modified.birthDate).toBeUndefined()
201-
expect(storedDraft.modified.lastCommitAt).toBeUndefined()
202-
expect(storedDraft.modified.modules).toStrictEqual([])
203-
expect(storedDraft.modified.role).toBe('maintainer')
204-
expect(storedDraft.modified.profile).toStrictEqual({
205-
avatar: {
206-
src: '/avatar.png',
207-
},
208-
})
209-
})
210-
211-
it('Create uses schema-derived json content for data collections', async () => {
212-
const jsonFsPath = generateDocumentFsPathWithExtension('settings', 'json')
213-
const jsonCollection = createCollection('settings', 'data', {
214-
$schema: 'http://json-schema.org/draft-07/schema#',
215-
$ref: '#/definitions/settings',
216-
definitions: {
217-
settings: {
218-
type: 'object',
219-
required: ['navigation', 'social'],
220-
properties: {
221-
theme: {
222-
type: 'string',
223-
default: 'system',
224-
},
225-
navigation: {
226-
anyOf: [
227-
{ type: 'boolean' },
228-
{
229-
type: 'object',
230-
required: ['title'],
231-
properties: {
232-
title: {
233-
type: 'string',
234-
default: 'Home',
235-
},
236-
},
237-
},
238-
],
239-
},
240-
social: {
241-
type: 'array',
242-
items: {
243-
type: 'string',
244-
},
245-
},
246-
},
247-
},
248-
},
249-
})
250-
vi.mocked(mockHost.collection.getByFsPath).mockReturnValue(jsonCollection)
251-
const content = generateInitialContentForPath(jsonFsPath, 'json', '# ignored', mockHost.collection.getByFsPath, {
252-
title: 'Settings',
253-
})
254-
255-
await context.itemActionHandler[StudioItemActionId.CreateDocument]({
256-
fsPath: jsonFsPath,
257-
content,
258-
})
259-
260-
expect(mockHost.document.db.create).toHaveBeenCalledWith(jsonFsPath, content)
261-
262-
const storedDraft = JSON.parse(mockStorageDraft.get(normalizeKey(jsonFsPath))!)
263-
expect(storedDraft.modified.extension).toBe('json')
264-
expect(storedDraft.modified.theme).toBe('system')
265-
expect(storedDraft.modified.navigation).toStrictEqual({
266-
title: 'Home',
267-
})
268-
expect(storedDraft.modified.social).toStrictEqual([])
269-
})
270-
271-
it('CreateDocumentFolder uses schema-derived starter content for both created files', async () => {
272-
const folderFsPath = `guides-${Math.random().toString(36).substr(2, 9)}`
273-
const folderCollection = createCollection('docs', 'page', {
274-
$schema: 'http://json-schema.org/draft-07/schema#',
275-
$ref: '#/definitions/docs',
276-
definitions: {
277-
docs: {
278-
type: 'object',
279-
required: ['title', 'links'],
280-
properties: {
281-
title: {
282-
type: 'string',
283-
default: 'Default title',
284-
},
285-
layout: {
286-
type: 'string',
287-
default: 'docs',
288-
},
289-
links: {
290-
type: 'array',
291-
items: {
292-
type: 'string',
293-
},
294-
},
295-
},
296-
},
297-
},
298-
})
299-
300-
vi.mocked(mockHost.collection.getByFsPath).mockImplementation(() => folderCollection)
301-
302-
await context.itemActionHandler[StudioItemActionId.CreateDocumentFolder]({
303-
fsPath: folderFsPath,
304-
})
305-
306-
const navigationFsPath = joinURL(folderFsPath, '.navigation.yml')
307-
const rootFsPath = joinURL(folderFsPath, 'index.md')
308-
const storedNavigationDraft = JSON.parse(mockStorageDraft.get(normalizeKey(navigationFsPath))!)
309-
const storedRootDraft = JSON.parse(mockStorageDraft.get(normalizeKey(rootFsPath))!)
310-
311-
expect(storedNavigationDraft.modified.layout).toBe('docs')
312-
expect(storedNavigationDraft.modified.title).toBe(folderFsPath.split('/').pop())
313-
expect(storedNavigationDraft.modified.links).toStrictEqual([])
314-
expect(storedRootDraft.modified.layout).toBe('docs')
315-
expect(storedRootDraft.modified.links).toStrictEqual([])
316-
expect(JSON.stringify(storedRootDraft.modified.body)).toContain('Guides')
317-
})
318-
31972
it('Create > Rename', async () => {
32073
const consoleInfoSpy = vi.spyOn(console, 'info')
32174
/* STEP 1: CREATE */

0 commit comments

Comments
 (0)