From c631d2652bde8a8d7b79999cb001612af42bcb2f Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Wed, 11 Nov 2026 13:40:57 +0530 Subject: [PATCH] feat(admin): replace the onboarding modal with an inline site type picker --- assets/src/admin/onboarding/index.tsx | 9 -- assets/src/admin/onboarding/page.tsx | 150 ------------------ assets/src/admin/settings/page.tsx | 3 + assets/src/components/SiteTypeSettings.tsx | 136 ++++++++++++++++ assets/src/css/admin.scss | 23 --- assets/src/css/onboarding.scss | 40 ----- inc/Modules/Core/Assets.php | 4 - inc/Modules/Settings/Admin.php | 142 ++++------------- tests/e2e/settings/activation.spec.ts | 26 --- tests/js/OnboardingScreen.test.tsx | 107 ------------- tests/js/SettingsPage.test.tsx | 13 +- tests/js/SiteTypeSettings.test.tsx | 106 +++++++++++++ .../Modules/Settings/AdminTest.php | 87 +++++----- webpack.config.js | 8 - 14 files changed, 331 insertions(+), 523 deletions(-) delete mode 100644 assets/src/admin/onboarding/index.tsx delete mode 100644 assets/src/admin/onboarding/page.tsx create mode 100644 assets/src/components/SiteTypeSettings.tsx delete mode 100644 assets/src/css/onboarding.scss delete mode 100644 tests/js/OnboardingScreen.test.tsx create mode 100644 tests/js/SiteTypeSettings.test.tsx diff --git a/assets/src/admin/onboarding/index.tsx b/assets/src/admin/onboarding/index.tsx deleted file mode 100644 index 2e71b04..0000000 --- a/assets/src/admin/onboarding/index.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import { createRoot } from 'react-dom/client'; -import OnboardingScreen from './page'; - -// Render to the target element. -const target = document.getElementById( 'onelogs-site-selection-modal' ); -if ( target ) { - const root = createRoot( target ); - root.render( ); -} diff --git a/assets/src/admin/onboarding/page.tsx b/assets/src/admin/onboarding/page.tsx deleted file mode 100644 index 23371c5..0000000 --- a/assets/src/admin/onboarding/page.tsx +++ /dev/null @@ -1,150 +0,0 @@ -import { useState, useEffect } from 'react'; -import apiFetch from '@wordpress/api-fetch'; -import { __ } from '@wordpress/i18n'; -import { - Card, - CardHeader, - CardBody, - Notice, - Button, - SelectControl, -} from '@wordpress/components'; - -const BRAND_SITE = 'brand-site'; -const GOVERNING_SITE = 'governing-site'; - -export type SiteType = typeof BRAND_SITE | typeof GOVERNING_SITE; - -interface NoticeState { - type: 'success' | 'error' | 'warning' | 'info'; - message: string; -} - -// WordPress sets up apiFetch's nonce and root URL, so no middleware is needed here. -const setupUrl = window.OneLogsSettings?.setupUrl ?? ''; -const initialSiteType = window.OneLogsSettings?.siteType ?? ''; - -const SiteTypeSelector = ( { - value, - setSiteType, -}: { - value: SiteType | ''; - setSiteType: ( v: SiteType | '' ) => void; -} ) => ( - { - setSiteType( v ); - } } - __nextHasNoMarginBottom - __next40pxDefaultSize - options={ [ - { label: __( 'Select…', 'onelogs' ), value: '' }, - { label: __( 'Brand Site', 'onelogs' ), value: BRAND_SITE }, - { - label: __( 'Governing site', 'onelogs' ), - value: GOVERNING_SITE, - }, - ] } - /> -); - -const OnboardingScreen = () => { - const [ siteType, setSiteType ] = useState< SiteType | '' >( - initialSiteType || '' - ); - const [ notice, setNotice ] = useState< NoticeState | null >( null ); - const [ isSaving, setIsSaving ] = useState( false ); - - useEffect( () => { - apiFetch< { onelogs_site_type?: SiteType } >( { - path: '/wp/v2/settings', - } ) - .then( ( settings ) => { - if ( settings?.onelogs_site_type ) { - setSiteType( settings.onelogs_site_type ); - } - } ) - .catch( () => { - setNotice( { - type: 'error', - message: __( 'Error fetching site type.', 'onelogs' ), - } ); - } ); - }, [] ); - - const handleSiteTypeChange = async ( value: SiteType | '' ) => { - // Optimistically set site type. - setSiteType( value ); - setIsSaving( true ); - - try { - await apiFetch< { onelogs_site_type?: SiteType } >( { - path: '/wp/v2/settings', - method: 'POST', - data: { onelogs_site_type: value }, - } ).then( ( settings ) => { - if ( ! settings?.onelogs_site_type ) { - throw new Error( - __( 'No site type in response', 'onelogs' ) - ); - } - - setSiteType( settings.onelogs_site_type ); - - // Redirect user to setup page. - if ( setupUrl ) { - window.location.href = setupUrl; - } - } ); - } catch { - setNotice( { - type: 'error', - message: __( 'Error setting site type.', 'onelogs' ), - } ); - } finally { - setIsSaving( false ); - } - }; - - return ( - - { !! notice?.message && ( - setNotice( null ) } - > - { notice?.message } - - ) } - - -

{ __( 'OneLogs', 'onelogs' ) }

-
- - - - - -
- ); -}; - -export default OnboardingScreen; diff --git a/assets/src/admin/settings/page.tsx b/assets/src/admin/settings/page.tsx index d92b089..c0576bc 100644 --- a/assets/src/admin/settings/page.tsx +++ b/assets/src/admin/settings/page.tsx @@ -16,6 +16,7 @@ import apiFetch from '@wordpress/api-fetch'; import SiteTable from '@/components/SiteTable'; import SiteModal from '@/components/SiteModal'; import SiteSettings from '@/components/SiteSettings'; +import SiteTypeSettings from '@/components/SiteTypeSettings'; import { REST_NAMESPACE, SHARED_SITES, SITE_TYPE } from '@/js/constants'; import { getErrorMessage } from '@/js/utils'; @@ -221,6 +222,8 @@ const SettingsPage = () => { ) ) } + { ! SITE_TYPE && } + { SITE_TYPE === 'brand-site' && } { SITE_TYPE === 'governing-site' && ( diff --git a/assets/src/components/SiteTypeSettings.tsx b/assets/src/components/SiteTypeSettings.tsx new file mode 100644 index 0000000..1a610d2 --- /dev/null +++ b/assets/src/components/SiteTypeSettings.tsx @@ -0,0 +1,136 @@ +/** + * External dependencies + */ +import { useState } from 'react'; + +/** + * WordPress dependencies + */ +import apiFetch from '@wordpress/api-fetch'; +import { __ } from '@wordpress/i18n'; +import { + Card, + CardHeader, + CardBody, + Notice, + Button, + SelectControl, +} from '@wordpress/components'; + +/** + * Internal dependencies + */ +import type { SiteType } from '@/types/global'; + +const BRAND_SITE = 'brand-site' as const; +const GOVERNING_SITE = 'governing-site' as const; + +const SiteTypeSelector = ( { + value, + setSiteType, +}: { + value: SiteType; + setSiteType: ( v: SiteType ) => void; +} ) => ( + { + setSiteType( v ); + } } + __nextHasNoMarginBottom + __next40pxDefaultSize + options={ [ + { label: __( 'Select…', 'onelogs' ), value: '' }, + { label: __( 'Brand Site', 'onelogs' ), value: BRAND_SITE }, + { + label: __( 'Governing site', 'onelogs' ), + value: GOVERNING_SITE, + }, + ] } + /> +); + +const SiteTypeSettings = () => { + const [ siteType, setSiteType ] = useState< SiteType >( '' ); + const [ error, setError ] = useState( '' ); + const [ isSaving, setIsSaving ] = useState( false ); + + const handleSiteTypeChange = async ( value: SiteType ) => { + setIsSaving( true ); + + try { + const settings = await apiFetch< { + onelogs_site_type?: SiteType; + } >( { + path: '/wp/v2/settings', + method: 'POST', + data: { onelogs_site_type: value }, + } ); + + if ( ! settings?.onelogs_site_type ) { + throw new Error( 'No site type in response' ); + } + + // Reload so the settings screen and menu match the new site type. + window.location.reload(); + } catch { + setError( __( 'Error setting site type.', 'onelogs' ) ); + } finally { + setIsSaving( false ); + } + }; + + return ( + + { !! error && ( + setError( '' ) } + > + { error } + + ) } + + +

{ __( 'OneLogs', 'onelogs' ) }

+
+ + + +

+ { __( 'Governing site', 'onelogs' ) }:{ ' ' } + { __( + 'connects the brand sites and shows their activity logs.', + 'onelogs' + ) } +
+ { __( 'Brand site', 'onelogs' ) }:{ ' ' } + { __( + 'connects to a governing site, which can view its activity logs.', + 'onelogs' + ) } +

+ +
+
+ ); +}; + +export default SiteTypeSettings; diff --git a/assets/src/css/admin.scss b/assets/src/css/admin.scss index b2d8403..94eccb4 100644 --- a/assets/src/css/admin.scss +++ b/assets/src/css/admin.scss @@ -58,29 +58,6 @@ } -[id="onelogs-site-selection-modal"] { - position: fixed; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - z-index: 10000; - min-width: 40vw; -} - -body.onelogs-site-selection-modal { - - &::before { - content: ""; - position: fixed; - top: 0; - left: 0; - width: 100%; - height: 100%; - background-color: rgba(0, 0, 0, 0.5); - z-index: 9999; - } -} - [id="toplevel_page_onelogs"] { .dashicons-admin-generic::before { diff --git a/assets/src/css/onboarding.scss b/assets/src/css/onboarding.scss deleted file mode 100644 index 8a30c20..0000000 --- a/assets/src/css/onboarding.scss +++ /dev/null @@ -1,40 +0,0 @@ -// Styles specific to the plugin onboarding (Plugins page) - -.onelogs-onboarding-page { - - .components-card { - margin-bottom: 20px; - border: 1px solid #ddd; - border-radius: 8px; - box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); - } - - .components-button.is-busy { - cursor: not-allowed; - opacity: 0.7; - } -} - -// Onboarding modal container and backdrop -[id="onelogs-site-selection-modal"] { - position: fixed; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - z-index: 10000; - min-width: 40vw; -} - -body.onelogs-site-selection-modal { - - &::before { - content: ""; - position: fixed; - top: 0; - left: 0; - width: 100%; - height: 100%; - background-color: rgba(0, 0, 0, 0.5); - z-index: 9999; - } -} diff --git a/inc/Modules/Core/Assets.php b/inc/Modules/Core/Assets.php index 4279305..da9f5fc 100644 --- a/inc/Modules/Core/Assets.php +++ b/inc/Modules/Core/Assets.php @@ -33,7 +33,6 @@ final class Assets implements Registrable { */ public const ADMIN_STYLES_HANDLE = self::PREFIX . 'admin'; public const SETTINGS_SCRIPT_HANDLE = self::PREFIX . 'settings'; - public const ONBOARDING_SCRIPT_HANDLE = self::PREFIX . 'setup'; public const LOGS_DASHBOARD_SCRIPT_HANDLE = self::PREFIX . 'logs-dashboard'; /** @@ -122,9 +121,6 @@ public function register_assets(): void { $this->register_script( self::SETTINGS_SCRIPT_HANDLE, 'settings' ); $this->register_style( self::SETTINGS_SCRIPT_HANDLE, 'settings', [ 'wp-components' ] ); - $this->register_script( self::ONBOARDING_SCRIPT_HANDLE, 'onboarding' ); - $this->register_style( self::ONBOARDING_SCRIPT_HANDLE, 'onboarding', [ 'wp-components' ] ); - $this->register_script( self::LOGS_DASHBOARD_SCRIPT_HANDLE, 'logs-dashboard', diff --git a/inc/Modules/Settings/Admin.php b/inc/Modules/Settings/Admin.php index 80b6b78..866c638 100644 --- a/inc/Modules/Settings/Admin.php +++ b/inc/Modules/Settings/Admin.php @@ -5,7 +5,7 @@ * @package OneLogs\Modules\Settings */ -declare( strict_types = 1 ); +declare(strict_types = 1); namespace OneLogs\Modules\Settings; @@ -13,7 +13,7 @@ use OneLogs\Modules\Core\Assets; /** - * Class Admin + * Class - Admin */ final class Admin implements Registrable { /** @@ -44,10 +44,9 @@ public function register_hooks(): void { add_action( 'admin_menu', [ $this, 'add_submenu' ], 20 ); // 20 priority to make sure settings page respect its position. add_action( 'admin_menu', [ $this, 'remove_default_submenu' ], 999 ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); - add_action( 'admin_footer', [ $this, 'inject_site_selection_modal' ] ); + add_action( 'admin_notices', [ $this, 'render_notices' ] ); add_filter( 'plugin_action_links_' . ONELOGS_PLUGIN_BASENAME, [ $this, 'add_action_links' ], 2 ); - add_filter( 'admin_body_class', [ $this, 'add_body_classes' ] ); } /** @@ -105,34 +104,43 @@ public function screen_callback(): void { } /** - * Enqueue admin scripts. + * Enqueue the settings screen's scripts, only on that screen. * * @param string $hook Current admin page hook. */ public function enqueue_scripts( string $hook ): void { - // Enqueue the onboarding modal. - $this->enqueue_onboarding_scripts(); - - if ( strpos( $hook, 'onelogs-settings' ) !== false ) { - $this->enqueue_settings_scripts(); + // The hook is `{parent}_page_onelogs-settings`, where the parent part comes from the (translatable) menu title. + if ( ! str_ends_with( $hook, '_page_' . self::SCREEN_ID ) ) { + return; } - // @todo Move other scripts from Assets to here. + wp_localize_script( Assets::SETTINGS_SCRIPT_HANDLE, 'OneLogsSettings', Assets::get_localized_data() ); + wp_enqueue_script( Assets::SETTINGS_SCRIPT_HANDLE ); + wp_enqueue_style( Assets::ADMIN_STYLES_HANDLE ); } /** - * Inject site selection modal into the admin footer. + * Renders the setup admin notice. */ - public function inject_site_selection_modal(): void { - if ( ! $this->should_display_site_selection_modal() ) { + public function render_notices(): void { + if ( ! current_user_can( 'manage_options' ) ) { return; } - ?> -
-
-
- needs_setup() && in_array( get_current_screen()?->base, [ 'plugins', 'dashboard' ], true ) ) { + wp_admin_notice( + sprintf( + /* translators: %s: URL of the OneLogs settings page. */ + __( 'OneLogs needs to be set up. Choose whether this is a governing or a brand site to get started.', 'onelogs' ), + esc_url( admin_url( sprintf( 'admin.php?page=%s', self::SCREEN_ID ) ) ) + ), + [ + 'type' => 'info', + 'dismissible' => true, + ] + ); + } } /** @@ -160,101 +168,9 @@ public function add_action_links( $links ): array { } /** - * Add body classes for the admin area. - * - * @param string $classes Existing body classes. - */ - public function add_body_classes( $classes ): string { - $current_screen = get_current_screen(); - - if ( ! $current_screen ) { - return $classes; - } - - // Cast to string in case it's null. - $classes = $this->add_body_class_for_modal( (string) $classes ); - $classes = $this->add_body_class_for_missing_sites( (string) $classes ); - - return $classes; - } - - /** - * Enqueue the scripts and styles for the settings screen. + * Whether the site still has to choose its type. */ - public function enqueue_settings_scripts(): void { - wp_localize_script( - Assets::SETTINGS_SCRIPT_HANDLE, - 'OneLogsSettings', - array_merge( - Assets::get_localized_data() - ) - ); - - wp_enqueue_script( Assets::SETTINGS_SCRIPT_HANDLE ); - wp_enqueue_style( Assets::SETTINGS_SCRIPT_HANDLE ); - } - - /** - * Enqueue scripts and styles for the onboarding screen. - */ - private function enqueue_onboarding_scripts(): void { - if ( ! $this->should_display_site_selection_modal() ) { - return; - } - - wp_localize_script( - Assets::ONBOARDING_SCRIPT_HANDLE, - 'OneLogsSettings', - Assets::get_localized_data(), - ); - - wp_enqueue_script( Assets::ONBOARDING_SCRIPT_HANDLE ); - wp_enqueue_style( Assets::ONBOARDING_SCRIPT_HANDLE ); - } - - /** - * Add body class if the modal is going to be shown. - * - * @param string $classes Existing body classes. - */ - private function add_body_class_for_modal( string $classes ): string { - if ( ! $this->should_display_site_selection_modal() ) { - return $classes; - } - - // Add onelogs-site-selection-modal class to body. - $classes .= ' onelogs-site-selection-modal '; - - return $classes; - } - - /** - * Add body class for missing sites. - * - * @param string $classes Existing body classes. - */ - private function add_body_class_for_missing_sites( string $classes ): string { - // Bail if the shared sites are already set. - $shared_sites = Settings::get_shared_sites(); - if ( ! empty( $shared_sites ) ) { - return $classes; - } - - $classes .= ' onelogs-missing-brand-sites '; - - return $classes; - } - - /** - * Whether to display the site selection modal. - */ - private function should_display_site_selection_modal(): bool { - $current_screen = get_current_screen(); - if ( ! $current_screen || ( 'plugins' !== $current_screen->base && ! str_contains( $current_screen->id, self::MENU_SLUG ) ) ) { - return false; - } - - // Bail if the site type is already set. + private function needs_setup(): bool { return empty( Settings::get_site_type() ); } } diff --git a/tests/e2e/settings/activation.spec.ts b/tests/e2e/settings/activation.spec.ts index 8ffe92a..3c5760c 100644 --- a/tests/e2e/settings/activation.spec.ts +++ b/tests/e2e/settings/activation.spec.ts @@ -10,34 +10,11 @@ test.describe( 'plugin activation', () => { } ) => { await admin.visitAdminPage( '/plugins.php' ); - // Helper to dismiss the onboarding modal if present. - const dismissOnboardingModal = async () => { - const modal = page.locator( '#onelogs-site-selection-modal' ); - const backdrop = page.locator( - 'body.onelogs-site-selection-modal' - ); - - if ( await modal.isVisible() ) { - await modal.evaluate( ( el ) => { - el.remove(); - } ); - } - - if ( await backdrop.isVisible() ) { - await backdrop.evaluate( ( el ) => { - el.classList.remove( 'onelogs-site-selection-modal' ); - } ); - } - }; - const pluginRow = page.locator( 'tr[data-plugin="onelogs/onelogs.php"]' ); await expect( pluginRow ).toBeVisible(); - // Dismiss modal before interacting with plugin row. - await dismissOnboardingModal(); - const activateLink = pluginRow.locator( 'a', { hasText: 'Activate' } ); await Promise.all( [ @@ -49,9 +26,6 @@ test.describe( 'plugin activation', () => { pluginRow.locator( 'a', { hasText: 'Deactivate' } ) ).toBeVisible( { timeout: 10000 } ); - // Dismiss modal again after activation. - await dismissOnboardingModal(); - const deactivateLink = pluginRow.locator( 'a', { hasText: 'Deactivate', } ); diff --git a/tests/js/OnboardingScreen.test.tsx b/tests/js/OnboardingScreen.test.tsx deleted file mode 100644 index 761c676..0000000 --- a/tests/js/OnboardingScreen.test.tsx +++ /dev/null @@ -1,107 +0,0 @@ -/** - * External dependencies - */ -import { fireEvent, render, screen, waitFor } from '@testing-library/react'; -/** - * WordPress dependencies - */ -import apiFetch from '@wordpress/api-fetch'; - -import OnboardingScreen from '@/admin/onboarding/page'; -import type { OneLogsSettings } from '@/types/global'; - -const mockedApiFetch = apiFetch as jest.MockedFunction< typeof apiFetch >; - -describe( 'OnboardingScreen', () => { - let consoleWarnSpy: jest.SpyInstance; - let consoleErrorSpy: jest.SpyInstance; - - beforeEach( () => { - mockedApiFetch.mockReset(); - window.OneLogsSettings = { - ...( window.OneLogsSettings as OneLogsSettings ), - siteType: '', - }; - - // Suppress WordPress component deprecation warnings (outside our control) - consoleWarnSpy = jest - .spyOn( console, 'warn' ) - .mockImplementation( () => {} ); - consoleErrorSpy = jest - .spyOn( console, 'error' ) - .mockImplementation( () => {} ); - } ); - - afterEach( () => { - consoleWarnSpy.mockRestore(); - consoleErrorSpy.mockRestore(); - } ); - - it( 'loads the current site type from settings', async () => { - mockedApiFetch.mockResolvedValueOnce( { - onelogs_site_type: 'brand-site', - } ); - - render( ); - - await waitFor( () => { - expect( screen.getByLabelText( 'Site Type' ) ).toHaveValue( - 'brand-site' - ); - } ); - } ); - - it( 'shows an error notice when loading fails', async () => { - mockedApiFetch.mockRejectedValueOnce( new Error( 'load failed' ) ); - - render( ); - - // WordPress renders notices in both accessibility region and visible content - expect( - await screen.findAllByText( 'Error fetching site type.' ) - ).toHaveLength( 2 ); - } ); - - it( 'saves the chosen site type', async () => { - mockedApiFetch.mockResolvedValueOnce( {} ).mockResolvedValueOnce( { - onelogs_site_type: 'governing-site', - } ); - - render( ); - - fireEvent.change( await screen.findByLabelText( 'Site Type' ), { - target: { value: 'governing-site' }, - } ); - fireEvent.click( - screen.getByRole( 'button', { name: 'Select Current Site Type' } ) - ); - - await waitFor( () => { - expect( mockedApiFetch ).toHaveBeenLastCalledWith( { - path: '/wp/v2/settings', - method: 'POST', - data: { onelogs_site_type: 'governing-site' }, - } ); - } ); - } ); - - it( 'shows an error notice when saving fails', async () => { - mockedApiFetch - .mockResolvedValueOnce( {} ) - .mockRejectedValueOnce( new Error( 'save failed' ) ); - - render( ); - - fireEvent.change( await screen.findByLabelText( 'Site Type' ), { - target: { value: 'brand-site' }, - } ); - fireEvent.click( - screen.getByRole( 'button', { name: 'Select Current Site Type' } ) - ); - - // WordPress renders notices in both accessibility region and visible content - expect( - await screen.findAllByText( 'Error setting site type.' ) - ).toHaveLength( 2 ); - } ); -} ); diff --git a/tests/js/SettingsPage.test.tsx b/tests/js/SettingsPage.test.tsx index 479e8c4..5ea3a32 100644 --- a/tests/js/SettingsPage.test.tsx +++ b/tests/js/SettingsPage.test.tsx @@ -116,6 +116,11 @@ jest.mock( '@/components/SiteSettings', () => ( { default: () =>
Brand Site Settings
, } ) ); +jest.mock( '@/components/SiteTypeSettings', () => ( { + __esModule: true, + default: () =>
Site Type
, +} ) ); + import SettingsPage from '@/admin/settings/page'; const mockedApiFetch = apiFetch as jest.MockedFunction< typeof apiFetch >; @@ -166,11 +171,14 @@ describe( 'SettingsPage', () => { mockSubmitResults.length = 0; } ); - it( 'shows neither settings view until the site type is chosen in the setup modal', () => { + it( 'shows the site type picker until the site type is chosen (QA TC-011)', () => { setSettings( '' ); render( ); + expect( + screen.getByTestId( 'site-type-settings' ) + ).toBeInTheDocument(); expect( screen.queryByTestId( 'site-table' ) ).not.toBeInTheDocument(); expect( screen.queryByTestId( 'site-settings' ) @@ -183,6 +191,9 @@ describe( 'SettingsPage', () => { render( ); expect( screen.getByTestId( 'site-settings' ) ).toBeInTheDocument(); + expect( + screen.queryByTestId( 'site-type-settings' ) + ).not.toBeInTheDocument(); expect( screen.queryByTestId( 'site-table' ) ).not.toBeInTheDocument(); } ); diff --git a/tests/js/SiteTypeSettings.test.tsx b/tests/js/SiteTypeSettings.test.tsx new file mode 100644 index 0000000..018ce0c --- /dev/null +++ b/tests/js/SiteTypeSettings.test.tsx @@ -0,0 +1,106 @@ +/** + * External dependencies + */ +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; + +/** + * WordPress dependencies + */ +import apiFetch from '@wordpress/api-fetch'; + +/** + * Internal dependencies + */ +import SiteTypeSettings from '@/components/SiteTypeSettings'; + +const mockedApiFetch = apiFetch as jest.MockedFunction< typeof apiFetch >; + +const selectButton = () => + screen.getByRole( 'button', { name: 'Select Current Site Type' } ); + +describe( 'SiteTypeSettings', () => { + it( 'keeps the select button disabled until a site type is chosen (QA TC-012)', () => { + render( ); + + expect( selectButton() ).toBeDisabled(); + + fireEvent.change( screen.getByLabelText( 'Site Type' ), { + target: { value: 'brand-site' }, + } ); + + expect( selectButton() ).not.toBeDisabled(); + } ); + + it( 'says the site type cannot be changed later (QA TC-016)', () => { + render( ); + + expect( + screen.getByText( /This setting cannot be changed later/ ) + ).toBeInTheDocument(); + } ); + + it( 'saves the chosen site type through the core settings route and reloads', async () => { + mockedApiFetch.mockResolvedValueOnce( { + onelogs_site_type: 'governing-site', + } ); + + render( ); + + fireEvent.change( screen.getByLabelText( 'Site Type' ), { + target: { value: 'governing-site' }, + } ); + fireEvent.click( selectButton() ); + + await waitFor( () => { + expect( mockedApiFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/settings', + method: 'POST', + data: { onelogs_site_type: 'governing-site' }, + } ); + } ); + + // jsdom can't reload, so the reload shows up as its "not implemented" error. + await waitFor( () => { + expect( console ).toHaveErrored(); + } ); + expect( screen.queryByText( 'Error setting site type.' ) ).toBeNull(); + } ); + + it( 'shows an error notice when saving fails', async () => { + mockedApiFetch.mockRejectedValueOnce( new Error( 'save failed' ) ); + + render( ); + + fireEvent.change( screen.getByLabelText( 'Site Type' ), { + target: { value: 'brand-site' }, + } ); + fireEvent.click( selectButton() ); + + // WordPress renders notices in both accessibility region and visible content. + // The region keeps the previous test's message, so wait for the visible notice too. + await waitFor( () => { + expect( + screen.getAllByText( 'Error setting site type.' ) + ).toHaveLength( 2 ); + } ); + } ); + + it( 'shows an error notice when the response has no site type', async () => { + mockedApiFetch.mockResolvedValueOnce( {} ); + + render( ); + + fireEvent.change( screen.getByLabelText( 'Site Type' ), { + target: { value: 'brand-site' }, + } ); + fireEvent.click( selectButton() ); + + // WordPress renders notices in both accessibility region and visible content. + // The region keeps the previous test's message, so wait for the visible notice too. + await waitFor( () => { + expect( + screen.getAllByText( 'Error setting site type.' ) + ).toHaveLength( 2 ); + } ); + } ); +} ); diff --git a/tests/phpunit/Integration/Modules/Settings/AdminTest.php b/tests/phpunit/Integration/Modules/Settings/AdminTest.php index 3124ce0..43f34df 100644 --- a/tests/phpunit/Integration/Modules/Settings/AdminTest.php +++ b/tests/phpunit/Integration/Modules/Settings/AdminTest.php @@ -9,6 +9,7 @@ namespace OneLogs\Tests\Integration\Modules\Settings; +use OneLogs\Modules\Core\Assets; use OneLogs\Modules\Settings\Admin; use OneLogs\Modules\Settings\Settings; use OneLogs\Tests\TestCase; @@ -142,67 +143,69 @@ public function test_add_action_links_appends_settings_link(): void { } /** - * Ensures admin body classes are returned as a string. + * Ensures the settings script only loads on the settings screen. */ - public function test_add_body_classes_returns_classes_string(): void { - set_current_screen( 'plugins.php' ); + public function test_enqueue_scripts_only_loads_on_the_settings_screen(): void { + ( new Assets() )->register_assets(); $admin = new Admin(); - $classes = $admin->add_body_classes( '' ); + $admin->enqueue_scripts( 'plugins.php' ); + $this->assertFalse( wp_script_is( Assets::SETTINGS_SCRIPT_HANDLE, 'enqueued' ) ); - $this->assertIsString( $classes ); - $this->assertStringContainsString( 'onelogs-site-selection-modal', $classes ); - $this->assertStringContainsString( 'onelogs-missing-brand-sites', $classes ); + $admin->enqueue_scripts( 'toplevel_page_' . Admin::MENU_SLUG ); + $this->assertFalse( wp_script_is( Assets::SETTINGS_SCRIPT_HANDLE, 'enqueued' ) ); - // Test with already configured which should remove the missing-brand-sites class and keep the modal class. - update_option( Settings::OPTION_SITE_TYPE, Settings::SITE_TYPE_GOVERNING ); - update_option( - Settings::OPTION_GOVERNING_SHARED_SITES, - [ - [ - 'url' => 'https://example.com', - 'name' => 'Test', - 'api_key' => '', - ], - ] - ); - $classes = $admin->add_body_classes( '' ); - $this->assertIsString( $classes ); - $this->assertStringNotContainsString( 'onelogs-site-selection-modal', $classes ); - $this->assertStringNotContainsString( 'onelogs-missing-brand-sites', $classes ); + $admin->enqueue_scripts( Admin::MENU_SLUG . '_page_' . Admin::SCREEN_ID ); + $this->assertTrue( wp_script_is( Assets::SETTINGS_SCRIPT_HANDLE, 'enqueued' ) ); - // Test with a bad current screen which should return the original classes unmodified. - set_current_screen( 'not-a-real-screen' ); - $classes = $admin->add_body_classes( 'original-classes' ); - $this->assertSame( 'original-classes', $classes ); + wp_dequeue_script( Assets::SETTINGS_SCRIPT_HANDLE ); } /** - * Ensures the site-selection modal renders when onboarding is needed. + * Ensures the setup notice links to the settings page while the site type isn't chosen (QA TC-011). */ - public function test_inject_site_selection_modal_outputs_html_when_conditions_met(): void { - set_current_screen( 'plugins.php' ); + public function test_render_notices_shows_setup_notice_until_the_site_type_is_set(): void { + set_current_screen( 'plugins' ); - ob_start(); - ( new Admin() )->inject_site_selection_modal(); - $output = (string) ob_get_clean(); + $output = $this->render_notices(); + $this->assertStringContainsString( 'OneLogs needs to be set up.', $output ); + $this->assertStringContainsString( 'admin.php?page=' . Admin::SCREEN_ID, $output ); - $this->assertStringContainsString( 'onelogs-site-selection-modal', $output ); - $this->assertStringContainsString( 'onelogs-modal', $output ); + update_option( Settings::OPTION_SITE_TYPE, Settings::SITE_TYPE_GOVERNING ); + + $this->assertSame( '', $this->render_notices() ); } /** - * Ensures the site-selection modal does not render after setup. + * Ensures the setup notice is only shown on the plugins and dashboard screens. */ - public function test_inject_site_selection_modal_outputs_nothing_when_site_type_set(): void { - update_option( Settings::OPTION_SITE_TYPE, Settings::SITE_TYPE_GOVERNING ); - set_current_screen( 'plugins.php' ); + public function test_render_notices_only_shows_on_plugins_and_dashboard(): void { + set_current_screen( 'dashboard' ); + $this->assertStringContainsString( 'OneLogs needs to be set up.', $this->render_notices() ); + // The settings page shows the site type picker itself. + set_current_screen( Admin::MENU_SLUG . '_page_' . Admin::SCREEN_ID ); + $this->assertSame( '', $this->render_notices() ); + } + + /** + * Ensures users who can't set the site type don't see the setup notice. + */ + public function test_render_notices_requires_manage_options(): void { + wp_set_current_user( self::factory()->user->create( [ 'role' => 'editor' ] ) ); + set_current_screen( 'plugins' ); + + $this->assertSame( '', $this->render_notices() ); + } + + /** + * Renders the admin notices. + */ + private function render_notices(): string { ob_start(); - ( new Admin() )->inject_site_selection_modal(); - $output = (string) ob_get_clean(); + ( new Admin() )->render_notices(); - $this->assertSame( '', trim( $output ) ); + return trim( (string) ob_get_clean() ); } /** diff --git a/webpack.config.js b/webpack.config.js index 9f9da06..a7f4762 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -65,14 +65,6 @@ const scripts = { 'logs', 'index.ts' ), - onboarding: path.resolve( - process.cwd(), - 'assets', - 'src', - 'admin', - 'onboarding', - 'index.tsx' - ), settings: path.resolve( process.cwd(), 'assets',