From c3f6d77fd9f890bda4804a04b7ef5ba9997ca2ed Mon Sep 17 00:00:00 2001 From: Jono Mingard Date: Mon, 12 Jun 2023 11:04:19 +1200 Subject: [PATCH] Added simple tests for design modal refs https://github.com/TryGhost/Team/issues/3354 --- .../src/admin-x-ds/global/Modal.tsx | 6 +- .../src/admin-x-ds/global/PreviewModal.tsx | 3 + .../src/admin-x-ds/global/TabView.tsx | 21 ++++-- .../components/providers/SettingsProvider.tsx | 12 ++-- .../components/settings/site/DesignModal.tsx | 9 +-- .../settings/site/DesignSetting.tsx | 1 + .../site/designAndBranding/ThemePreivew.tsx | 7 +- .../test/e2e/site/design.test.ts | 62 ++++++++++++++++ ghost/admin-x-settings/test/utils/e2e.ts | 72 +++++++++++++------ .../responses/custom_theme_settings.json | 45 ++++++++++++ 10 files changed, 202 insertions(+), 36 deletions(-) create mode 100644 ghost/admin-x-settings/test/e2e/site/design.test.ts create mode 100644 ghost/admin-x-settings/test/utils/responses/custom_theme_settings.json diff --git a/ghost/admin-x-settings/src/admin-x-ds/global/Modal.tsx b/ghost/admin-x-settings/src/admin-x-ds/global/Modal.tsx index db8f615c6a..adfedbfdd6 100644 --- a/ghost/admin-x-settings/src/admin-x-ds/global/Modal.tsx +++ b/ghost/admin-x-settings/src/admin-x-ds/global/Modal.tsx @@ -15,6 +15,7 @@ export interface ModalProps { */ size?: ModalSize; + testId?: string; title?: string; okLabel?: string; okColor?: string; @@ -32,6 +33,7 @@ export interface ModalProps { const Modal: React.FC = ({ size = 'md', + testId, title, okLabel = 'OK', cancelLabel = 'Cancel', @@ -179,7 +181,7 @@ const Modal: React.FC = ({ 'pointer-events-none fixed inset-0 z-0', backDrop && 'bg-[rgba(98,109,121,0.15)] backdrop-blur-[3px]' )}> -
+
{title && {title}} @@ -194,4 +196,4 @@ const Modal: React.FC = ({ ); }; -export default Modal; \ No newline at end of file +export default Modal; diff --git a/ghost/admin-x-settings/src/admin-x-ds/global/PreviewModal.tsx b/ghost/admin-x-settings/src/admin-x-ds/global/PreviewModal.tsx index 22207b1e89..dbd15d28c8 100644 --- a/ghost/admin-x-settings/src/admin-x-ds/global/PreviewModal.tsx +++ b/ghost/admin-x-settings/src/admin-x-ds/global/PreviewModal.tsx @@ -10,6 +10,7 @@ import {IButton} from './Button'; import {SelectOption} from './Select'; export interface PreviewModalProps { + testId?: string; title?: string; sidebar?: React.ReactNode; preview?: React.ReactNode; @@ -31,6 +32,7 @@ export interface PreviewModalProps { } export const PreviewModalContent: React.FC = ({ + testId, title, sidebar, preview, @@ -138,6 +140,7 @@ export const PreviewModalContent: React.FC = ({ customFooter={(<>)} noPadding={true} size='full' + testId={testId} title='' >
diff --git a/ghost/admin-x-settings/src/admin-x-ds/global/TabView.tsx b/ghost/admin-x-settings/src/admin-x-ds/global/TabView.tsx index 763d6e5911..4bbfad792c 100644 --- a/ghost/admin-x-settings/src/admin-x-ds/global/TabView.tsx +++ b/ghost/admin-x-settings/src/admin-x-ds/global/TabView.tsx @@ -1,4 +1,5 @@ import React, {useState} from 'react'; +import clsx from 'clsx'; export type Tab = { id: string, @@ -29,13 +30,25 @@ const TabView: React.FC = ({tabs, defaultSelected}) => { return (
-
+
{tabs.map(tab => ( - + ))}
{tabs.map(tab => ( -
+
{tab.contents}
))} @@ -43,4 +56,4 @@ const TabView: React.FC = ({tabs, defaultSelected}) => { ); }; -export default TabView; \ No newline at end of file +export default TabView; diff --git a/ghost/admin-x-settings/src/components/providers/SettingsProvider.tsx b/ghost/admin-x-settings/src/components/providers/SettingsProvider.tsx index 9b07f36e8d..a569d137c7 100644 --- a/ghost/admin-x-settings/src/components/providers/SettingsProvider.tsx +++ b/ghost/admin-x-settings/src/components/providers/SettingsProvider.tsx @@ -5,7 +5,7 @@ import {Setting, SiteData} from '../../types/api'; // Define the Settings Context interface SettingsContextProps { settings: Setting[] | null; - saveSettings: (updatedSettings: Setting[]) => Promise; + saveSettings: (updatedSettings: Setting[]) => Promise; siteData: SiteData | null; } @@ -16,7 +16,7 @@ interface SettingsProviderProps { const SettingsContext = createContext({ settings: null, siteData: null, - saveSettings: async () => {} + saveSettings: async () => [] }); function serialiseSettingsData(settings: Setting[]): Setting[] { @@ -100,16 +100,20 @@ const SettingsProvider: React.FC = ({children}) => { fetchSettings(); }, [api]); - const saveSettings = useCallback(async (updatedSettings: Setting[]): Promise => { + const saveSettings = useCallback(async (updatedSettings: Setting[]) => { try { // handle transformation for settings before save updatedSettings = deserializeSettings(updatedSettings); // Make an API call to save the updated settings const data = await api.settings.edit(updatedSettings); + const newSettings = serialiseSettingsData(data.settings); - setSettings(serialiseSettingsData(data.settings)); + setSettings(newSettings); + + return newSettings; } catch (error) { // Log error in settings API + return []; } }, [api]); diff --git a/ghost/admin-x-settings/src/components/settings/site/DesignModal.tsx b/ghost/admin-x-settings/src/components/settings/site/DesignModal.tsx index 28915ec5b8..c945931efa 100644 --- a/ghost/admin-x-settings/src/components/settings/site/DesignModal.tsx +++ b/ghost/admin-x-settings/src/components/settings/site/DesignModal.tsx @@ -60,7 +60,7 @@ const DesignModal: React.FC = () => { }); }, [api]); - const {settings} = useContext(SettingsContext); + const {settings, saveSettings} = useContext(SettingsContext); const { formState, @@ -80,8 +80,8 @@ const DesignModal: React.FC = () => { } if (formState.settings.some(setting => setting.dirty)) { - const response = await api.settings.edit(formState.settings); - updateForm(state => ({...state, settings: response.settings})); + const newSettings = await saveSettings(formState.settings.filter(setting => setting.dirty)); + updateForm(state => ({...state, settings: newSettings})); } } }); @@ -127,7 +127,7 @@ const DesignModal: React.FC = () => { return { updateThemeSetting={updateThemeSetting} />} sidebarPadding={false} + testId='design-modal' title='Design' onCancel={() => { if (saveState === 'unsaved') { diff --git a/ghost/admin-x-settings/src/components/settings/site/DesignSetting.tsx b/ghost/admin-x-settings/src/components/settings/site/DesignSetting.tsx index 4e13bbddb6..3e90507d43 100644 --- a/ghost/admin-x-settings/src/components/settings/site/DesignSetting.tsx +++ b/ghost/admin-x-settings/src/components/settings/site/DesignSetting.tsx @@ -14,6 +14,7 @@ const DesignSetting: React.FC = () => { customButtons={