Skip to content

Commit c800d90

Browse files
committed
Add test of syncArticlesFromQiita & pull command
1 parent d38516f commit c800d90

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

src/commands/pull.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { getFileSystemRepo } from "../lib/get-file-system-repo";
2+
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance";
3+
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita";
4+
import { pull } from "./pull";
5+
6+
jest.mock("../lib/get-qiita-api-instance");
7+
jest.mock("../lib/get-file-system-repo");
8+
jest.mock("../lib/sync-articles-from-qiita");
9+
const mockGetQiitaApiInstance = jest.mocked(getQiitaApiInstance);
10+
const mockGetFileSystemRepo = jest.mocked(getFileSystemRepo);
11+
const mockSyncArticlesFromQiita = jest.mocked(syncArticlesFromQiita);
12+
13+
describe("pull", () => {
14+
const qiitaApi = {} as ReturnType<typeof getQiitaApiInstance>;
15+
const fileSystemRepo = {} as ReturnType<typeof getFileSystemRepo>;
16+
17+
beforeEach(() => {
18+
mockGetQiitaApiInstance.mockReset();
19+
mockGetFileSystemRepo.mockReset();
20+
mockSyncArticlesFromQiita.mockReset();
21+
22+
mockGetQiitaApiInstance.mockReturnValue(qiitaApi);
23+
mockGetFileSystemRepo.mockReturnValue(fileSystemRepo);
24+
mockSyncArticlesFromQiita.mockImplementation();
25+
jest.spyOn(console, "log").mockImplementation();
26+
});
27+
28+
it("pulls articles", async () => {
29+
await pull([]);
30+
31+
expect(mockSyncArticlesFromQiita).toBeCalledWith({
32+
fileSystemRepo,
33+
qiitaApi,
34+
forceUpdate: undefined,
35+
});
36+
expect(mockSyncArticlesFromQiita).toBeCalledTimes(1);
37+
});
38+
39+
describe('with "--force" option', () => {
40+
it("pulls articles with forceUpdate", async () => {
41+
await pull(["--force"]);
42+
43+
expect(mockSyncArticlesFromQiita).toBeCalledWith({
44+
fileSystemRepo,
45+
qiitaApi,
46+
forceUpdate: true,
47+
});
48+
expect(mockSyncArticlesFromQiita).toBeCalledTimes(1);
49+
});
50+
51+
it("pulls articles with forceUpdate", async () => {
52+
await pull(["-f"]);
53+
54+
expect(mockSyncArticlesFromQiita).toBeCalledWith({
55+
fileSystemRepo,
56+
qiitaApi,
57+
forceUpdate: true,
58+
});
59+
});
60+
});
61+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { Item, QiitaApi } from "../qiita-api";
2+
import type { FileSystemRepo } from "./file-system-repo";
3+
import { syncArticlesFromQiita } from "./sync-articles-from-qiita";
4+
import { config } from "./config";
5+
6+
jest.mock("./config");
7+
const mockConfig = jest.mocked(config);
8+
9+
describe("syncArticlesFromQiita", () => {
10+
const qiitaApi = {
11+
authenticatedUserItems: () => {},
12+
} as unknown as QiitaApi;
13+
const fileSystemRepo = {
14+
saveItems: () => {},
15+
} as unknown as FileSystemRepo;
16+
17+
const mockAuthenticatedUserItems = jest.spyOn(
18+
qiitaApi,
19+
"authenticatedUserItems"
20+
);
21+
const mockSaveItems = jest.spyOn(fileSystemRepo, "saveItems");
22+
const mockGetUserConfig = jest.spyOn(mockConfig, "getUserConfig");
23+
24+
const items = [{ private: false }, { private: true }] as Item[];
25+
26+
beforeEach(() => {
27+
mockAuthenticatedUserItems.mockReset();
28+
mockSaveItems.mockReset();
29+
mockGetUserConfig.mockReset();
30+
31+
mockAuthenticatedUserItems.mockImplementation(async (page?: number) => {
32+
if (page && page < 2) return items;
33+
return [];
34+
});
35+
mockSaveItems.mockImplementation();
36+
});
37+
38+
describe("with userConfig", () => {
39+
describe("when includePrivate is true", () => {
40+
it("called saveItems with all item", async () => {
41+
mockGetUserConfig.mockImplementation(async () => ({
42+
includePrivate: true,
43+
}));
44+
45+
await syncArticlesFromQiita({ fileSystemRepo, qiitaApi });
46+
47+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(1, 1, 100);
48+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(2, 2, 100);
49+
expect(mockAuthenticatedUserItems).toBeCalledTimes(2);
50+
expect(mockSaveItems).toHaveBeenCalledWith(items, false);
51+
expect(mockSaveItems).toBeCalledTimes(1);
52+
});
53+
});
54+
55+
describe("when includePrivate is false", () => {
56+
it("called saveItems with only public item", async () => {
57+
mockGetUserConfig.mockImplementation(async () => ({
58+
includePrivate: false,
59+
}));
60+
61+
await syncArticlesFromQiita({ fileSystemRepo, qiitaApi });
62+
63+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(1, 1, 100);
64+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(2, 2, 100);
65+
expect(mockAuthenticatedUserItems).toBeCalledTimes(2);
66+
expect(mockSaveItems).toHaveBeenCalledWith([items[0]], false);
67+
expect(mockSaveItems).toBeCalledTimes(1);
68+
});
69+
});
70+
});
71+
72+
describe("with forceUpdate", () => {
73+
const expectSaveItemsToBeCalledWithForceUpdate = async (
74+
forceUpdate: boolean
75+
) => {
76+
mockGetUserConfig.mockImplementation(async () => ({
77+
includePrivate: true,
78+
}));
79+
80+
await syncArticlesFromQiita({
81+
fileSystemRepo,
82+
qiitaApi,
83+
forceUpdate,
84+
});
85+
86+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(1, 1, 100);
87+
expect(mockAuthenticatedUserItems).toHaveBeenNthCalledWith(2, 2, 100);
88+
expect(mockAuthenticatedUserItems).toBeCalledTimes(2);
89+
expect(mockSaveItems).toHaveBeenCalledWith(items, forceUpdate);
90+
expect(mockSaveItems).toBeCalledTimes(1);
91+
};
92+
93+
describe("when forceUpdate is true", () => {
94+
it("called saveItems without forceUpdate", async () => {
95+
await expectSaveItemsToBeCalledWithForceUpdate(true);
96+
});
97+
});
98+
99+
describe("when forceUpdate is false", () => {
100+
it("called saveItems without forceUpdate", async () => {
101+
await expectSaveItemsToBeCalledWithForceUpdate(false);
102+
});
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)