|
| 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