UNPKG

@mintlify/previewing

Version:

Preview Mintlify docs locally

71 lines (70 loc) 3.18 kB
import { access, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { getDocsState } from '../local-preview/listener/getDocsState.js'; describe('getDocsState', () => { let contentDirectory; let clientDirectory; beforeEach(async () => { contentDirectory = await mkdtemp(path.join(os.tmpdir(), 'preview-content-')); clientDirectory = await mkdtemp(path.join(os.tmpdir(), 'preview-client-')); }); afterEach(async () => { await Promise.all([ rm(contentDirectory, { recursive: true, force: true }), rm(clientDirectory, { recursive: true, force: true }), ]); }); it('rejects case-insensitive GraphQL and AsyncAPI collisions before overwriting either page', async () => { await writeFile(path.join(contentDirectory, 'schema.graphql'), 'type Query { ping: String }'); await writeFile(path.join(contentDirectory, 'asyncapi.json'), JSON.stringify({ asyncapi: '3.0.0', info: { title: 'Events', version: '1.0.0' }, channels: { ping: { address: 'ping', tags: [{ name: 'Queries' }], messages: { ping: { payload: { type: 'string' } } }, }, }, operations: { ping: { action: 'send', channel: { $ref: '#/channels/ping' }, messages: [{ $ref: '#/channels/ping/messages/ping' }], }, }, })); await writeFile(path.join(contentDirectory, 'docs.json'), JSON.stringify({ theme: 'mint', name: 'Preview collision', colors: { primary: '#000000' }, navigation: { tabs: [ { tab: 'GraphQL', graphql: { source: 'schema.graphql', directory: 'API' }, }, { tab: 'AsyncAPI', asyncapi: { source: 'asyncapi.json', directory: 'api' }, }, ], }, })); const graphqlPath = path.join(clientDirectory, 'src/_props/API/queries/ping.mdx'); const asyncApiPath = path.join(clientDirectory, 'src/_props/api/queries/ping.mdx'); await Promise.all([ mkdir(path.dirname(graphqlPath), { recursive: true }), mkdir(path.dirname(asyncApiPath), { recursive: true }), ]); await Promise.all([ writeFile(graphqlPath, 'existing page'), writeFile(asyncApiPath, 'existing page'), ]); await expect(getDocsState(undefined, { contentDirectory, clientDirectory })).rejects.toThrow('Generated GraphQL route'); await expect(readFile(graphqlPath, 'utf8')).resolves.toBe('existing page'); await expect(readFile(asyncApiPath, 'utf8')).resolves.toBe('existing page'); await expect(access(path.join(clientDirectory, 'src/_props/graphql-data.json'))).rejects.toMatchObject({ code: 'ENOENT' }); }); });