From f65655e56c91aefa840b6a6bff5e4fb27a614aa7 Mon Sep 17 00:00:00 2001 From: Beverly Nguyen Date: Wed, 7 Aug 2024 15:42:46 -0700 Subject: [PATCH] fixing console errors --- app/assets/javascripts/totalMessagesChart.js | 3 ++- tests/javascripts/totalMessagesChart.test.js | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/totalMessagesChart.js b/app/assets/javascripts/totalMessagesChart.js index aec3e8ba3..ff409c969 100644 --- a/app/assets/javascripts/totalMessagesChart.js +++ b/app/assets/javascripts/totalMessagesChart.js @@ -25,7 +25,8 @@ // Ensure the width is set correctly if (width === 0) { - throw new Error('Chart container width is 0, cannot set SVG width.'); + console.error('Chart container width is 0, cannot set SVG width.'); + return; } svg.attr("width", width).attr("height", height); diff --git a/tests/javascripts/totalMessagesChart.test.js b/tests/javascripts/totalMessagesChart.test.js index e4a3708c5..bb2e770d5 100644 --- a/tests/javascripts/totalMessagesChart.test.js +++ b/tests/javascripts/totalMessagesChart.test.js @@ -179,16 +179,17 @@ test('Handles zero width chart container', () => { // Mock console.error const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); - // Set chart container width to 0 - Object.defineProperty(document.getElementById('totalMessageChartContainer'), 'clientWidth', { value: 0 }); + // Set chart container width to 0 + const chartContainer = document.getElementById('totalMessageChartContainer'); + Object.defineProperty(chartContainer, 'clientWidth', { value: 0, configurable: true }); - try { - // Call the function to create the chart - window.createTotalMessagesChart(); - } catch (error) { - // Check if the error message is as expected - expect(error.message).toBe('Chart container width is 0, cannot set SVG width.'); - } + // Call the function to create the chart + window.createTotalMessagesChart(); + + // Check if the console error was called + expect(consoleSpy).toHaveBeenCalledWith('Chart container width is 0, cannot set SVG width.'); + + consoleSpy.mockRestore(); }); test('Creates chart on DOMContentLoaded', () => {