-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcreate.spec.js
80 lines (69 loc) · 2.72 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
71
72
73
74
75
76
77
78
79
import {afterEach, describe, expect, it, vi} from 'vitest';
const { getQuestionToday} = require('../common/utils/getQuestionToday');
const {sourceFilePath} = require('../common/utils/createQuestion');
const {generateTemplateContent} = require('../common/utils/fulfillQuestion');
const {getQuestionById} = require('../common/utils/getQuestionById');
const {removeDomTags} = require('../common/utils/removeDomTags');
const fs = require('fs');
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 handleText = (input) => input.replace(/\n+/g, '\n').replaceAll('\n', '\n * ');
const mockKeys = [ 'enName', 'title', 'detail', 'id', 'jsCode', 'date' ];
const oneDay = 24*60*60*1000;
function isValidQuestion(res) {
const content = generateTemplateContent(fileContent,res);
// 是否含有函数
expect(isContainJsCode(content)).toBeTruthy();
// 是否含有测试用例
expect(isContainTestCase(content)).toBeTruthy();
// 是否含有描述
expect(content.includes('示例')).toBeTruthy();
expect(content.includes('提示')).toBeTruthy();
}
const fileContent = fs.readFileSync(sourceFilePath, 'utf-8');
describe('leet-create', ()=> {
// 清楚mock历史记录
afterEach(()=> {
vi.clearAllMocks();
})
describe('with -t option', async() => {
const res = await getQuestionToday();
it('是否正确获取了今天的题目', () => {
expect(Object.keys(res)).toEqual(mockKeys);
// 比较日期是否相等
expect(Math.trunc(new Date(res.date).valueOf()/oneDay)).toEqual(Math.trunc(new Date().valueOf()/oneDay));
});
it('是否正确的填充了今天的题目', async ()=> {
isValidQuestion(res)
})
});
describe('with -r 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);
})
})
})