-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcreate.spec.js
70 lines (63 loc) · 2.55 KB
/
create.spec.js
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
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getQuestionToday } from '#common/utils/question-getter/getQuestionToday.js';
import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js';
import { generateTemplateContent } from '#common/utils/question-handler/fulfillQuestion.js';
vi.mock('fs/promises', () => {
return {
writeFile: vi.fn()
};
});
const funRegex = /var\s+(\w+)\s*=\s*function\s*\(([^)]*)\)\s*{\s*([^}]*)\s*}/;
const isContainJsCode = (input) => funRegex.test(input);
const isContainTestCase = (input) => input.includes('showLogs(');
const mockKeys = ['id', 'slug', 'title', 'detail', 'lang', 'code', 'jsonExampleTestcases', 'exampleTestcases', 'date'];
function isValidQuestion(res) {
const content = generateTemplateContent(res);
// 是否含有函数
expect(isContainJsCode(content)).toBeTruthy();
// 是否含有测试用例
expect(isContainTestCase(content)).toBeTruthy();
// 是否含有描述
expect(content.includes('示例')).toBeTruthy();
expect(content.includes('提示')).toBeTruthy();
}
describe('lc', () => {
// 清楚mock历史记录
afterEach(() => {
vi.clearAllMocks();
});
describe('with -t option', async () => {
const res = await getQuestionToday();
it('是否正确获取了今天的题目', () => {
expect(Object.keys(res)).toEqual(mockKeys);
});
it('是否正确的填充了今天的题目', async () => {
isValidQuestion(res);
});
});
describe('with -i option', async () => {
const id_25 = '25';
const res_25 = await getQuestionById(id_25);
const id_LCS_03 = 'LCS 03';
const res_LCS_03 = await getQuestionById(id_LCS_03);
it('是否正确的获取了指定id的题目 25', async () => {
expect(res_25.id).toEqual(id_25);
});
it('是否正确填充了指定id的题目 25', async () => {
isValidQuestion(res_25);
});
it('是否正确的获取了指定id的题目 LCS 03', async () => {
expect(res_LCS_03.id).toEqual(id_LCS_03);
});
it('是否正确填充了指定id的题目 9', async () => {
isValidQuestion(res_LCS_03);
});
it('是否正确的获取了指定内容的题目 主题空间', async () => {
const content = '主题空间';
const res = await getQuestionById(content);
expect(res?.id).toEqual(null);
});
});
// describe('with -r option', async () => {
// })
});