Skip to content

Commit 7c6854d

Browse files
committed
chore: add test cases for operationUtils
1 parent 673e2f6 commit 7c6854d

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/__tests__/operationUtils.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as operationUtils from '../operationUtils'
2+
3+
describe('run-operationUtils', () => {
4+
const node: any = { range: [54, 106] }
5+
const start: number = 100
6+
const end: number = 101
7+
const rangeAt: number = 1000
8+
const modifyText: string = 'Change Code'
9+
const blankTest: string = ''
10+
11+
it('insertTextAfter code to equal object', () => {
12+
expect(operationUtils.insertTextAfter(node, modifyText)).toStrictEqual({
13+
range: [node.range[1], node.range[1]],
14+
text: modifyText,
15+
})
16+
})
17+
18+
it('insertTextAfterRange code to equal object', () => {
19+
expect(
20+
operationUtils.insertTextAfterRange([start, end], modifyText)
21+
).toStrictEqual({ range: [end, end], text: modifyText })
22+
})
23+
24+
it('insertTextAt code to equal object', () => {
25+
expect(operationUtils.insertTextAt(rangeAt, modifyText)).toStrictEqual({
26+
range: [rangeAt, rangeAt],
27+
text: modifyText,
28+
})
29+
})
30+
31+
it('insertTextBefore code to equal object', () => {
32+
expect(operationUtils.insertTextBefore(node, modifyText)).toStrictEqual({
33+
range: [node.range[0], node.range[0]],
34+
text: modifyText,
35+
})
36+
})
37+
38+
it('insertTextBeforeRange code to equal object', () => {
39+
expect(
40+
operationUtils.insertTextBeforeRange([start, end], modifyText)
41+
).toStrictEqual({ range: [start, start], text: modifyText })
42+
})
43+
44+
it('remove code to equal object', () => {
45+
expect(operationUtils.remove(node)).toStrictEqual({
46+
range: node.range,
47+
text: blankTest,
48+
})
49+
})
50+
51+
it('removeRange code to equal object', () => {
52+
expect(operationUtils.removeRange([start, end])).toStrictEqual({
53+
range: [start, end],
54+
text: blankTest,
55+
})
56+
})
57+
58+
it('replaceText code to equal object', () => {
59+
expect(operationUtils.replaceText(node, modifyText)).toStrictEqual({
60+
range: node.range,
61+
text: modifyText,
62+
})
63+
})
64+
65+
it('replaceTextRange code to equal object', () => {
66+
expect(
67+
operationUtils.replaceTextRange(node.range, modifyText)
68+
).toStrictEqual({ range: node.range, text: modifyText })
69+
})
70+
})

src/testUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
jest.autoMockOff()
2+
3+
import * as fs from 'fs'
4+
import * as path from 'path'
5+
import runTransformation from './runTransformation'
6+
7+
export const runTest = (
8+
description: string,
9+
transformationName: string,
10+
fixtureName: string,
11+
extension: string = 'vue',
12+
transformationType: string = 'vue'
13+
) => {
14+
test(description, () => {
15+
const fixtureDir = path.resolve(
16+
__dirname,
17+
transformationType == 'vue'
18+
? '../vue-transformations'
19+
: '../wrapAstTransformation',
20+
'./__testfixtures__',
21+
transformationName
22+
)
23+
24+
const inputPath = path.resolve(
25+
fixtureDir,
26+
`${fixtureName}.input.${extension}`
27+
)
28+
const outputPath = path.resolve(
29+
fixtureDir,
30+
`${fixtureName}.output.${extension}`
31+
)
32+
33+
const fileInfo = {
34+
path: inputPath,
35+
source: fs.readFileSync(inputPath).toString(),
36+
}
37+
const transformation = require((transformationType == 'vue'
38+
? '../vue-transformations'
39+
: '../wrapAstTransformation') + `/${transformationName}`)
40+
expect(runTransformation(fileInfo, transformation)).toEqual(
41+
fs.readFileSync(outputPath).toString()
42+
)
43+
})
44+
}

0 commit comments

Comments
 (0)