|
| 1 | +// eslint-disable-next-line import/order -- This is a test file and we need to import the mocks first |
| 2 | +import { mockStore } from '../../../utils/test-mocks'; |
| 3 | +import { useConnection } from '@automattic/jetpack-connection'; |
| 4 | +import { isJetpackSelfHostedSite, siteHasFeature } from '@automattic/jetpack-script-data'; |
| 5 | +import { render, screen } from '@testing-library/react'; |
| 6 | +import { SocialAdminPage } from '../'; |
| 7 | +import { getSocialScriptData } from '../../../utils'; |
| 8 | + |
| 9 | +// Mock child components to simplify testing - We only test the SocialAdminPage component here |
| 10 | +jest.mock( '../connection-screen', () => () => <div data-testid="connection-screen" /> ); |
| 11 | +jest.mock( '../header', () => () => <div data-testid="header" /> ); |
| 12 | +jest.mock( '../info-section', () => () => <div data-testid="info-section" /> ); |
| 13 | +jest.mock( '../page-header', () => () => <div data-testid="page-header" /> ); |
| 14 | +jest.mock( '../pricing-page', () => ( { onDismiss } ) => ( |
| 15 | + <button data-testid="pricing-page" onClick={ onDismiss } onKeyDown={ onDismiss } /> |
| 16 | +) ); |
| 17 | +jest.mock( '../support-section', () => () => <div data-testid="support-section" /> ); |
| 18 | +jest.mock( '../toggles/social-image-generator-toggle', () => () => ( |
| 19 | + <div data-testid="social-image-generator-toggle" /> |
| 20 | +) ); |
| 21 | +jest.mock( '../toggles/social-module-toggle', () => () => ( |
| 22 | + <div data-testid="social-module-toggle" /> |
| 23 | +) ); |
| 24 | +jest.mock( '../toggles/social-notes-toggle', () => () => ( |
| 25 | + <div data-testid="social-notes-toggle" /> |
| 26 | +) ); |
| 27 | +jest.mock( '../toggles/utm-toggle', () => () => <div data-testid="utm-toggle" /> ); |
| 28 | + |
| 29 | +describe( 'SocialAdminPage', () => { |
| 30 | + beforeEach( () => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + mockStore(); |
| 33 | + useConnection.mockReturnValue( { |
| 34 | + isUserConnected: true, |
| 35 | + isRegistered: true, |
| 36 | + } ); |
| 37 | + isJetpackSelfHostedSite.mockReturnValue( true ); |
| 38 | + siteHasFeature.mockReturnValue( true ); |
| 39 | + getSocialScriptData.mockReturnValue( { |
| 40 | + plugin_info: { |
| 41 | + social: { version: '1.0.0' }, |
| 42 | + jetpack: { version: '1.0.0' }, |
| 43 | + }, |
| 44 | + } ); |
| 45 | + } ); |
| 46 | + |
| 47 | + describe( 'Page rendering', () => { |
| 48 | + it( 'should render connection screen when not connected', () => { |
| 49 | + useConnection.mockReturnValue( { |
| 50 | + isUserConnected: false, |
| 51 | + isRegistered: false, |
| 52 | + } ); |
| 53 | + |
| 54 | + render( <SocialAdminPage /> ); |
| 55 | + expect( screen.getByTestId( 'connection-screen' ) ).toBeInTheDocument(); |
| 56 | + } ); |
| 57 | + |
| 58 | + it( 'should render main admin page when connected', () => { |
| 59 | + render( <SocialAdminPage /> ); |
| 60 | + |
| 61 | + expect( screen.getByTestId( 'page-header' ) ).toBeInTheDocument(); |
| 62 | + expect( screen.getByTestId( 'header' ) ).toBeInTheDocument(); |
| 63 | + expect( screen.getByTestId( 'info-section' ) ).toBeInTheDocument(); |
| 64 | + expect( screen.getByTestId( 'support-section' ) ).toBeInTheDocument(); |
| 65 | + } ); |
| 66 | + |
| 67 | + it( 'should render pricing page when showPricingPage is true and no paid features', () => { |
| 68 | + mockStore( { |
| 69 | + getSocialSettings: () => ( { showPricingPage: true } ), |
| 70 | + } ); |
| 71 | + |
| 72 | + render( <SocialAdminPage /> ); |
| 73 | + expect( screen.getByTestId( 'pricing-page' ) ).toBeInTheDocument(); |
| 74 | + } ); |
| 75 | + } ); |
| 76 | + |
| 77 | + describe( 'Toggle visibility', () => { |
| 78 | + describe( 'UTM toggle', () => { |
| 79 | + it( 'should show when module is enabled', () => { |
| 80 | + render( <SocialAdminPage /> ); |
| 81 | + expect( screen.getByTestId( 'utm-toggle' ) ).toBeInTheDocument(); |
| 82 | + } ); |
| 83 | + |
| 84 | + it( 'should not show when module is disabled', () => { |
| 85 | + mockStore( { |
| 86 | + getSocialModuleSettings: () => ( { publicize: false } ), |
| 87 | + } ); |
| 88 | + render( <SocialAdminPage /> ); |
| 89 | + expect( screen.queryByTestId( 'utm-toggle' ) ).not.toBeInTheDocument(); |
| 90 | + } ); |
| 91 | + } ); |
| 92 | + |
| 93 | + describe( 'Social Notes toggle', () => { |
| 94 | + it( 'should show when plugin is active and module is enabled', () => { |
| 95 | + render( <SocialAdminPage /> ); |
| 96 | + expect( screen.getByTestId( 'social-notes-toggle' ) ).toBeInTheDocument(); |
| 97 | + } ); |
| 98 | + |
| 99 | + it( 'should not show when plugin is not active', () => { |
| 100 | + getSocialScriptData.mockReturnValue( { |
| 101 | + plugin_info: { |
| 102 | + social: { version: null }, |
| 103 | + jetpack: { version: '1.0.0' }, |
| 104 | + }, |
| 105 | + } ); |
| 106 | + render( <SocialAdminPage /> ); |
| 107 | + expect( screen.queryByTestId( 'social-notes-toggle' ) ).not.toBeInTheDocument(); |
| 108 | + } ); |
| 109 | + |
| 110 | + it( 'should not show when module is disabled', () => { |
| 111 | + mockStore( { |
| 112 | + getSocialModuleSettings: () => ( { publicize: false } ), |
| 113 | + } ); |
| 114 | + render( <SocialAdminPage /> ); |
| 115 | + expect( screen.queryByTestId( 'social-notes-toggle' ) ).not.toBeInTheDocument(); |
| 116 | + } ); |
| 117 | + } ); |
| 118 | + |
| 119 | + describe( 'Social Image Generator toggle', () => { |
| 120 | + it( 'should show when feature is available and module is enabled', () => { |
| 121 | + render( <SocialAdminPage /> ); |
| 122 | + expect( screen.getByTestId( 'social-image-generator-toggle' ) ).toBeInTheDocument(); |
| 123 | + } ); |
| 124 | + |
| 125 | + it( 'should not show when feature is not available', () => { |
| 126 | + siteHasFeature.mockReturnValue( false ); |
| 127 | + render( <SocialAdminPage /> ); |
| 128 | + expect( screen.queryByTestId( 'social-image-generator-toggle' ) ).not.toBeInTheDocument(); |
| 129 | + } ); |
| 130 | + |
| 131 | + it( 'should not show when module is disabled', () => { |
| 132 | + mockStore( { |
| 133 | + getSocialModuleSettings: () => ( { publicize: false } ), |
| 134 | + } ); |
| 135 | + render( <SocialAdminPage /> ); |
| 136 | + expect( screen.queryByTestId( 'social-image-generator-toggle' ) ).not.toBeInTheDocument(); |
| 137 | + } ); |
| 138 | + } ); |
| 139 | + } ); |
| 140 | +} ); |
0 commit comments