Skip to content

Commit a40cf4c

Browse files
committed
add mockLogin function
1 parent 165e5a1 commit a40cf4c

File tree

3 files changed

+76
-54
lines changed

3 files changed

+76
-54
lines changed

__tests__/__snapshots__/atcoder.ts.snap

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`AtCoder contest abc101 1`] = `
3+
exports[`AtCoder get contest and task information contest abc101 1`] = `
44
Object {
55
"id": "abc101",
66
"title": "AtCoder Beginner Contest 101",
77
"url": "https://atcoder.jp/contests/abc101",
88
}
99
`;
1010

11-
exports[`AtCoder contest arc101 1`] = `
11+
exports[`AtCoder get contest and task information contest arc101 1`] = `
1212
Object {
1313
"id": "arc101",
1414
"title": "AtCoder Regular Contest 101",
1515
"url": "https://atcoder.jp/contests/arc101",
1616
}
1717
`;
1818

19-
exports[`AtCoder task abc101 abc101_a 1`] = `
19+
exports[`AtCoder get contest and task information task abc101 abc101_a 1`] = `
2020
Object {
2121
"id": "abc101_a",
2222
"label": "A",
@@ -25,7 +25,7 @@ Object {
2525
}
2626
`;
2727

28-
exports[`AtCoder task abc101 abc101_b 1`] = `
28+
exports[`AtCoder get contest and task information task abc101 abc101_b 1`] = `
2929
Object {
3030
"id": "abc101_b",
3131
"label": "B",
@@ -34,7 +34,7 @@ Object {
3434
}
3535
`;
3636

37-
exports[`AtCoder task arc101 arc101_a 1`] = `
37+
exports[`AtCoder get contest and task information task arc101 arc101_a 1`] = `
3838
Object {
3939
"id": "arc101_a",
4040
"label": "C",
@@ -43,7 +43,7 @@ Object {
4343
}
4444
`;
4545

46-
exports[`AtCoder tasks abc101 1`] = `
46+
exports[`AtCoder get contest and task information tasks abc101 1`] = `
4747
Array [
4848
Object {
4949
"id": "abc101_a",
@@ -72,7 +72,7 @@ Array [
7272
]
7373
`;
7474

75-
exports[`AtCoder tasks arc101 1`] = `
75+
exports[`AtCoder get contest and task information tasks arc101 1`] = `
7676
Array [
7777
Object {
7878
"id": "arc101_a",

__tests__/atcoder.ts

+39-36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ Cookie.prototype.loadConfigFile = jest.fn(async function () {
1717
Cookie.prototype.saveConfigFile = jest.fn(async function () {
1818
});
1919

20+
// 標準入力を求めることなくログインを行う
21+
const mockLogin = async (atcoder: AtCoder) => {
22+
// ユーザー名とパスワードを標準入力で受け付けるかわりに、JSONファイルから取得した情報を流し込む
23+
// @ts-ignore
24+
inquirer.prompt.mockResolvedValueOnce({username, password});
25+
return await atcoder.login();
26+
};
27+
2028
/*
2129
このテストが失敗する場合、まず以下の点を確認してください
2230
- __tests__/auth.json が存在し、正しいユーザー名とパスワードが記述されていること
@@ -28,49 +36,44 @@ test("AtCoder Login", async () => {
2836
const atcoder = new AtCoder();
2937
expect(await atcoder.checkSession()).toBe(false);
3038

31-
// ユーザー名とパスワードを標準入力で受け付けるかわりにファイルから流し込むようモックする
32-
// @ts-ignore
33-
inquirer.prompt.mockResolvedValueOnce({username, password});
34-
35-
expect(await atcoder.login()).toBe(true);
39+
expect(await mockLogin(atcoder)).toBe(true);
3640
expect(await atcoder.checkSession(true)).toBe(true);
3741
});
3842

3943
describe("AtCoder", async () => {
40-
let atcoder: AtCoder;
41-
beforeEach(async () => {
42-
atcoder = new AtCoder();
43-
// @ts-ignore
44-
inquirer.prompt.mockResolvedValueOnce({username, password});
45-
await atcoder.login();
46-
});
47-
const contests = ["abc101", "arc101"];
48-
describe("contest", async () => {
49-
test.each(contests)("%s", async (contest_id) => {
50-
expect(await atcoder.contest(contest_id)).toMatchSnapshot();
51-
});
52-
test("invalid contest id", async()=>{
53-
await expect(atcoder.contest("abc0xx")).rejects.toThrow();
54-
});
55-
});
56-
describe("tasks", async () => {
57-
test.each(contests)("%s", async (contest_id) => {
58-
expect(await atcoder.tasks(contest_id)).toMatchSnapshot();
59-
});
60-
test("invalid contest id", async()=>{
61-
await expect(atcoder.tasks("abc0xx")).rejects.toThrow();
62-
});
44+
const atcoder = new AtCoder();
45+
beforeAll(async () => {
46+
await mockLogin(atcoder);
6347
});
64-
const tasks = [["abc101", "abc101_a"], ["abc101", "abc101_b"], ["arc101", "arc101_a"]];
65-
describe("task", async () => {
66-
test.each(tasks)("%s %s", async (contest_id, task_id) => {
67-
expect(await atcoder.task(contest_id, task_id)).toMatchSnapshot();
48+
describe("get contest and task information", async()=> {
49+
const contests = ["abc101", "arc101"];
50+
describe("contest", async () => {
51+
test.each(contests)("%s", async (contest_id) => {
52+
expect(await atcoder.contest(contest_id)).toMatchSnapshot();
53+
});
54+
test("invalid contest id", async () => {
55+
await expect(atcoder.contest("abc0xx")).rejects.toThrow();
56+
});
6857
});
69-
test("invalid contest id", async()=>{
70-
await expect(atcoder.task("abc0xx", "abc0xx_z")).rejects.toThrow();
58+
describe("tasks", async () => {
59+
test.each(contests)("%s", async (contest_id) => {
60+
expect(await atcoder.tasks(contest_id)).toMatchSnapshot();
61+
});
62+
test("invalid contest id", async () => {
63+
await expect(atcoder.tasks("abc0xx")).rejects.toThrow();
64+
});
7165
});
72-
test("invalid task id", async()=>{
73-
await expect(atcoder.task("abc101", "abc102_a")).rejects.toThrow();
66+
const tasks = [["abc101", "abc101_a"], ["abc101", "abc101_b"], ["arc101", "arc101_a"]];
67+
describe("task", async () => {
68+
test.each(tasks)("%s %s", async (contest_id, task_id) => {
69+
expect(await atcoder.task(contest_id, task_id)).toMatchSnapshot();
70+
});
71+
test("invalid contest id", async () => {
72+
await expect(atcoder.task("abc0xx", "abc0xx_z")).rejects.toThrow();
73+
});
74+
test("invalid task id", async () => {
75+
await expect(atcoder.task("abc101", "abc102_a")).rejects.toThrow();
76+
});
7477
});
7578
});
7679
});

package-lock.json

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

0 commit comments

Comments
 (0)