forked from revisit-studies/study
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryExampleStudyGenerator.spec.ts
More file actions
90 lines (76 loc) · 3.17 KB
/
Copy pathLibraryExampleStudyGenerator.spec.ts
File metadata and controls
90 lines (76 loc) · 3.17 KB
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
import fs from 'fs';
import os from 'os';
import path from 'path';
import { createRequire } from 'module';
import {
afterEach,
describe,
expect,
it,
vi,
} from 'vitest';
const require = createRequire(import.meta.url);
const {
createExampleConfig,
generateLibraryExamples,
getLibraries,
} = require('./libraryExampleStudyGenerator.cjs');
describe('libraryExampleStudyGenerator', () => {
const tempDirs: string[] = [];
afterEach(() => {
tempDirs.forEach((dir) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
tempDirs.length = 0;
});
it('createExampleConfig builds config with expected defaults', () => {
const config = createExampleConfig('my-lib');
expect(config.studyMetadata.title).toBe('my-lib Example Study');
expect(config.uiConfig.withSidebar).toBe(false);
expect(config.importedLibraries).toEqual(['my-lib']);
expect(config.components.introduction.path).toBe('library-my-lib/assets/my-lib.md');
expect(config.sequence.components).toEqual(['introduction']);
});
it('getLibraries filters hidden entries and .DS_Store entries', () => {
const base = fs.mkdtempSync(path.join(os.tmpdir(), 'lib-example-list-'));
tempDirs.push(base);
const libsPath = path.join(base, 'public', 'libraries');
fs.mkdirSync(libsPath, { recursive: true });
fs.mkdirSync(path.join(libsPath, 'alpha'));
fs.mkdirSync(path.join(libsPath, '.hidden'));
fs.mkdirSync(path.join(libsPath, 'test'));
fs.writeFileSync(path.join(libsPath, '.DS_Store'), '');
expect(getLibraries(libsPath)).toEqual(['alpha']);
});
it('generateLibraryExamples creates missing example study and invokes doc generation command', () => {
const base = fs.mkdtempSync(path.join(os.tmpdir(), 'lib-example-run-'));
tempDirs.push(base);
const libraryName = 'alpha';
const librariesPath = path.join(base, 'public', 'libraries', libraryName);
fs.mkdirSync(librariesPath, { recursive: true });
const generateDocsFn = vi.fn();
generateLibraryExamples(base, generateDocsFn);
const examplePath = path.join(base, 'public', `library-${libraryName}`);
const configPath = path.join(examplePath, 'config.json');
const assetsPath = path.join(examplePath, 'assets');
expect(fs.existsSync(examplePath)).toBe(true);
expect(fs.existsSync(assetsPath)).toBe(true);
expect(fs.existsSync(configPath)).toBe(true);
expect(generateDocsFn).toHaveBeenCalledTimes(1);
expect(generateDocsFn).toHaveBeenCalledWith(base);
});
it('throws when doc generation fails', () => {
const base = fs.mkdtempSync(path.join(os.tmpdir(), 'lib-example-error-'));
tempDirs.push(base);
fs.mkdirSync(path.join(base, 'public', 'libraries', 'alpha'), { recursive: true });
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
const generateDocsFn = vi.fn(() => {
throw new Error('test Error');
});
expect(() => generateLibraryExamples(base, generateDocsFn)).toThrow('test Error');
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Error running libraryDocGenerator.cjs: Error: test Error'));
errorSpy.mockRestore();
});
});