UNPKG

@mintlify/previewing

Version:

Preview Mintlify docs locally

61 lines (60 loc) 2.76 kB
import fse from 'fs-extra'; import os from 'os'; import path from 'path'; import { generatePages, writeStaticUserInfo } from '../local-preview/export.js'; import { getGroupFilteredRoutes } from '../local-preview/getGroupFilteredRoutes.js'; describe('getGroupFilteredRoutes', () => { it('prunes routes outside the selected groups', () => { const routes = getGroupFilteredRoutes({ groups: [ { group: 'Guides', pages: [ { href: '/index', title: 'Introduction' }, { href: '/quickstart', title: 'Quickstart', groups: ['admin'] }, { href: '/development', title: 'Development', groups: ['developer'] }, { href: '/authenticated', title: 'Authenticated', groups: ['*'] }, ], }, ], }, ['admin']); expect(routes).toEqual(['/', '/index', '/quickstart', '/authenticated']); }); }); describe('generatePages', () => { let outputDir; beforeEach(async () => { outputDir = await fse.mkdtemp(path.join(os.tmpdir(), 'mint-export-test-')); }); afterEach(async () => { vi.unstubAllGlobals(); await fse.remove(outputDir); }); it('uses the first accessible route for the root page', async () => { const fetchMock = vi.fn().mockResolvedValue(new Response('<html>Introduction</html>')); vi.stubGlobal('fetch', fetchMock); const failedPageCount = await generatePages('http://localhost:3000', ['/'], outputDir, '/introduction'); expect(failedPageCount).toBe(0); expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/introduction'); await expect(fse.readFile(path.join(outputDir, 'index.html'), 'utf8')).resolves.toBe('<html>Introduction</html>'); }); }); describe('writeStaticUserInfo', () => { let outputDir; beforeEach(async () => { outputDir = await fse.mkdtemp(path.join(os.tmpdir(), 'mint-export-test-')); }); afterEach(async () => { await fse.remove(outputDir); }); it('writes mock groups to the static user endpoint', async () => { await writeStaticUserInfo(outputDir, ['admin', 'user']); const userInfo = await fse.readJson(path.join(outputDir, '_mintlify', 'api', 'user', 'index.html')); expect(userInfo).toEqual({ user: { groups: ['admin', 'user'] } }); }); it('does not write the endpoint without groups', async () => { await writeStaticUserInfo(outputDir, undefined); const endpointExists = await fse.pathExists(path.join(outputDir, '_mintlify', 'api', 'user', 'index.html')); expect(endpointExists).toBe(false); }); });