From f3c8fc35ad25f571fd10880518b4abf0276e1aba Mon Sep 17 00:00:00 2001 From: Caleb Evans Date: Sat, 9 Sep 2023 14:57:00 -0700 Subject: [PATCH] Replace app if current dashboard's name is updated --- __tests__/DashboardManager.test.tsx | 28 ++++++++++++++++++++++++++++ components/app/DashboardManager.tsx | 7 +++++++ 2 files changed, 35 insertions(+) diff --git a/__tests__/DashboardManager.test.tsx b/__tests__/DashboardManager.test.tsx index 9c03e2f..fede82f 100644 --- a/__tests__/DashboardManager.test.tsx +++ b/__tests__/DashboardManager.test.tsx @@ -295,6 +295,34 @@ describe('Dashboard Manager', () => { }); }); + it('should update state of current dashboard if its own name is edited', async () => { + const localDashboard = secondDashboardJson; + const availableDashboards = [ + firstDashboardJson, + secondDashboardJson, + thirdDashboardJson + ]; + await openDashboardManager({ + localDashboard, + availableDashboards + }); + const newDashboardName = 'Spiritual Warfare Dashboard'; + mockPromptOnce(() => newDashboardName); + mockSupabaseUpsert('dashboards'); + mockSupabaseUpsert('widgets'); + await userEvent.click( + screen.getByRole('button', { + name: `Edit Name for Dashboard "${localDashboard.name}"` + }) + ); + await waitFor(() => { + expect(screen.getByText(newDashboardName)).toBeInTheDocument(); + expect(supabaseFromMocks.dashboards.upsert).toHaveBeenCalledTimes(1); + expect(supabaseFromMocks.widgets.upsert).toHaveBeenCalledTimes(0); + expect(getAppData().name).toEqual(newDashboardName); + }); + }); + it('should handle errors while editing dashboard name', async () => { const error = new Error('Could not edit dashboard name'); const availableDashboards = [ diff --git a/components/app/DashboardManager.tsx b/components/app/DashboardManager.tsx index b85a42f..0ad783e 100644 --- a/components/app/DashboardManager.tsx +++ b/components/app/DashboardManager.tsx @@ -78,6 +78,13 @@ const DashboardManager = ({ onClose }: Props) => { return; } setDashboards(updateDashboardInList(dashboards, dashboard)); + // Update persisted app state if the current dashboard's own name is edited + if (dashboard.id === app.id) { + dispatchToApp({ + type: 'replaceApp', + payload: dashboard + }); + } } async function deleteDashboard(dashboard: SyncedAppState) {