Skip to content

Commit 4209155

Browse files
committed
add login check test
1 parent f65057a commit 4209155

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
bin/
33
src/**/*.js
44
src/**/*.d.ts
5+
__tests__/auth.json

__tests__/auth.example.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"username": "UserName",
3+
"password": "YourPassword(keep gitignored!)"
4+
}

__tests__/login.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {username, password} from "./auth.json";
2+
import inquirer from "inquirer";
3+
import {AtCoder} from "../src/atcoder";
4+
import {Cookie} from "../src/cookie";
5+
6+
jest.mock("inquirer");
7+
8+
/*
9+
* Cookieファイルの実装を差し替えておく
10+
* コンフィグファイルとの読み書きを行わず、必ず空のセッションが返るようにする
11+
*/
12+
Cookie.prototype.loadConfigFile = jest.fn(async function () {
13+
// @ts-ignore
14+
this.cookies = []
15+
});
16+
Cookie.prototype.saveConfigFile = jest.fn(async function () {
17+
});
18+
19+
/*
20+
このテストが失敗する場合、まず以下の点を確認してください
21+
- __tests__/auth.json が存在し、正しいユーザー名とパスワードが記述されていること
22+
すべてのテストを開始する前に、__tests__/auth.example.jsonをコピーして__tests__/auth.json を作成し、
23+
有効なAtCoderアカウントのユーザー名とパスワードを記述してください
24+
__tests__/auth.jsonはgit管理に含めないように注意してください
25+
*/
26+
test("AtCoder Login", async () => {
27+
const atcoder = new AtCoder();
28+
expect(await atcoder.checkSession()).toBe(false);
29+
30+
// ユーザー名とパスワードを標準入力で受け付けるかわりにファイルから流し込むようモックする
31+
// @ts-ignore
32+
inquirer.prompt.mockResolvedValueOnce({username, password});
33+
34+
expect(await atcoder.login()).toBe(true);
35+
expect(await atcoder.checkSession(true)).toBe(true);
36+
});

__tests__/test.js.ts

-3
This file was deleted.

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"resolveJsonModule": true
1313
},
1414
"exclude": [
15-
"node_modules"
15+
"node_modules",
16+
"__tests__"
1617
]
1718
}

0 commit comments

Comments
 (0)