-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(<Sources sources={mockSources} />); | ||
expect(screen.getByText('Sources')).toBeInTheDocument(); | ||
}); | ||
|
||
it('opens and closes the popup on click', () => { | ||
render(<Sources sources={mockSources} />); | ||
|
||
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(<Sources sources={mockSources} />); | ||
|
||
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(<Sources sources={[]} />); | ||
|
||
const triggerButton = screen.getByText('Sources'); | ||
fireEvent.click(triggerButton); | ||
|
||
expect( | ||
screen.getByText('Data provenance is not set for this visualization.'), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |