Skip to content

Commit 5736b67

Browse files
committed
テスト追加
1 parent 3c0c4a7 commit 5736b67

File tree

8 files changed

+304
-2159
lines changed

8 files changed

+304
-2159
lines changed

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
"vscode": "^1.27.0"
1616
},
1717
"dependencies": {
18-
"@types/jest": "^23.3.2",
18+
"@types/chai": "^4.1.5",
19+
"@types/mocha": "^5.2.5",
1920
"@types/node": "^10.10.1",
21+
"@types/sinon": "^5.0.2",
22+
"chai": "^4.1.2",
2023
"core-js": "^2.5.7",
2124
"fuse.js": "^3.2.1",
22-
"jest": "^23.6.0",
25+
"mocha": "^5.2.0",
2326
"qiita-js-2": "^1.2.5",
24-
"ts-jest": "^23.1.4",
27+
"sinon": "^6.3.4",
2528
"tslint": "^5.8.0",
2629
"typescript": "^3.0.3",
2730
"vscode": "^1.1.6",

src/quickpicks/tagQuickPickCreator.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SearchTagResult } from 'qiita-js-2';
12
import { QuickPickItem, window } from 'vscode';
23
import { client } from '../client';
34

@@ -12,26 +13,38 @@ export const makeQuickPickItemFromTag = (id: string, followersCount: number) =>
1213
description: `${followersCount}件の投稿`,
1314
});
1415

16+
/**
17+
* ユーザーからの入力が検索結果に無いときにその入力を結果の先頭に挿入
18+
* @param value ユーザーの入力
19+
* @param suggestions 検索結果
20+
* @return フォーマットされた検索結果
21+
*/
22+
export const insertInputRaw = (value: string, suggestions: SearchTagResult[]) => {
23+
if (!value) {
24+
return suggestions;
25+
}
26+
27+
if (suggestions
28+
.map((tag) => tag.name)
29+
.map((name) => RegExp(name, 'i').test(value))
30+
.includes(true)) {
31+
return suggestions;
32+
}
33+
34+
suggestions.unshift({ name: value, url_name: value, follower_count: 0, item_count: 0 });
35+
36+
return suggestions;
37+
};
38+
1539
/**
1640
* キーワードからタグを検索して QuickPickItem の形で返します
1741
* @param value キーワード
1842
* @return QuickPickItem
1943
*/
20-
async function suggestTags (value: string) {
44+
export async function suggestTags (value: string): Promise<QuickPickItem[]> {
2145
const results = (await client.searchTags(value)).slice(0, 9);
22-
23-
// キーワードに相当するタグがまだ作成されていない場合、候補の一件目にそのタグを挿入
24-
// tag.nameをケースインセンシティブで比較
25-
if (value && !results.map((tag) => tag.name).map((name) => RegExp(name, 'i').test(value)).includes(true)) {
26-
results.unshift({
27-
name: value,
28-
url_name: value,
29-
follower_count: 0,
30-
item_count: 0,
31-
});
32-
}
33-
34-
return results.map((tag) => makeQuickPickItemFromTag(tag.name, tag.follower_count));
46+
const formattedResults = insertInputRaw(value, results);
47+
return formattedResults.map((tag) => makeQuickPickItemFromTag(tag.name, tag.follower_count));
3548
}
3649

3750
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect } from 'chai';
2+
import { getFilenameFromPath } from '../../commands/compose';
3+
4+
describe('compose', () => {
5+
it('パスから拡張子を除いたファイル名を取得', () => {
6+
const path = 'file:///test.md';
7+
const result = getFilenameFromPath(path);
8+
expect(result).to.be.equal('test');
9+
});
10+
});

src/test/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING
3+
//
4+
// This file is providing the test runner to use when running extension tests.
5+
// By default the test runner in use is Mocha based.
6+
//
7+
// You can provide your own test runner if you want to override it by exporting
8+
// a function run(testRoot: string, clb: (error:Error) => void) that the extension
9+
// host can call to run the tests. The test runner is expected to use console.log
10+
// to report the results back to the caller. When the tests are finished, return
11+
// a possible error to the callback or null if none.
12+
13+
import * as testRunner from 'vscode/lib/testrunner';
14+
15+
// You can directly control Mocha options by uncommenting the following lines
16+
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
17+
testRunner.configure({
18+
ui: 'bdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
19+
useColors: true, // colored output from test results
20+
});
21+
22+
module.exports = testRunner;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect } from 'chai';
2+
import { insertInputRaw, makeQuickPickItemFromTag } from '../../quickpicks/tagQuickPickCreator';
3+
4+
describe('tagQuickPickCreator', () => {
5+
it('渡されたIDと文字列からQuickPickItemを返す', () => {
6+
const quickPickItem = makeQuickPickItemFromTag('apple', 12345);
7+
8+
expect(quickPickItem).to.deep.equal({
9+
label: 'apple',
10+
description: '12345件の投稿',
11+
});
12+
});
13+
14+
it('渡されたキーワードが検索結果に無いときに検索結果の先頭に挿入', async () => {
15+
const result = insertInputRaw('apple', [
16+
{ name: 'google', url_name: 'google', follower_count: 1, item_count: 1 },
17+
{ name: 'facebook', url_name: 'facebook', follower_count: 2, item_count: 2 },
18+
{ name: 'microsoft', url_name: 'microsoft', follower_count: 3, item_count: 2 },
19+
]);
20+
21+
expect(result).to.deep.include({ name: 'apple', url_name: 'apple', follower_count: 0, item_count: 0});
22+
});
23+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from 'chai';
2+
import { titleInputBoxCreator } from '../../quickpicks/titleInputBoxCreator';
3+
4+
describe('titleInputBoxCreator', () => {
5+
it('デフォルトの値を指定してInputBoxを作成', () => {
6+
const inputBox = titleInputBoxCreator('タイトルです');
7+
expect(inputBox.value).to.be.equal('タイトルです');
8+
});
9+
10+
// 値を直接変更したらonDidChangeValueが呼ばれないみたいです >_<
11+
// it('255文字以上の際に警告を表示', () => {
12+
// const inputBox = titleInputBoxCreator('0123456789'.repeat(26));
13+
// expect(inputBox.validationMessage).to.be.a('string');
14+
// });
15+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from 'chai';
2+
import { fake } from 'sinon';
3+
import { QuickPick } from 'vscode';
4+
import { createMultiStepInput } from '../../utils/createMultiStepInput';
5+
6+
describe('createMultiStepInput', () => {
7+
it('複数のQuickPick/InputBoxをラップして最初の1つを呼び出す関数を返す', () => {
8+
const onDidAccept = fake();
9+
const show = fake();
10+
const quickPick = { onDidAccept, show } as any as QuickPick<any>;
11+
12+
createMultiStepInput([quickPick])();
13+
14+
expect(show.calledOnce).to.be.ok;
15+
});
16+
});

0 commit comments

Comments
 (0)