|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { render } from '@testing-library/react'; |
| 7 | +import { ViewProps } from '../../../../../data_explorer/public'; |
| 8 | + |
| 9 | +import DiscoverContext from './index'; |
| 10 | + |
| 11 | +// Mock the heavy dependencies so we can focus on the page-context registration logic. |
| 12 | +jest.mock('../utils/use_search', () => ({ |
| 13 | + useSearch: jest.fn(() => ({})), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('../../../../../opensearch_dashboards_react/public', () => ({ |
| 17 | + useOpenSearchDashboards: () => ({ services: {} }), |
| 18 | + OpenSearchDashboardsContextProvider: () => null, |
| 19 | +})); |
| 20 | + |
| 21 | +const mockUsePageContext = jest.fn(); |
| 22 | +const mockGetServices = jest.fn(); |
| 23 | +jest.mock('../../../opensearch_dashboards_services', () => ({ |
| 24 | + getServices: () => mockGetServices(), |
| 25 | +})); |
| 26 | + |
| 27 | +// DiscoverContext is mounted by the router with full ViewProps (AppMountParameters); |
| 28 | +// for these unit tests we only care about the page-context side effect, so we cast a |
| 29 | +// minimal props object and let the mocked hooks handle the rest. |
| 30 | +const renderContext = () => |
| 31 | + render( |
| 32 | + <DiscoverContext {...({} as ViewProps)}> |
| 33 | + <div>child</div> |
| 34 | + </DiscoverContext> |
| 35 | + ); |
| 36 | + |
| 37 | +describe('DiscoverContext page context registration', () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('registers page context with the contextProvider hook when available', () => { |
| 43 | + mockGetServices.mockReturnValue({ |
| 44 | + contextProvider: { hooks: { usePageContext: mockUsePageContext } }, |
| 45 | + }); |
| 46 | + |
| 47 | + renderContext(); |
| 48 | + |
| 49 | + expect(mockUsePageContext).toHaveBeenCalledTimes(1); |
| 50 | + const options = mockUsePageContext.mock.calls[0][0]; |
| 51 | + expect(options.description).toBe('Discover application page context'); |
| 52 | + expect(options.categories).toEqual(['page', 'static']); |
| 53 | + expect(typeof options.convert).toBe('function'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('maps url state to the expected page context shape via convert', () => { |
| 57 | + mockGetServices.mockReturnValue({ |
| 58 | + contextProvider: { hooks: { usePageContext: mockUsePageContext } }, |
| 59 | + }); |
| 60 | + |
| 61 | + renderContext(); |
| 62 | + |
| 63 | + const { convert } = mockUsePageContext.mock.calls[0][0]; |
| 64 | + |
| 65 | + const dataset = { id: 'logs-*', title: 'logs-*', type: 'INDEX_PATTERN' }; |
| 66 | + const result = convert({ |
| 67 | + _g: { time: { from: 'now-15m', to: 'now' } }, |
| 68 | + _q: { query: { query: 'status:200', language: 'PPL', dataset } }, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result).toEqual({ |
| 72 | + appId: 'discover', |
| 73 | + timeRange: { from: 'now-15m', to: 'now' }, |
| 74 | + query: { query: 'status:200', language: 'PPL' }, |
| 75 | + dataset, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('falls back to safe defaults when url state is empty', () => { |
| 80 | + mockGetServices.mockReturnValue({ |
| 81 | + contextProvider: { hooks: { usePageContext: mockUsePageContext } }, |
| 82 | + }); |
| 83 | + |
| 84 | + renderContext(); |
| 85 | + |
| 86 | + const { convert } = mockUsePageContext.mock.calls[0][0]; |
| 87 | + const result = convert({}); |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + appId: 'discover', |
| 91 | + timeRange: undefined, |
| 92 | + query: { query: '', language: 'kuery' }, |
| 93 | + dataset: undefined, |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does not throw when the contextProvider plugin is unavailable (NOOP fallback)', () => { |
| 98 | + mockGetServices.mockReturnValue({}); |
| 99 | + |
| 100 | + expect(() => renderContext()).not.toThrow(); |
| 101 | + expect(mockUsePageContext).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments