Skip to content

Commit 8129b11

Browse files
committed
unit test with jest
1 parent f2d9aed commit 8129b11

20 files changed

+186
-57
lines changed

Diff for: .prettierrc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
module.exports = {
33
trailingComma: 'es5',
44
tabWidth: 4,
5-
semi: false,
5+
semi: true,
66
singleQuote: true,
7-
};
7+
}

Diff for: jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
},
66
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
77
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8+
moduleDirectories: ['node_modules', 'src'],
89
}

Diff for: package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"pretest": "npm run compile",
3030
"test": "node ./out/test/runTest.js",
3131
"test:jest": "jest",
32-
"prettier": "prettier --write src/*"
32+
"prettier": "prettier --write src/**/*.ts"
3333
},
3434
"husky": {
3535
"hooks": {

Diff for: src/lib/extension.ts

+5-46
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import * as vscode from 'vscode'
4-
import * as fs from 'fs'
5-
import translate from '@vitalets/google-translate-api'
3+
import * as vscode from 'vscode';
64

75
// this method is called when your extension is activated
86
// your extension is activated the very first time the command is executed
@@ -20,53 +18,14 @@ export function activate(context: vscode.ExtensionContext) {
2018
// Display a message box to the user
2119
vscode.window.showInformationMessage(
2220
'Hello World! Welcome to .md language converter to urdu'
23-
)
24-
// Getting information of current open file if that file is markdown get content of file
25-
await checkFileType()
21+
);
2622
}
27-
)
23+
);
2824

29-
context.subscriptions.push(disposable)
30-
}
31-
32-
// this method is called for checking current open file
33-
async function checkFileType() {
34-
await Promise.all(
35-
vscode.workspace.textDocuments.map(async file => {
36-
const { languageId, fileName } = file
37-
if (languageId === 'markdown') {
38-
await getFileContent(fileName)
39-
}
40-
})
41-
)
42-
}
43-
44-
// this method is called for getting content of current file
45-
async function getFileContent(fileName: string) {
46-
await fs.readFile(fileName, async (err, data) => {
47-
if (!err) {
48-
const fileData = await data.toString()
49-
translate(fileData, { to: 'ur' })
50-
.then(async (res: any) => {
51-
if (!res) throw Error('Unable to convert to urdu')
52-
else {
53-
const urduText = res.text
54-
await fs.writeFile(fileName, urduText, err => {
55-
if (err) throw Error('Unable to convert to urdu')
56-
vscode.window.showInformationMessage(
57-
'.md Successfully Converted to urdu'
58-
)
59-
})
60-
}
61-
})
62-
.catch((err: object) => {
63-
throw Error('Unable to convert to urdu')
64-
})
65-
} else throw Error('Unable to convert to urdu')
66-
})
25+
context.subscriptions.push(disposable);
6726
}
6827

6928
// this method is called when your extension is deactivated
7029
export function deactivate() {
71-
vscode.window.showInformationMessage('md2urdu is deactivated')
30+
vscode.window.showInformationMessage('md2urdu is deactivated');
7231
}

Diff for: src/lib/internal/checkFileType.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This class is responsible for getting current editor file type
2+
3+
// CheckFileType Class
4+
// Getting information of current open file if that file is markdown get content of file
5+
module.exports = class CheckFileType {
6+
vscode: any;
7+
constructor(vscode: any) {
8+
this.vscode = vscode;
9+
}
10+
// this method is called for checking current open file
11+
checkFileType = async () => {
12+
let response;
13+
await Promise.all(
14+
this.vscode.workspace.textDocuments.map(async (file: any) => {
15+
const { languageId, fileName } = file;
16+
if (languageId === 'markdown') {
17+
response = true;
18+
}
19+
})
20+
);
21+
return response;
22+
};
23+
};

Diff for: src/lib/internal/getFileContent.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This class is responsible for getting file content
2+
3+
module.exports = class GetFileContent {
4+
vscode: any;
5+
fs: any;
6+
constructor(vscode: any, fs: any) {
7+
this.vscode = vscode;
8+
this.fs = fs;
9+
}
10+
// this method is called for getting content of current file
11+
getFileContent = async (fileName: string) => {
12+
const fs = this.fs;
13+
const vscode = this.vscode;
14+
return new Promise(function(resolve: any, reject: any) {
15+
fs.readFile(fileName, async (err: any, data: any) => {
16+
if (!err) {
17+
const fileData = await data.toString();
18+
resolve(fileData);
19+
} else {
20+
vscode.window.showInformationMessage(
21+
'.md Not Converted to urdu'
22+
);
23+
reject(undefined);
24+
}
25+
});
26+
});
27+
};
28+
};

Diff for: src/lib/internal/translate.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This class is responsible for translations
2+
3+
module.exports = class Translate {
4+
translate: any;
5+
vscode: any;
6+
constructor(translate: any, vscode: any) {
7+
this.translate = translate;
8+
this.vscode = vscode;
9+
}
10+
// this method will translate string
11+
translation = async (fileData: string) => {
12+
return await this.translate(fileData, { to: 'ur' })
13+
.then((res: any) => {
14+
if (!res)
15+
this.vscode.window.showInformationMessage(
16+
'Unable to convert'
17+
);
18+
else {
19+
const urduText = res.text;
20+
return urduText;
21+
}
22+
})
23+
.catch((err: object) => {
24+
this.vscode.window.showInformationMessage('Unable to convert');
25+
return err;
26+
});
27+
};
28+
};

Diff for: src/lib/internal/writeFileContent.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This class is responsible for writting file content
2+
3+
module.exports = class WriteFileContent {
4+
vscode: any;
5+
fs: any;
6+
constructor(vscode: any, fs: any) {
7+
this.vscode = vscode;
8+
this.fs = fs;
9+
}
10+
// this method is called for writting content of current file
11+
writeFileContent = async (fileName: string, urduText: String) => {
12+
const fs = this.fs;
13+
const vscode = this.vscode;
14+
return new Promise(function(resolve: any, reject: any) {
15+
fs.writeFile(fileName, urduText, (err: any) => {
16+
if (err) reject(undefined);
17+
resolve(urduText);
18+
});
19+
});
20+
};
21+
};

Diff for: src/typings/google-translate-api.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
declare module "@vitalets/google-translate-api";
1+
declare module '@vitalets/google-translate-api';

Diff for: src/typings/vscode.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'vscode';

Diff for: test/integration/extension.test.tsx

-3
This file was deleted.

Diff for: test/unit/extension.test.tsx

-3
This file was deleted.

Diff for: test/unit/lib/internal/checkFileType.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This will test type of current file
2+
const checkFileTypeImport = require('./import')
3+
4+
test('checking file type in editor', async () => {
5+
const { CheckFileType, vscode } = checkFileTypeImport
6+
const { currentWorkSpace } = vscode
7+
const fileTypeResult = new CheckFileType(currentWorkSpace)
8+
const { checkFileType } = fileTypeResult
9+
const response = await checkFileType()
10+
expect(response).toBe(true)
11+
})

Diff for: test/unit/lib/internal/example.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
آپ کیسے ہو

Diff for: test/unit/lib/internal/getWriteFileContent.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This test will get file content and write file content
2+
const getWriteFileContentImport = require('./import')
3+
4+
test('getting file content', async () => {
5+
const { GetFileContent, vscode, fs, path } = getWriteFileContentImport
6+
const { windowMessage } = vscode
7+
const { join } = path
8+
const getFileContentResult = new GetFileContent(windowMessage, fs)
9+
const { getFileContent } = getFileContentResult
10+
const exampleMdPath = join(__dirname, 'example.md')
11+
const response = await getFileContent(exampleMdPath)
12+
expect(typeof response).toBe('string')
13+
})
14+
15+
test('writing in file content', async () => {
16+
const { WriteFileContent, vscode, fs, path } = getWriteFileContentImport
17+
const { windowMessage } = vscode
18+
const { join } = path
19+
const writeFileContentResult = new WriteFileContent(windowMessage, fs)
20+
const { writeFileContent } = writeFileContentResult
21+
const exampleMdPath = join(__dirname, 'example.md')
22+
const response = await writeFileContent(exampleMdPath, 'آپ کیسے ہو')
23+
expect(typeof response).toBe('string')
24+
})

Diff for: test/unit/lib/internal/import.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// All imports for test cases will be here
2+
3+
exports.fs = require('fs')
4+
exports.path = require('path')
5+
exports.GetFileContent = require('../../../../src/lib/internal/getFileContent')
6+
exports.WriteFileContent = require('../../../../src/lib/internal/writeFileContent')
7+
exports.vscode = require('./vscode')
8+
exports.CheckFileType = require('../../../../src/lib/internal/checkFileType')
9+
exports.translate = require('@vitalets/google-translate-api')
10+
exports.Translate = require('../../../../src/lib/internal/translate')

Diff for: test/unit/lib/internal/translate.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This test will translate string
2+
const translateImport = require('./import')
3+
4+
test('translate', async () => {
5+
const { Translate, translate: translateAPI, vscode } = translateImport
6+
const { windowMessage } = vscode
7+
const translateResult = new Translate(translateAPI, windowMessage)
8+
const { translation } = translateResult
9+
const response = await translation('hello world')
10+
expect(typeof response).toBe('string')
11+
})

Diff for: test/unit/lib/internal/vscode.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
windowMessage: {
3+
window: {
4+
showInformationMessage: (response: String) => {
5+
console.log(response)
6+
},
7+
},
8+
},
9+
currentWorkSpace: {
10+
workspace: { textDocuments: [{ languageId: 'markdown' }] },
11+
},
12+
}

Diff for: tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/* Additional Checks */
1414
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
1515
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
16-
"noUnusedParameters": true, /* Report errors on unused parameters. */
16+
"noUnusedParameters": true /* Report errors on unused parameters. */
1717
},
1818
"exclude": [
1919
"node_modules",

0 commit comments

Comments
 (0)