|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +const mockInsertAccount = vi.fn(); |
| 4 | +const mockInsertAccountInfo = vi.fn(); |
| 5 | +const mockSelectAccountWithSocials = vi.fn(); |
| 6 | +const mockInsertAccountArtistId = vi.fn(); |
| 7 | +const mockAddArtistToOrganization = vi.fn(); |
| 8 | + |
| 9 | +vi.mock("@/lib/supabase/accounts/insertAccount", () => ({ |
| 10 | + insertAccount: (...args: unknown[]) => mockInsertAccount(...args), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("@/lib/supabase/account_info/insertAccountInfo", () => ({ |
| 14 | + insertAccountInfo: (...args: unknown[]) => mockInsertAccountInfo(...args), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("@/lib/supabase/accounts/selectAccountWithSocials", () => ({ |
| 18 | + selectAccountWithSocials: (...args: unknown[]) => mockSelectAccountWithSocials(...args), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("@/lib/supabase/account_artist_ids/insertAccountArtistId", () => ({ |
| 22 | + insertAccountArtistId: (...args: unknown[]) => mockInsertAccountArtistId(...args), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("@/lib/supabase/artist_organization_ids/addArtistToOrganization", () => ({ |
| 26 | + addArtistToOrganization: (...args: unknown[]) => mockAddArtistToOrganization(...args), |
| 27 | +})); |
| 28 | + |
| 29 | +import { createArtistInDb } from "../createArtistInDb"; |
| 30 | + |
| 31 | +describe("createArtistInDb", () => { |
| 32 | + const mockAccount = { |
| 33 | + id: "artist-123", |
| 34 | + name: "Test Artist", |
| 35 | + created_at: "2026-01-15T00:00:00Z", |
| 36 | + updated_at: "2026-01-15T00:00:00Z", |
| 37 | + }; |
| 38 | + |
| 39 | + const mockAccountInfo = { |
| 40 | + id: "info-123", |
| 41 | + account_id: "artist-123", |
| 42 | + image: null, |
| 43 | + instruction: null, |
| 44 | + knowledges: null, |
| 45 | + label: null, |
| 46 | + organization: null, |
| 47 | + company_name: null, |
| 48 | + job_title: null, |
| 49 | + role_type: null, |
| 50 | + onboarding_status: null, |
| 51 | + onboarding_data: null, |
| 52 | + }; |
| 53 | + |
| 54 | + const mockFullAccount = { |
| 55 | + ...mockAccount, |
| 56 | + account_socials: [], |
| 57 | + account_info: [mockAccountInfo], |
| 58 | + }; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("creates an artist account with all required steps", async () => { |
| 65 | + mockInsertAccount.mockResolvedValue(mockAccount); |
| 66 | + mockInsertAccountInfo.mockResolvedValue(mockAccountInfo); |
| 67 | + mockSelectAccountWithSocials.mockResolvedValue(mockFullAccount); |
| 68 | + mockInsertAccountArtistId.mockResolvedValue({ id: "rel-123" }); |
| 69 | + |
| 70 | + const result = await createArtistInDb("Test Artist", "owner-456"); |
| 71 | + |
| 72 | + expect(mockInsertAccount).toHaveBeenCalledWith({ name: "Test Artist" }); |
| 73 | + expect(mockInsertAccountInfo).toHaveBeenCalledWith({ account_id: "artist-123" }); |
| 74 | + expect(mockSelectAccountWithSocials).toHaveBeenCalledWith("artist-123"); |
| 75 | + expect(mockInsertAccountArtistId).toHaveBeenCalledWith("owner-456", "artist-123"); |
| 76 | + expect(result).toMatchObject({ |
| 77 | + id: "artist-123", |
| 78 | + account_id: "artist-123", |
| 79 | + name: "Test Artist", |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("links artist to organization when organizationId is provided", async () => { |
| 84 | + mockInsertAccount.mockResolvedValue(mockAccount); |
| 85 | + mockInsertAccountInfo.mockResolvedValue(mockAccountInfo); |
| 86 | + mockSelectAccountWithSocials.mockResolvedValue(mockFullAccount); |
| 87 | + mockInsertAccountArtistId.mockResolvedValue({ id: "rel-123" }); |
| 88 | + mockAddArtistToOrganization.mockResolvedValue("org-rel-123"); |
| 89 | + |
| 90 | + const result = await createArtistInDb("Test Artist", "owner-456", "org-789"); |
| 91 | + |
| 92 | + expect(mockAddArtistToOrganization).toHaveBeenCalledWith("artist-123", "org-789"); |
| 93 | + expect(result).not.toBeNull(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns null when account creation fails", async () => { |
| 97 | + mockInsertAccount.mockRejectedValue(new Error("Insert failed")); |
| 98 | + |
| 99 | + const result = await createArtistInDb("Test Artist", "owner-456"); |
| 100 | + |
| 101 | + expect(result).toBeNull(); |
| 102 | + expect(mockInsertAccountInfo).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns null when account info creation fails", async () => { |
| 106 | + mockInsertAccount.mockResolvedValue(mockAccount); |
| 107 | + mockInsertAccountInfo.mockResolvedValue(null); |
| 108 | + |
| 109 | + const result = await createArtistInDb("Test Artist", "owner-456"); |
| 110 | + |
| 111 | + expect(result).toBeNull(); |
| 112 | + expect(mockSelectAccountWithSocials).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("returns null when fetching full account data fails", async () => { |
| 116 | + mockInsertAccount.mockResolvedValue(mockAccount); |
| 117 | + mockInsertAccountInfo.mockResolvedValue(mockAccountInfo); |
| 118 | + mockSelectAccountWithSocials.mockResolvedValue(null); |
| 119 | + |
| 120 | + const result = await createArtistInDb("Test Artist", "owner-456"); |
| 121 | + |
| 122 | + expect(result).toBeNull(); |
| 123 | + expect(mockInsertAccountArtistId).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("returns null when associating artist with owner fails", async () => { |
| 127 | + mockInsertAccount.mockResolvedValue(mockAccount); |
| 128 | + mockInsertAccountInfo.mockResolvedValue(mockAccountInfo); |
| 129 | + mockSelectAccountWithSocials.mockResolvedValue(mockFullAccount); |
| 130 | + mockInsertAccountArtistId.mockRejectedValue(new Error("Association failed")); |
| 131 | + |
| 132 | + const result = await createArtistInDb("Test Artist", "owner-456"); |
| 133 | + |
| 134 | + expect(result).toBeNull(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments