|
| 1 | +/** |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { render } from '@testing-library/react'; |
| 13 | + |
| 14 | +const mockNavigate = vi.fn(); |
| 15 | +const mockLocation = { pathname: '/topics', searchStr: '' }; |
| 16 | +const mockRouter = {}; |
| 17 | + |
| 18 | +vi.mock('@tanstack/react-router', () => ({ |
| 19 | + useNavigate: () => mockNavigate, |
| 20 | + useLocation: () => mockLocation, |
| 21 | + useRouter: () => mockRouter, |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../../config', () => ({ |
| 25 | + config: { |
| 26 | + jwt: undefined as string | undefined, |
| 27 | + clusterId: undefined as string | undefined, |
| 28 | + }, |
| 29 | + isEmbedded: vi.fn(() => false), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('../../hubspot/hubspot.helper', () => ({ |
| 33 | + trackHubspotPage: vi.fn(), |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('../../state/app-global', () => ({ |
| 37 | + appGlobal: { |
| 38 | + setNavigate: vi.fn(), |
| 39 | + setRouter: vi.fn(), |
| 40 | + setLocation: vi.fn(), |
| 41 | + }, |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('../../state/backend-api', () => ({ |
| 45 | + api: { |
| 46 | + errors: [], |
| 47 | + }, |
| 48 | +})); |
| 49 | + |
| 50 | +import { RouterSync } from './router-sync'; |
| 51 | +import { config, isEmbedded } from '../../config'; |
| 52 | + |
| 53 | +describe('RouterSync', () => { |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + config.jwt = undefined; |
| 57 | + config.clusterId = undefined; |
| 58 | + mockLocation.pathname = '/topics'; |
| 59 | + }); |
| 60 | + |
| 61 | + test('does not dispatch [console] navigated events when not embedded', () => { |
| 62 | + const dispatchSpy = vi.spyOn(window, 'dispatchEvent'); |
| 63 | + vi.mocked(isEmbedded).mockReturnValue(false); |
| 64 | + |
| 65 | + render(<RouterSync />); |
| 66 | + |
| 67 | + const consoleEvents = dispatchSpy.mock.calls.filter( |
| 68 | + ([event]) => event instanceof CustomEvent && event.type === '[console] navigated' |
| 69 | + ); |
| 70 | + expect(consoleEvents).toHaveLength(0); |
| 71 | + |
| 72 | + dispatchSpy.mockRestore(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('does not dispatch [console] navigated events in federated mode (clusterId set)', () => { |
| 76 | + const dispatchSpy = vi.spyOn(window, 'dispatchEvent'); |
| 77 | + vi.mocked(isEmbedded).mockReturnValue(true); |
| 78 | + config.clusterId = 'test-cluster-123'; |
| 79 | + mockLocation.pathname = '/schema-registry'; |
| 80 | + |
| 81 | + render(<RouterSync />); |
| 82 | + |
| 83 | + const consoleEvents = dispatchSpy.mock.calls.filter( |
| 84 | + ([event]) => event instanceof CustomEvent && event.type === '[console] navigated' |
| 85 | + ); |
| 86 | + expect(consoleEvents).toHaveLength(0); |
| 87 | + |
| 88 | + dispatchSpy.mockRestore(); |
| 89 | + }); |
| 90 | + |
| 91 | + test('dispatches [console] navigated events in embedded non-federated mode', () => { |
| 92 | + const dispatchSpy = vi.spyOn(window, 'dispatchEvent'); |
| 93 | + vi.mocked(isEmbedded).mockReturnValue(true); |
| 94 | + config.clusterId = undefined; |
| 95 | + mockLocation.pathname = '/schema-registry'; |
| 96 | + |
| 97 | + render(<RouterSync />); |
| 98 | + |
| 99 | + const consoleEvents = dispatchSpy.mock.calls.filter( |
| 100 | + ([event]) => event instanceof CustomEvent && event.type === '[console] navigated' |
| 101 | + ); |
| 102 | + expect(consoleEvents).toHaveLength(1); |
| 103 | + expect((consoleEvents[0][0] as CustomEvent).detail).toBe('/schema-registry'); |
| 104 | + |
| 105 | + dispatchSpy.mockRestore(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments