From 35c621031b4cd25777ac19a2789eac08ffd48176 Mon Sep 17 00:00:00 2001 From: dobri1408 Date: Thu, 22 Aug 2024 13:39:00 +0300 Subject: [PATCH] double check preview --- src/Toolbar/Sources.test.jsx | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Toolbar/Sources.test.jsx diff --git a/src/Toolbar/Sources.test.jsx b/src/Toolbar/Sources.test.jsx new file mode 100644 index 0000000..a8794b6 --- /dev/null +++ b/src/Toolbar/Sources.test.jsx @@ -0,0 +1,72 @@ +import React from 'react'; +import { render, fireEvent, screen } from '@testing-library/react'; +import Sources from './Sources'; +import '@testing-library/jest-dom/extend-expect'; + +const mockSources = [ + { + chart_source: 'Source 1', + chart_source_link: 'http://example.com/source1', + organisation: 'Organisation 1', + title: 'Title 1', + link: 'http://example.com/link1', + }, + { + chart_source: 'Source 2', + chart_source_link: 'http://example.com/source2', + organisation: 'Organisation 2', + title: 'Title 2', + link: 'http://example.com/link2', + }, +]; + +describe('Sources', () => { + it('renders the Sources button', () => { + render(); + expect(screen.getByText('Sources')).toBeInTheDocument(); + }); + + it('opens and closes the popup on click', () => { + render(); + + const triggerButton = screen.getByText('Sources'); + + expect(screen.queryByText('Source 1')).toBeInTheDocument(); + + fireEvent.click(triggerButton); + expect(screen.getByText('Source 1')).toBeInTheDocument(); + expect(screen.getByText('Source 2')).toBeInTheDocument(); + + fireEvent.click(triggerButton); + }); + + it('renders sources correctly', () => { + render(); + + const triggerButton = screen.getByText('Sources'); + fireEvent.click(triggerButton); + + expect(screen.getByText('Source 1')).toBeInTheDocument(); + expect(screen.getByText('Source 2')).toBeInTheDocument(); + + expect(screen.getByText('Source 1').closest('a')).toHaveAttribute( + 'href', + 'http://example.com/source1', + ); + expect(screen.getByText('Source 2').closest('a')).toHaveAttribute( + 'href', + 'http://example.com/source2', + ); + }); + + it('renders a message when there are no sources', () => { + render(); + + const triggerButton = screen.getByText('Sources'); + fireEvent.click(triggerButton); + + expect( + screen.getByText('Data provenance is not set for this visualization.'), + ).toBeInTheDocument(); + }); +});