From 4bfbd9f71f337953d5b18722f91135419948786e Mon Sep 17 00:00:00 2001 From: Beverly Nguyen Date: Wed, 7 Aug 2024 15:27:43 -0700 Subject: [PATCH] fixing console errors --- app/assets/javascripts/totalMessagesChart.js | 4 ++-- tests/javascripts/totalMessagesChart.test.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/totalMessagesChart.js b/app/assets/javascripts/totalMessagesChart.js index ff409c969..f437cfc4c 100644 --- a/app/assets/javascripts/totalMessagesChart.js +++ b/app/assets/javascripts/totalMessagesChart.js @@ -25,10 +25,10 @@ // Ensure the width is set correctly if (width === 0) { - console.error('Chart container width is 0, cannot set SVG width.'); - return; + throw new Error('Chart container width is 0, cannot set SVG width.'); } + svg.attr("width", width).attr("height", height); var x = d3.scaleLinear() diff --git a/tests/javascripts/totalMessagesChart.test.js b/tests/javascripts/totalMessagesChart.test.js index 6f74d0630..e4a3708c5 100644 --- a/tests/javascripts/totalMessagesChart.test.js +++ b/tests/javascripts/totalMessagesChart.test.js @@ -182,14 +182,14 @@ test('Handles zero width chart container', () => { // Set chart container width to 0 Object.defineProperty(document.getElementById('totalMessageChartContainer'), 'clientWidth', { value: 0 }); + try { // 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(); - }); + } catch (error) { + // Check if the error message is as expected + expect(error.message).toBe('Chart container width is 0, cannot set SVG width.'); + } +}); test('Creates chart on DOMContentLoaded', () => { const createTotalMessagesChartSpy = jest.spyOn(window, 'createTotalMessagesChart');