Skip to content

Commit

Permalink
Info.test.tsx and App.test.tsx mainly commented out
Browse files Browse the repository at this point in the history
Info.test.tsx and App.test.tsx mainly commented out, because the UI has changed and those test-programs need to be rewrote, before further testing.
  • Loading branch information
tihyyti committed Aug 28, 2024
1 parent 8ec052c commit 5b74219
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 143 deletions.
190 changes: 57 additions & 133 deletions client/tests/internal/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import { render, screen, fireEvent, waitFor } from '@testing-library/react';
//import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import App from '@/App';
import '@testing-library/jest-dom';
import { expect, test, vi, describe, beforeEach } from 'vitest';
//import { toast } from 'sonner';
//import { toast } from 'sonner'; (legacy UI-test)
import { MockEventSource } from './setupTests';
import { BrowserRouter as Router } from 'react-router-dom';
import { TooltipProvider } from '@radix-ui/react-tooltip';
Expand Down Expand Up @@ -39,25 +40,22 @@ describe('App component', () => {
expect(loginView).not.toBeInTheDocument();
});

waitFor(
/* waitFor(
async () => {
expect(screen.getByText(/RSS feeds/i)).toBeInTheDocument();
},
{ timeout: 3000 }
);
); */
});
/*

test('renders app component', () => {
expect(screen.getByText(/News Article Collector/i)).toBeInTheDocument();
//expect(screen.getByText(/Add or delete feeds/i)).toBeInTheDocument();
});
/*
test('submits RSS feed URLs', async () => {
const toastSuccessSpy = vi.spyOn(toast, 'success');
// const input = screen.getByText('Input RSS feed address here...');
// fireEvent.change(input, { target: { value: 'https://blabla.com/feed' } });
const addToListButton = screen.getByText(/Add to list/i);
fireEvent.click(addToListButton);
Expand All @@ -69,44 +67,39 @@ describe('App component', () => {
expect(toastSuccessSpy).toHaveBeenCalledWith(
'Feed list updated successfully!'
);
});
});
*/
/*
});*/

test('removes RSS feed URLs', async () => {
// Simulate adding a feed URL
render(
<Router>
<TooltipProvider>
<App />
</TooltipProvider>
</Router>
);
const input = screen.getByText('RSS feed address here...');
fireEvent.change(input, { target: { value: 'https://blabla.com/feed' } });
const addToListButton = screen.getByText(/Add to list/i);
fireEvent.click(addToListButton);
await waitFor(() => {
// render(
// <Router>
// <TooltipProvider>
// <App />
// </TooltipProvider>
// </Router>
// );

// const input = screen.getByText('RSS feed address here...');
// fireEvent.change(input, { target: { value: 'https://blabla.com/feed' } });
// This test is testing the legacy UI remove-button, needs to update
/* waitFor(() => {
expect(screen.getByText('https://blabla.com/feed')).toBeInTheDocument();
});
// Simulate removing the feed URL
const removeButton = screen.getByText(/Remove/i);
fireEvent.click(removeButton);
await waitFor(() => {
waitFor(() => {
expect(screen.queryByText('https://blabla.com/feed')).not.toBeInTheDocument();
});
// Check if the success message is displayed
await waitFor(() => {
waitFor(() => {
expect(screen.getByText('Feed list updated successfully!')).toBeInTheDocument();
});
});
*/
}); */

});

// note the use of userEvent which solved issues with dropdown menu
Expand Down Expand Up @@ -135,119 +128,51 @@ describe('App component', () => {
testDownloadOption('JSON');
});

test('clicks CSV download option', async () => {
testDownloadOption('CSV');
});
// test('clicks CSV download option', async () => {
// testDownloadOption('CSV');
// });

test('clicks Parquet download option', async () => {
testDownloadOption('Parquet');
});
// test('clicks Parquet download option', async () => {
// testDownloadOption('Parquet');
// });

// Handles a download error
test('handles download error', async () => {
const errorMessage = 'Failed to download the file.';

// Simulates a download error
global.fetch = vi.fn(() =>
Promise.reject(new Error('Failed to fetch'))
);

const serverUrl = 'http://localhost:4000';
try {
const response = await fetch(`${serverUrl}/api/get_feed_urls`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
//toast.error(errorMessage);
}

// Check if the error message is displayed in the DOM
await (() => {
expect(screen.getByText(errorMessage)).toBeInTheDocument();
});
});

/*
// Download error handling
test('handles download error', async () => {
const error = 'Failed to download the file.'
const toastErrorSpy = vi.spyOn(toast, 'message');
// Simulate a download error
global.fetch = vi.fn(() =>
Promise.reject(new Error('Failed to fetch'))
);
const serverUrl = 'http://localhost:4000';
try {
const response = await fetch(`${serverUrl}/api/get_feed_urls`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
toast.error('Failed to download the file.');
}
expect(toastErrorSpy).toHaveBeenCalledWith('Failed to download the file.');
});
*/

// Download error handling
/* test('handles download error', async () => {
const errorMessage = 'Failed to download the file.';
const toastErrorSpy = vi.spyOn(toast, 'error');
//test('handles download error', async () => {
/* //const errorMessage = 'Failed to download the file.';
// Simulate a download error
global.fetch = vi.fn(() =>
Promise.reject(new Error('Failed to fetch'))
);
const serverUrl = 'http://localhost:4000';
try {
const response = await fetch(`${serverUrl}/api/get_feed_urls`);
// Simulates a download error
global.fetch = vi.fn(() =>
Promise.reject(new Error('Failed to fetch'))
);
//const serverUrl = 'http://localhost:4000';
const response = await fetch(`http://localhost:4000/api/get_feed_urls`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
toast.error(errorMessage);
}
expect(toastErrorSpy).toHaveBeenCalledWith(errorMessage);
}); */

// handler at setup is only using generalQuery for now
/* test('handles download error', async () => {
const toastErrorSpy = vi.spyOn(toast, 'error');
vi.spyOn(global, 'fetch').mockImplementation(() =>
Promise.reject(new Error('Download failed'))
);
const downloadButton = screen.getByRole('button', { name: /^JSON$/i });
fireEvent.click(downloadButton);
await waitFor(() => {
expect(toastErrorSpy).toHaveBeenCalledWith('Download failed');
});
});
*/
// Check if the error message is displayed in the DOM
//expect(screen.getByText(errorMessage)).toBeInTheDocument();
} });*/

// Test with a mocked server:
test('searches articles based on query', async () => {
/* test('searches articles based on query', async () => {
const user = userEvent.setup();

waitFor(() => {
const searchLink = screen.getByRole('link', { name: /^Search$/ });
await user.click(searchLink);

expect(searchLink).toBeInTheDocument();
user.click(searchLink);
}); */
/*
// seems to be based on legacy UI:
waitFor(() => {
expect(screen.getByText(/Search articles/i)).toBeInTheDocument();
}, { timeout: 2000 });

const searchInput = screen.getByText('Insert query...');
const searchButton = screen.getByRole('button', { name: /Search/i });
const searchInput = await screen.findByText(undefined);
const searchButton = await screen.queryByRole('button', { name: /Search/i });
await user.type(searchInput, 'Full text 1');
//await user.click(searchButton);('link', { name: /^Search$/ });
await fireEvent.click(searchButton);
waitFor(() => {
Expand All @@ -259,12 +184,10 @@ describe('App component', () => {
expect(table).toBeInTheDocument();
const rows = screen.getAllByRole('row');
expect(rows.length).toBe(2);
});
expect(rows.length).toBe(2);*/
//});

test('handles no articles found during search', async () => {
//never used
//const searchLink = await screen.findByText
/* test('handles no articles found during search', async () => {
waitFor(() => {
expect(screen.getByText(/Search articles/i)).toBeInTheDocument();
Expand All @@ -285,4 +208,5 @@ describe('App component', () => {
},
{ timeout: 5000 }
);
});
}); */
})
26 changes: 16 additions & 10 deletions client/tests/internal/Info.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
//import { render, screen, fireEvent, cleanup } from '@testing-library/react';
import { render, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom';
import Info from '@/features/info/Info';
import { expect, test, afterEach } from 'vitest';
//import { expect, test, afterEach } from 'vitest';
import { test, afterEach } from 'vitest';

afterEach(() => {
cleanup();
Expand All @@ -10,7 +12,9 @@ afterEach(() => {
test('renders QuestionsAccordion component and toggles content', () => {
render(<Info/>);

const heading = screen.getByText(/How does the collector work?/i);
// these Info-texts are removed from the current Info-page:

/* const heading = screen.getByText(/How does the collector work?/i);
expect(heading).toBeInTheDocument();
let content = screen.queryByText(/The news feed article collector automatically gathers articles/i);
Expand All @@ -19,14 +23,16 @@ test('renders QuestionsAccordion component and toggles content', () => {
fireEvent.click(heading);
content = screen.getByText(/The news feed article collector automatically gathers articles/i);
expect(content).toBeInTheDocument();
expect(content).toBeInTheDocument(); */
});

// Test for different accordion items.
test('renders another accordion item and toggles content', () => {
render(<Info/>);
render(<Info />);

// these Info-texts are removed from the current Info-page:

const heading = screen.getByText(/How to add a new feed?/i);
/* const heading = screen.getByText(/How to add a new feed?/i);
//expect(heading).toBeInTheDocument();
let content = screen.queryByText(/To add a new feed, click on the "Add Feed" button/i);
Expand All @@ -35,14 +41,14 @@ test('renders another accordion item and toggles content', () => {
fireEvent.click(heading);
content = screen.getByText(/To add a new feed, click on the "Add Feed" button/i);
expect(content).toBeInTheDocument();
expect(content).toBeInTheDocument(); */
});

// Test for empty content.
test('renders accordion item with empty content', () => {
render(<Info/>);
render(<Info />);

const heading = screen.getByText(/Empty Question?/i);
/* const heading = screen.getByText(/Empty Question?/i);
//expect(heading).toBeInTheDocument();
let content = screen.queryByText(/This is an empty answer/i);
Expand All @@ -51,5 +57,5 @@ test('renders accordion item with empty content', () => {
fireEvent.click(heading);
content = screen.getByText(/This is an empty answer/i);
expect(content).toBeInTheDocument();
expect(content).toBeInTheDocument(); */
});

0 comments on commit 5b74219

Please sign in to comment.