|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 5 | + |
| 6 | +import { FileViewer } from '../../src/components/FileViewer'; |
| 7 | +import type { ProjectFile } from '../../src/types'; |
| 8 | + |
| 9 | +const { analyticsTrackMock } = vi.hoisted(() => ({ |
| 10 | + analyticsTrackMock: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('../../src/analytics/provider', async () => { |
| 14 | + const actual = await vi.importActual<typeof import('../../src/analytics/provider')>( |
| 15 | + '../../src/analytics/provider', |
| 16 | + ); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + useAnalytics: () => ({ |
| 20 | + ...actual.useAnalytics(), |
| 21 | + track: analyticsTrackMock, |
| 22 | + }), |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + cleanup(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | + vi.unstubAllGlobals(); |
| 30 | + analyticsTrackMock.mockReset(); |
| 31 | +}); |
| 32 | + |
| 33 | +function baseFile(overrides: Partial<ProjectFile>): ProjectFile { |
| 34 | + return { |
| 35 | + name: 'asset.png', |
| 36 | + path: 'asset.png', |
| 37 | + type: 'file', |
| 38 | + size: 1024, |
| 39 | + mtime: 1710000000, |
| 40 | + kind: 'image', |
| 41 | + mime: 'image/png', |
| 42 | + ...overrides, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function deployableHtmlFile(): ProjectFile { |
| 47 | + return baseFile({ |
| 48 | + name: 'index.html', |
| 49 | + path: 'index.html', |
| 50 | + mime: 'text/html', |
| 51 | + kind: 'html', |
| 52 | + artifactManifest: { |
| 53 | + version: 1, |
| 54 | + kind: 'html', |
| 55 | + title: 'Page', |
| 56 | + entry: 'index.html', |
| 57 | + renderer: 'html', |
| 58 | + exports: ['html'], |
| 59 | + }, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function mockDeployFetch() { |
| 64 | + return vi.fn(async (input: string | URL | Request, init?: RequestInit) => { |
| 65 | + const url = typeof input === 'string' ? input : input instanceof Request ? input.url : String(input); |
| 66 | + const method = init?.method || (input instanceof Request ? input.method : 'GET'); |
| 67 | + |
| 68 | + if (url === '/api/projects/project-1/deployments') { |
| 69 | + return new Response(JSON.stringify({ deployments: [] }), { status: 200 }); |
| 70 | + } |
| 71 | + if (url.startsWith('/api/deploy/config') && method === 'PUT') { |
| 72 | + const body = JSON.parse(String(init?.body ?? '{}')) as Record<string, unknown>; |
| 73 | + return new Response(JSON.stringify({ |
| 74 | + providerId: body.providerId ?? 'netlify', |
| 75 | + configured: true, |
| 76 | + tokenMask: 'saved-token', |
| 77 | + githubTokenMask: 'saved-github-token', |
| 78 | + }), { status: 200 }); |
| 79 | + } |
| 80 | + if (url.startsWith('/api/deploy/config') && method === 'GET') { |
| 81 | + const parsedUrl = new URL(url, 'http://localhost'); |
| 82 | + const providerId = parsedUrl.searchParams.get('providerId') ?? 'cloudflare-pages'; |
| 83 | + return new Response(JSON.stringify({ |
| 84 | + providerId, |
| 85 | + configured: false, |
| 86 | + tokenMask: '', |
| 87 | + githubTokenMask: '', |
| 88 | + }), { status: 200 }); |
| 89 | + } |
| 90 | + if (url === '/api/projects/project-1/deploy' && method === 'POST') { |
| 91 | + const body = JSON.parse(String(init?.body ?? '{}')) as Record<string, unknown>; |
| 92 | + return new Response(JSON.stringify({ |
| 93 | + id: 'deploy-1', |
| 94 | + projectId: 'project-1', |
| 95 | + fileName: 'index.html', |
| 96 | + providerId: body.providerId ?? 'netlify', |
| 97 | + url: 'https://demo.netlify.app', |
| 98 | + deploymentId: 'dep-1', |
| 99 | + deploymentCount: 1, |
| 100 | + target: 'production', |
| 101 | + status: 'ready', |
| 102 | + createdAt: 1, |
| 103 | + updatedAt: 2, |
| 104 | + }), { status: 200 }); |
| 105 | + } |
| 106 | + return new Response(JSON.stringify({}), { status: 404 }); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +describe('FileViewer deploy analytics attribution', () => { |
| 111 | + it('tracks artifact_deploy_result with provider: "netlify" and saved_new_token: true when GitHub PAT is entered', async () => { |
| 112 | + vi.stubGlobal('fetch', mockDeployFetch()); |
| 113 | + |
| 114 | + render( |
| 115 | + <FileViewer projectId="project-1" projectKind="prototype" file={deployableHtmlFile()} |
| 116 | + liveHtml="<html><body><h1>Hello</h1></body></html>" |
| 117 | + />, |
| 118 | + ); |
| 119 | + |
| 120 | + fireEvent.click(screen.getByRole('button', { name: /share/i })); |
| 121 | + fireEvent.click(await screen.findByRole('menuitem', { name: /Deploy to Cloudflare Pages/i })); |
| 122 | + |
| 123 | + const providerSelect = await screen.findByRole('combobox', { name: /Provider/i }); |
| 124 | + fireEvent.change(providerSelect, { target: { value: 'netlify' } }); |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect((providerSelect as HTMLSelectElement).value).toBe('netlify'); |
| 128 | + }); |
| 129 | + |
| 130 | + const deployTokenInput = document.getElementById('deploy-token'); |
| 131 | + expect(deployTokenInput).not.toBeNull(); |
| 132 | + fireEvent.change(deployTokenInput!, { target: { value: 'nnt_123456' } }); |
| 133 | + |
| 134 | + const githubPatInput = document.getElementById('github-pat-token'); |
| 135 | + expect(githubPatInput).not.toBeNull(); |
| 136 | + fireEvent.change(githubPatInput!, { target: { value: 'ghp_secretpat123' } }); |
| 137 | + |
| 138 | + const deployButtons = screen.getAllByRole('button', { name: /^Deploy$/i }); |
| 139 | + fireEvent.click(deployButtons[deployButtons.length - 1]!); |
| 140 | + |
| 141 | + await waitFor(() => { |
| 142 | + const trackCalls = analyticsTrackMock.mock.calls.filter( |
| 143 | + (call: [string, Record<string, unknown>]) => call[0] === 'artifact_deploy_result', |
| 144 | + ); |
| 145 | + expect(trackCalls.length).toBeGreaterThan(0); |
| 146 | + const [, props] = trackCalls[0]; |
| 147 | + expect(props).toMatchObject({ |
| 148 | + page_name: 'artifact', |
| 149 | + area: 'deploy_modal', |
| 150 | + provider: 'netlify', |
| 151 | + result: 'success', |
| 152 | + saved_new_token: true, |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments