const { createTable, handleDropdownChange, fetchData, createChart } = require('../../app/assets/javascripts/dashboardVisualization.js'); // Mock d3 to avoid errors related to it jest.mock('d3', () => { const selectAllMock = jest.fn().mockReturnValue({ remove: jest.fn(), }); const appendMock = jest.fn().mockReturnValue({ attr: jest.fn().mockReturnThis(), append: jest.fn().mockReturnThis(), style: jest.fn().mockReturnThis(), text: jest.fn(), }); const selectMock = jest.fn().mockReturnValue({ selectAll: selectAllMock, append: appendMock, attr: jest.fn().mockReturnThis(), style: jest.fn().mockReturnThis(), text: jest.fn(), }); const scaleBandMock = jest.fn().mockReturnValue({ domain: jest.fn().mockReturnThis(), range: jest.fn().mockReturnThis(), padding: jest.fn().mockReturnThis(), }); const scaleLinearMock = jest.fn().mockReturnValue({ domain: jest.fn().mockReturnThis(), nice: jest.fn().mockReturnThis(), range: jest.fn().mockReturnThis(), }); const axisMock = jest.fn().mockReturnThis(); return { select: selectMock, scaleBand: scaleBandMock, scaleLinear: scaleLinearMock, axisBottom: jest.fn(() => axisMock), axisLeft: jest.fn(() => axisMock), stack: jest.fn(() => jest.fn().mockReturnValue([])), format: jest.fn(() => jest.fn()), }; }); describe('Dashboard Visualization Module', () => { test('should have createTable function', () => { expect(typeof createTable).toBe('function'); }); test('should have handleDropdownChange function', () => { expect(typeof handleDropdownChange).toBe('function'); }); test('should have fetchData function', () => { expect(typeof fetchData).toBe('function'); }); }); describe('Table Creation', () => { beforeAll(() => { document.body.innerHTML = `
Weekly
`; }); test('creates a table with the correct data', () => { const labels = ['Day 1', 'Day 2', 'Day 3']; const deliveredData = [10, 20, 30]; const failedData = [1, 2, 3]; createTable('weeklyTable', 'Weekly', labels, deliveredData, failedData); const table = document.getElementById('weeklyTable'); console.log(table); expect(document.body.contains(table)).toBe(true); expect(table.querySelectorAll('tbody tr').length).toBe(labels.length); }); }); describe('Dropdown Change Handler', () => { beforeAll(() => { document.body.innerHTML = `
Service Name - Last 7 Days
`; // Mock Socket.IO global.io = jest.fn().mockReturnValue({ on: jest.fn(), emit: jest.fn(), }); }); test('updates subtitle and aria-live region correctly', () => { const dropdown = document.getElementById('options'); dropdown.value = 'individual'; handleDropdownChange({ target: dropdown }); const subTitle = document.querySelector('.chart-subtitle'); expect(subTitle.textContent).toBe('User Name - Last 7 Days'); const ariaLiveRegion = document.getElementById('aria-live-account'); expect(ariaLiveRegion.textContent).toBe('Data updated for User Name - Last 7 Days'); }); });