|
| 1 | +import { act, renderHook } from '@testing-library/react' |
| 2 | +import { DownloadPlace, SegmentEvent } from '../modules/segment' |
| 3 | +import { useCreatorHubDownload } from './useCreatorHubDownload' |
| 4 | + |
| 5 | +const mockTrack = jest.fn() |
| 6 | +const mockTriggerFileDownload = jest.fn() |
| 7 | + |
| 8 | +let mockAnonUserId: string | undefined = 'anon-xyz' |
| 9 | +let mockHasValidIdentity = false |
| 10 | +let mockUserAgent: [boolean, { os: { name: string }; cpu: { architecture: string } } | undefined] = [ |
| 11 | + false, |
| 12 | + { os: { name: 'macOS' }, cpu: { architecture: 'arm64' } } |
| 13 | +] |
| 14 | +let mockLinks: Record<string, Record<string, string>> | undefined |
| 15 | +let mockLinksLoading = false |
| 16 | + |
| 17 | +jest.mock('@dcl/hooks', () => ({ |
| 18 | + useAdvancedUserAgentData: () => mockUserAgent |
| 19 | +})) |
| 20 | + |
| 21 | +jest.mock('./useDeferredTrack', () => ({ |
| 22 | + useDeferredTrack: () => mockTrack |
| 23 | +})) |
| 24 | + |
| 25 | +jest.mock('./useAnonUserId', () => ({ |
| 26 | + ANON_USER_ID_PARAM: 'anon_user_id', |
| 27 | + useAnonUserId: () => mockAnonUserId |
| 28 | +})) |
| 29 | + |
| 30 | +jest.mock('./useAuthIdentity', () => ({ |
| 31 | + useAuthIdentity: () => ({ hasValidIdentity: mockHasValidIdentity }) |
| 32 | +})) |
| 33 | + |
| 34 | +jest.mock('./useLatestGithubRelease', () => ({ |
| 35 | + Repo: { CREATOR_HUB: 'creator-hub' }, |
| 36 | + useLatestGithubRelease: () => ({ links: mockLinks, loading: mockLinksLoading }) |
| 37 | +})) |
| 38 | + |
| 39 | +jest.mock('../modules/file', () => ({ |
| 40 | + triggerFileDownload: (...args: unknown[]) => mockTriggerFileDownload(...args) |
| 41 | +})) |
| 42 | + |
| 43 | +jest.mock('../modules/url', () => ({ |
| 44 | + addQueryParamsToUrlString: (url: string, params: Record<string, string | undefined | null>) => { |
| 45 | + const u = new URL(url) |
| 46 | + Object.entries(params).forEach(([key, value]) => { |
| 47 | + if (value !== undefined && value !== null) u.searchParams.append(key, value) |
| 48 | + }) |
| 49 | + return u.toString() |
| 50 | + }, |
| 51 | + updateUrlWithLastValue: (url: string, key: string, value: string) => { |
| 52 | + const u = new URL(url) |
| 53 | + u.searchParams.delete(key) |
| 54 | + u.searchParams.append(key, value) |
| 55 | + return u.toString() |
| 56 | + } |
| 57 | +})) |
| 58 | + |
| 59 | +const MAC_LINK = 'https://cdn.example.com/Decentraland-Creator-Hub-arm64.dmg' |
| 60 | +const WIN_LINK = 'https://cdn.example.com/Creator-Hub-Setup.exe' |
| 61 | + |
| 62 | +const originalLocation = window.location |
| 63 | + |
| 64 | +describe('useCreatorHubDownload', () => { |
| 65 | + beforeAll(() => { |
| 66 | + Object.defineProperty(window, 'location', { |
| 67 | + configurable: true, |
| 68 | + writable: true, |
| 69 | + value: { href: '', origin: 'http://localhost' } |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + afterAll(() => { |
| 74 | + Object.defineProperty(window, 'location', { configurable: true, writable: true, value: originalLocation }) |
| 75 | + }) |
| 76 | + |
| 77 | + beforeEach(() => { |
| 78 | + jest.useFakeTimers() |
| 79 | + mockAnonUserId = 'anon-xyz' |
| 80 | + mockHasValidIdentity = false |
| 81 | + mockUserAgent = [false, { os: { name: 'macOS' }, cpu: { architecture: 'arm64' } }] |
| 82 | + mockLinks = { macOS: { arm64: MAC_LINK }, Windows: { amd64: WIN_LINK } } |
| 83 | + mockLinksLoading = false |
| 84 | + window.location.href = '' |
| 85 | + }) |
| 86 | + |
| 87 | + afterEach(() => { |
| 88 | + jest.runOnlyPendingTimers() |
| 89 | + jest.useRealTimers() |
| 90 | + jest.resetAllMocks() |
| 91 | + }) |
| 92 | + |
| 93 | + describe('when handleDownload is called with a valid option', () => { |
| 94 | + it('should fire download_started with the creator-hub-download-page place and trigger the file download', () => { |
| 95 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 96 | + |
| 97 | + act(() => { |
| 98 | + result.current.handleDownload(result.current.primaryOption!) |
| 99 | + }) |
| 100 | + |
| 101 | + expect(mockTrack).toHaveBeenCalledWith( |
| 102 | + SegmentEvent.DOWNLOAD_STARTED, |
| 103 | + expect.objectContaining({ |
| 104 | + href: MAC_LINK, |
| 105 | + os: 'macOS', |
| 106 | + arch: 'arm64', |
| 107 | + place: DownloadPlace.CREATOR_HUB_DOWNLOAD_PAGE, |
| 108 | + |
| 109 | + anon_user_id: 'anon-xyz', |
| 110 | + |
| 111 | + auth_state: 'anonymous', |
| 112 | + revisit: 0, |
| 113 | + |
| 114 | + started_at: expect.any(Number) |
| 115 | + }) |
| 116 | + ) |
| 117 | + expect(mockTriggerFileDownload).toHaveBeenCalledWith(MAC_LINK) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should report auth_state authenticated when the visitor has a valid identity', () => { |
| 121 | + mockHasValidIdentity = true |
| 122 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 123 | + |
| 124 | + act(() => { |
| 125 | + result.current.handleDownload(result.current.primaryOption!) |
| 126 | + }) |
| 127 | + |
| 128 | + expect(mockTrack).toHaveBeenCalledWith( |
| 129 | + SegmentEvent.DOWNLOAD_STARTED, |
| 130 | + |
| 131 | + expect.objectContaining({ auth_state: 'authenticated' }) |
| 132 | + ) |
| 133 | + }) |
| 134 | + |
| 135 | + it('should forward arch, os and anon_user_id to the success redirect url', () => { |
| 136 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 137 | + |
| 138 | + act(() => { |
| 139 | + result.current.handleDownload(result.current.primaryOption!) |
| 140 | + }) |
| 141 | + act(() => { |
| 142 | + jest.advanceTimersByTime(3000) |
| 143 | + }) |
| 144 | + |
| 145 | + expect(window.location.href).toContain('/download/creator-hub-success') |
| 146 | + expect(window.location.href).toContain('os=macOS') |
| 147 | + expect(window.location.href).toContain('arch=arm64') |
| 148 | + expect(window.location.href).toContain('anon_user_id=anon-xyz') |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + describe('when anon_user_id is unavailable', () => { |
| 153 | + beforeEach(() => { |
| 154 | + mockAnonUserId = undefined |
| 155 | + }) |
| 156 | + |
| 157 | + it('should omit anon_user_id from the event payload and the redirect url', () => { |
| 158 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 159 | + |
| 160 | + act(() => { |
| 161 | + result.current.handleDownload(result.current.primaryOption!) |
| 162 | + }) |
| 163 | + act(() => { |
| 164 | + jest.advanceTimersByTime(3000) |
| 165 | + }) |
| 166 | + |
| 167 | + const payload = mockTrack.mock.calls[0][1] |
| 168 | + expect(payload).not.toHaveProperty('anon_user_id') |
| 169 | + expect(window.location.href).not.toContain('anon_user_id') |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + describe('when the option has no download link', () => { |
| 174 | + it('should not track or trigger a download', () => { |
| 175 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 176 | + |
| 177 | + act(() => { |
| 178 | + result.current.handleDownload({ text: 'macOS', image: 'icon.svg' }) |
| 179 | + }) |
| 180 | + |
| 181 | + expect(mockTrack).not.toHaveBeenCalled() |
| 182 | + expect(mockTriggerFileDownload).not.toHaveBeenCalled() |
| 183 | + }) |
| 184 | + }) |
| 185 | + |
| 186 | + describe('when handleDownload is called twice with a pending redirect', () => { |
| 187 | + it('should clear the previous redirect timer and re-fire the funnel', () => { |
| 188 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 189 | + |
| 190 | + act(() => { |
| 191 | + result.current.handleDownload(result.current.primaryOption!) |
| 192 | + }) |
| 193 | + act(() => { |
| 194 | + result.current.handleDownload(result.current.primaryOption!) |
| 195 | + }) |
| 196 | + |
| 197 | + expect(mockTrack).toHaveBeenCalledTimes(2) |
| 198 | + expect(mockTriggerFileDownload).toHaveBeenCalledTimes(2) |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe('when the visitor OS has no available download', () => { |
| 203 | + beforeEach(() => { |
| 204 | + mockUserAgent = [false, { os: { name: 'Linux' }, cpu: { architecture: 'amd64' } }] |
| 205 | + }) |
| 206 | + |
| 207 | + it('should expose no primary option', () => { |
| 208 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 209 | + |
| 210 | + expect(result.current.primaryOption).toBeNull() |
| 211 | + }) |
| 212 | + }) |
| 213 | + |
| 214 | + describe('when user agent data is still loading', () => { |
| 215 | + beforeEach(() => { |
| 216 | + mockLinksLoading = true |
| 217 | + }) |
| 218 | + |
| 219 | + it('should report not ready', () => { |
| 220 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 221 | + |
| 222 | + expect(result.current.isReady).toBe(false) |
| 223 | + }) |
| 224 | + }) |
| 225 | + |
| 226 | + describe('when the visitor is on Windows', () => { |
| 227 | + beforeEach(() => { |
| 228 | + mockUserAgent = [false, { os: { name: 'Windows' }, cpu: { architecture: 'amd64' } }] |
| 229 | + }) |
| 230 | + |
| 231 | + it('should expose macOS as a secondary option', () => { |
| 232 | + const { result } = renderHook(() => useCreatorHubDownload()) |
| 233 | + |
| 234 | + expect(result.current.primaryOption?.text).toBe('Windows') |
| 235 | + expect(result.current.secondaryOptions.map(option => option.text)).toContain('macOS') |
| 236 | + }) |
| 237 | + }) |
| 238 | +}) |
0 commit comments