|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React from 'react'; |
| 7 | +import { EuiButtonIcon, EuiLink } from '@elastic/eui'; |
| 8 | +import { mount } from 'enzyme'; |
| 9 | +import { Homepage } from './homepage'; |
| 10 | +import { |
| 11 | + HomeOpenSearchDashboardsServices, |
| 12 | + setServices, |
| 13 | +} from '../../opensearch_dashboards_services'; |
| 14 | +import { createHomeServicesMock } from '../../../mocks'; |
| 15 | +import { SectionTypeService } from '../../../services'; |
| 16 | +import { BehaviorSubject } from 'rxjs'; |
| 17 | +import { HeroSection, Section } from '../../../services/section_type/section_type'; |
| 18 | +import { OpenSearchDashboardsContextProvider } from 'src/plugins/opensearch_dashboards_react/public'; |
| 19 | +import { mountWithIntl } from 'test_utils/enzyme_helpers'; |
| 20 | +import { act } from 'react-dom/test-utils'; |
| 21 | +import { Welcome } from '../welcome'; |
| 22 | + |
| 23 | +let services: HomeOpenSearchDashboardsServices; |
| 24 | +const mockErrors = new BehaviorSubject<unknown | undefined>(undefined); |
| 25 | +let mockHeros: any; |
| 26 | +let mockSections: any; |
| 27 | + |
| 28 | +// Mock implementation of the SectionTypeService class |
| 29 | +jest.mock('../../../services', () => { |
| 30 | + return { |
| 31 | + SectionTypeService: jest.fn().mockImplementation(() => ({ |
| 32 | + setup: jest.fn().mockReturnValue({ |
| 33 | + registerHeroSection: jest.fn(), |
| 34 | + registerSection: jest.fn(), |
| 35 | + }), |
| 36 | + start: jest.fn(), |
| 37 | + getHomepage: () => { |
| 38 | + return { |
| 39 | + heroes$: mockHeros, |
| 40 | + sections$: mockSections, |
| 41 | + error$: mockErrors, |
| 42 | + saveHomepage: jest.fn(), |
| 43 | + fetchHomepageData: jest.fn(), |
| 44 | + getHeroSectionTypes: jest.fn(), |
| 45 | + getSectionTypes: jest.fn(), |
| 46 | + getSavedHomepageLoader: jest.fn(), |
| 47 | + cleanup: jest.fn(), |
| 48 | + }; |
| 49 | + }, |
| 50 | + })), |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +describe('Home page', () => { |
| 55 | + beforeAll(() => { |
| 56 | + services = createHomeServicesMock(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders the loading spinner if home page is still being loaded', async () => { |
| 60 | + mockSections = new BehaviorSubject<Section[] | undefined>(undefined); |
| 61 | + mockHeros = new BehaviorSubject<HeroSection[] | undefined>(undefined); |
| 62 | + const sectionTypes = new SectionTypeService(); |
| 63 | + |
| 64 | + setServices({ |
| 65 | + ...services, |
| 66 | + homeConfig: { |
| 67 | + disableWelcomeScreen: true, |
| 68 | + disableNewThemeModal: true, |
| 69 | + }, |
| 70 | + sectionTypes, |
| 71 | + }); |
| 72 | + |
| 73 | + await act(async () => { |
| 74 | + const component = mountWithIntl( |
| 75 | + <OpenSearchDashboardsContextProvider services={services}> |
| 76 | + <Homepage /> |
| 77 | + </OpenSearchDashboardsContextProvider> |
| 78 | + ); |
| 79 | + expect(component.find('[data-test-subj="loading"]')).toBeTruthy(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('renders the welcome modal if enabled', async () => { |
| 84 | + mockSections = new BehaviorSubject<Section[] | undefined>([]); |
| 85 | + mockHeros = new BehaviorSubject<HeroSection[] | undefined>([]); |
| 86 | + const sectionTypes = new SectionTypeService(); |
| 87 | + |
| 88 | + setServices({ |
| 89 | + ...services, |
| 90 | + homeConfig: { |
| 91 | + disableWelcomeScreen: false, |
| 92 | + disableNewThemeModal: true, |
| 93 | + }, |
| 94 | + sectionTypes, |
| 95 | + }); |
| 96 | + |
| 97 | + await act(async () => { |
| 98 | + const component = mountWithIntl( |
| 99 | + <OpenSearchDashboardsContextProvider services={services}> |
| 100 | + <Homepage /> |
| 101 | + </OpenSearchDashboardsContextProvider> |
| 102 | + ); |
| 103 | + expect(component.find('[data-test-subj="welcome"]')).toBeTruthy(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('renders home page content sections', async () => { |
| 108 | + const sectionTypes = new SectionTypeService(); |
| 109 | + const sectionMock = { |
| 110 | + id: 'id', |
| 111 | + title: 'title', |
| 112 | + render: jest.fn(), |
| 113 | + }; |
| 114 | + const heroMock = { |
| 115 | + id: 'id', |
| 116 | + render: jest.fn(), |
| 117 | + }; |
| 118 | + sectionTypes.getHomepage().sections$ = new BehaviorSubject<Section[] | undefined>([ |
| 119 | + sectionMock, |
| 120 | + ]); |
| 121 | + sectionTypes.getHomepage().heroes$ = new BehaviorSubject<HeroSection[] | undefined>([heroMock]); |
| 122 | + setServices({ |
| 123 | + ...services, |
| 124 | + homeConfig: { |
| 125 | + disableWelcomeScreen: true, |
| 126 | + disableNewThemeModal: true, |
| 127 | + }, |
| 128 | + sectionTypes, |
| 129 | + }); |
| 130 | + await act(async () => { |
| 131 | + const component = mountWithIntl( |
| 132 | + <OpenSearchDashboardsContextProvider services={services}> |
| 133 | + <Homepage /> |
| 134 | + </OpenSearchDashboardsContextProvider> |
| 135 | + ); |
| 136 | + expect(component.find('[data-test-subj="loading"]')).toBeTruthy(); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments