Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 13c6f86

Browse files
committed
test: unit test
1 parent 875659c commit 13c6f86

File tree

1 file changed

+19
-51
lines changed

1 file changed

+19
-51
lines changed

__tests__/utils.spec.ts

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,26 @@
11
import { ref } from 'vue'
22
import { MODIFIED_START_TAG, MODIFIED_CLOSE_TAG, getDiffType, getSplitLines, getUnifiedLines, renderLines, renderWords, setHighlightCode } from '../src/utils'
33

4-
describe('Utils unit', () => {
5-
const added = {
6-
count: 1,
7-
added: true,
8-
removed: undefined,
9-
value: ''
10-
}
11-
12-
const removed = {
13-
count: 1,
14-
added: undefined,
15-
removed: true,
16-
value: ''
17-
}
4+
import type { Diff } from 'diff-match-patch'
185

19-
const equal = {
20-
count: 1,
21-
added: undefined,
22-
removed: undefined,
23-
value: ''
24-
}
25-
26-
const disabled = {
27-
value: ''
28-
}
6+
describe('Utils unit', () => {
7+
// diff-match-patch output
8+
const added: Diff = [1, '']
9+
const removed: Diff = [-1, '']
10+
const equal: Diff = [0, '']
11+
// custom output
12+
const disabled: Diff = [2, '']
2913

30-
const prev = 'a\nb'
31-
const curr = 'c'
14+
const prev: Diff = [-1, 'a\nb']
15+
const curr: Diff = [1, 'c']
3216

33-
const diffsMap = [
34-
[
35-
{
36-
count: 2,
37-
added: undefined,
38-
removed: true,
39-
value: prev
40-
},
41-
{
42-
count: 1,
43-
added: true,
44-
removed: undefined,
45-
value: curr
46-
}
47-
]
48-
]
17+
const diffsMap = [[prev, curr]]
4918

5019
it('getDiffType', () => {
51-
expect(getDiffType(added)).toBe('added')
52-
expect(getDiffType(removed)).toBe('removed')
53-
expect(getDiffType(equal)).toBe('equal')
54-
expect(getDiffType(disabled)).toBe('disabled')
20+
expect(getDiffType(added[0])).toBe('added')
21+
expect(getDiffType(removed[0])).toBe('removed')
22+
expect(getDiffType(equal[0])).toBe('equal')
23+
expect(getDiffType(disabled[0])).toBe('disabled')
5524
})
5625

5726
it('getSplitLines', () => {
@@ -60,7 +29,7 @@ describe('Utils unit', () => {
6029
expect(result[0][0].type).toBe('removed')
6130
expect(result[0][0].lineNum).toBe(1)
6231
expect(result[0][0].value).toBe('a')
63-
expect(result[0][0].chkWords).toBe(false)
32+
expect(result[0][0].chkWords).toBe(true)
6433
})
6534

6635
it('getUnifiedLines', () => {
@@ -78,16 +47,15 @@ describe('Utils unit', () => {
7847
})
7948

8049
it('getUnifiedLines', () => {
81-
const split = renderLines('split', prev, curr)
82-
const unified = renderLines('unified', prev, curr)
50+
const split = renderLines('split', prev[1], curr[1])
51+
const unified = renderLines('unified', prev[1], curr[1])
8352
expect(split.length).toBe(2)
8453
expect(unified.length).toBe(3)
8554
})
8655

8756
it('renderWords', () => {
8857
expect(renderWords('abc', 'abc')).toBe('abc')
89-
expect(renderWords('abc', 'acc')).toBe(`${MODIFIED_START_TAG}acc${MODIFIED_CLOSE_TAG}`)
90-
expect(renderWords('a b c', 'a c c')).toBe(`a ${MODIFIED_START_TAG}c${MODIFIED_CLOSE_TAG} c`)
58+
expect(renderWords('abc', 'acc')).toBe(`a${MODIFIED_START_TAG}c${MODIFIED_CLOSE_TAG}c`)
9159
})
9260

9361
it('setHighlightCode', () => {

0 commit comments

Comments
 (0)