|
| 1 | +import { vi, describe, it, expect, beforeEach, Mock } from "vitest"; |
| 2 | +import { getCaptchaToken } from "../lib/auth"; |
| 3 | +import { |
| 4 | + CreateSendLinksResponse, |
| 5 | + SendLink, |
| 6 | + UploadService, |
| 7 | +} from "./upload.service"; |
| 8 | +import { FileWithPath } from "react-dropzone"; |
| 9 | +import { SendItemData } from "../models/SendItem"; |
| 10 | +import axios from "axios"; |
| 11 | + |
| 12 | +vi.mock("axios"); |
| 13 | + |
| 14 | +vi.mock("../lib/auth", () => ({ |
| 15 | + getCaptchaToken: vi.fn().mockResolvedValue("mock-token"), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("./upload.service", async () => { |
| 19 | + const mod = await vi.importActual<typeof import("./upload.service")>( |
| 20 | + "./upload.service" |
| 21 | + ); |
| 22 | + return { |
| 23 | + ...mod, |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +const mockSendLink: SendLink = { |
| 28 | + id: "mock-id-12345", |
| 29 | + name: "example-file.txt", |
| 30 | + type: "file", |
| 31 | + size: 1024, |
| 32 | + networkId: "network-id-67890", |
| 33 | + encryptionKey: "encryption-key-abcdef", |
| 34 | + parent_folder: null, |
| 35 | +}; |
| 36 | + |
| 37 | +const mockSendLinkFolder: SendLink = { |
| 38 | + id: "mock-id-54321", |
| 39 | + name: "example-folder", |
| 40 | + type: "folder", |
| 41 | + size: 0, |
| 42 | + networkId: "network-id-09876", |
| 43 | + encryptionKey: "encryption-key-zyxwvu", |
| 44 | + parent_folder: "parent-folder-id", |
| 45 | +}; |
| 46 | + |
| 47 | +const mockCreateSendLinksResponse: CreateSendLinksResponse = { |
| 48 | + id: "mock-id-12345", |
| 49 | + title: "Example Send Link", |
| 50 | + subject: "Example Subject", |
| 51 | + code: "mock-code-abcdef", |
| 52 | + sender: "mock-sender@example.com", |
| 53 | + receivers: ["receiver1@example.com", "receiver2@example.com"], |
| 54 | + views: 5, |
| 55 | + userId: 42, |
| 56 | + items: [ |
| 57 | + { |
| 58 | + id: "item-id-1", |
| 59 | + name: "example-file.txt", |
| 60 | + type: "file", |
| 61 | + size: 1024, // 1 KB |
| 62 | + networkId: "network-id-67890", |
| 63 | + encryptionKey: "encryption-key-abcdef", |
| 64 | + parent_folder: null, |
| 65 | + }, |
| 66 | + { |
| 67 | + id: "item-id-2", |
| 68 | + name: "example-folder", |
| 69 | + type: "folder", |
| 70 | + size: 0, |
| 71 | + networkId: "network-id-09876", |
| 72 | + encryptionKey: "encryption-key-zyxwvu", |
| 73 | + parent_folder: "parent-folder-id", |
| 74 | + }, |
| 75 | + ], |
| 76 | + createdAt: new Date().toISOString(), |
| 77 | + updatedAt: new Date().toISOString(), |
| 78 | + expirationAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(), |
| 79 | +}; |
| 80 | + |
| 81 | +beforeEach(() => { |
| 82 | + vi.clearAllMocks(); |
| 83 | + (getCaptchaToken as Mock).mockResolvedValue("mock-token"); |
| 84 | + (axios.post as Mock).mockResolvedValue({ data: mockCreateSendLinksResponse }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe("upload.service", () => { |
| 88 | + it("When the uploadFiles() is done, then should call storeSendLinks() and ensure getCaptchaToken() is called once", async () => { |
| 89 | + const uploadFilesSpy = vi.spyOn(UploadService, "uploadFiles"); |
| 90 | + const storeSendLinksSpy = vi.spyOn(UploadService, "storeSendLinks"); |
| 91 | + |
| 92 | + const axiosPostSpy = vi.spyOn(axios, "post"); |
| 93 | + |
| 94 | + uploadFilesSpy.mockResolvedValue([mockSendLink, mockSendLinkFolder]); |
| 95 | + |
| 96 | + const result = await UploadService.uploadFilesAndGetLink([ |
| 97 | + { |
| 98 | + id: "1", |
| 99 | + name: "file1.txt", |
| 100 | + type: "file", |
| 101 | + size: 100, |
| 102 | + file: {} as unknown as FileWithPath, |
| 103 | + } as unknown as SendItemData, |
| 104 | + { |
| 105 | + id: "2", |
| 106 | + name: "file2.txt", |
| 107 | + type: "file", |
| 108 | + size: 200, |
| 109 | + file: {} as unknown as FileWithPath, |
| 110 | + } as unknown as SendItemData, |
| 111 | + ]); |
| 112 | + |
| 113 | + expect(uploadFilesSpy).toHaveBeenCalledTimes(1); |
| 114 | + expect(getCaptchaToken).toHaveBeenCalledTimes(1); |
| 115 | + expect(storeSendLinksSpy).toHaveBeenCalledTimes(1); |
| 116 | + expect(storeSendLinksSpy).toHaveBeenCalledWith( |
| 117 | + expect.objectContaining({ |
| 118 | + items: expect.any(Array), |
| 119 | + code: expect.any(String), |
| 120 | + mnemonic: expect.any(String), |
| 121 | + plainCode: expect.any(String), |
| 122 | + }) |
| 123 | + ); |
| 124 | + expect(axiosPostSpy).toHaveBeenCalledTimes(1); |
| 125 | + expect(uploadFilesSpy.mock.invocationCallOrder[0]).toBeLessThan( |
| 126 | + (getCaptchaToken as Mock).mock.invocationCallOrder[0] |
| 127 | + ); |
| 128 | + expect((getCaptchaToken as Mock).mock.invocationCallOrder[0]).toBeLessThan( |
| 129 | + axiosPostSpy.mock.invocationCallOrder[0] |
| 130 | + ); |
| 131 | + expect(result).toContain("/download/"); |
| 132 | + expect(result).toContain("?code="); |
| 133 | + }); |
| 134 | + |
| 135 | + it("When storeSendLinks() is called, then getCaptchaToken() should be called before all", async () => { |
| 136 | + const storeSendLinksSpy = vi.spyOn(UploadService, "storeSendLinks"); |
| 137 | + vi.spyOn(UploadService, "uploadFiles").mockResolvedValue([mockSendLink]); |
| 138 | + const axiosPostSpy = vi.spyOn(axios, "post"); |
| 139 | + |
| 140 | + await UploadService.uploadFilesAndGetLink([ |
| 141 | + { |
| 142 | + id: "1", |
| 143 | + name: "file1.txt", |
| 144 | + type: "file", |
| 145 | + size: 100, |
| 146 | + file: {} as unknown as FileWithPath, |
| 147 | + } as unknown as SendItemData, |
| 148 | + ]); |
| 149 | + |
| 150 | + expect(getCaptchaToken).toHaveBeenCalled(); |
| 151 | + expect(storeSendLinksSpy).toHaveBeenCalled(); |
| 152 | + expect(storeSendLinksSpy.mock.invocationCallOrder[0]).toBeLessThan( |
| 153 | + (getCaptchaToken as Mock).mock.invocationCallOrder[0] |
| 154 | + ); |
| 155 | + expect((getCaptchaToken as Mock).mock.invocationCallOrder[0]).toBeLessThan( |
| 156 | + axiosPostSpy.mock.invocationCallOrder[0] |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
0 commit comments