Skip to content

Commit aaf846b

Browse files
authored
Merge pull request #13 from Paker30/10-default-imports
10 default imports
2 parents 26a118f + 6adb815 commit aaf846b

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

src/actions/initTest.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const typeRegex = /\s*?type\s*(\w*)/gm;
88

99
const identity = <T>(arg: T): T => arg;
1010

11-
export const createTest = ({
11+
export const writeTest = ({
1212
editor,
1313
pathTo,
1414
mockedImports
@@ -31,6 +31,20 @@ export const createTest = ({
3131

3232
};
3333

34+
export const generateCode = (pathTo: string) => (content: string) => {
35+
return content.match(importedRegex)
36+
?.map((line) => new RegExp(splitImportRegex).exec(line))
37+
.map((importedContent) => {
38+
const path = importedContent?.pop();
39+
const [_, defaultImport, imports = ''] = importedContent!;
40+
const mockedFunctions = [defaultImport ? 'default' : defaultImport, ...imports.split(',')]
41+
.filter(identity)
42+
.map((imp) => new RegExp(typeRegex).test(imp) ? new RegExp(typeRegex).exec(imp)![1] : imp)
43+
.map((fn) => `${fn}: mock.fn()`);
44+
return `'${relative(pathTo, path!)}': { ${mockedFunctions?.join(',')}}`;
45+
});
46+
};
47+
3448
export const initTest = async () => {
3549
const editor = vscode.window.activeTextEditor;
3650
if (!editor) {
@@ -48,18 +62,6 @@ export const initTest = async () => {
4862
const pathTo = relative(dirname(vscode.window.activeTextEditor!.document.uri.fsPath), targetFile[0].fsPath);
4963
const mockedImports = await vscode.workspace.fs.readFile(targetFile[0])
5064
.then((readData) => Buffer.from(readData).toString('utf8'))
51-
.then((content) => {
52-
return content.match(importedRegex)
53-
?.map((line) => new RegExp(splitImportRegex).exec(line))
54-
.map((importedContent) => {
55-
const path = importedContent?.pop();
56-
const [defaultImport, imports] = importedContent?.slice(1)!;
57-
const mockedFunctions = [defaultImport ? 'default' : defaultImport, ...imports.split(',')]
58-
.filter(identity)
59-
.map((imp) => new RegExp(typeRegex).test(imp) ? new RegExp(typeRegex).exec(imp)![1] : imp)
60-
.map((fn) => `${fn}: mock.fn()`);
61-
return `'${relative(pathTo, path!)}': { ${mockedFunctions?.join(',')}}`;
62-
});
63-
});
64-
editor.edit(createTest({ editor, pathTo: formatPath(pathTo), mockedImports}));
65+
.then(generateCode(pathTo));
66+
editor.edit(writeTest({ editor, pathTo: formatPath(pathTo), mockedImports }));
6567
};

src/test/extension.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sinon from 'sinon';
44
// You can import and use all API from the 'vscode' module
55
// as well as import your extension to test it
66
import * as vscode from 'vscode';
7-
import { importedRegex, splitImportRegex, createTest } from '../actions/initTest';
7+
import { importedRegex, splitImportRegex, generateCode, writeTest } from '../actions/initTest';
88

99
suite('Extension Test Suite', () => {
1010
vscode.window.showInformationMessage('Start all tests.');
@@ -37,7 +37,23 @@ suite('Extension Test Suite', () => {
3737
});
3838
});
3939

40-
suite('createTest', () => {
40+
suite('generateCode', () => {
41+
const fakePath = 'utils';
42+
test('import default', () => {
43+
const [createdCode] = generateCode(fakePath)('import cheers from \'./utils.ts\';')!;
44+
assert.equal(createdCode.replace('\\', '/'), "'../utils.ts': { default: mock.fn()}");
45+
});
46+
test('import with destructuring', () => {
47+
const [createdCode] = generateCode(fakePath)("import { goodbye, greeter, Person } from './utils.ts';")!;
48+
assert.equal(createdCode.replace('\\', '/'), "'../utils.ts': { goodbye: mock.fn(), greeter: mock.fn(), Person : mock.fn()}");
49+
});
50+
test('import with default and destructuring', () => {
51+
const [createdCode] = generateCode(fakePath)("import cheers, { hello, goodbye, greeter, Person } from './utils.ts';")!;
52+
assert.equal(createdCode.replace('\\', '/'), "'../utils.ts': { default: mock.fn(), hello: mock.fn(), goodbye: mock.fn(), greeter: mock.fn(), Person : mock.fn()}");
53+
});
54+
});
55+
56+
suite('writeTest', () => {
4157
const fakeInsert = sinon.stub();
4258
const fakeEditBuilder = {
4359
insert: fakeInsert
@@ -53,7 +69,7 @@ suite('Extension Test Suite', () => {
5369
} as unknown as vscode.TextEditor;
5470

5571
test('check inserted code', () => {
56-
createTest({ editor: fakeEditor as any, pathTo: '', mockedImports: [] })(fakeEditBuilder);
72+
writeTest({ editor: fakeEditor as any, pathTo: '', mockedImports: [] })(fakeEditBuilder);
5773
sinon.assert.callCount(fakeInsert, 4);
5874
});
5975

0 commit comments

Comments
 (0)