Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for WebScrape.tsx #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/components/UIUC-Components/WebScrape.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import nock from 'nock';
import WebScrape from './WebScrape';

describe('WebScrape component', () => {
test('renders without crashing', () => {
const { getByPlaceholderText } = render(<WebScrape />);
expect(getByPlaceholderText('Enter URL')).toBeInTheDocument();
});

test('validateUrl function works correctly', () => {
const { getByPlaceholderText } = render(<WebScrape />);
const input = getByPlaceholderText('Enter URL');
fireEvent.change(input, { target: { value: 'https://validurl.com' } });
expect(input.value).toBe('https://validurl.com');
fireEvent.change(input, { target: { value: 'invalidurl' } });
expect(input.value).toBe('invalidurl');
});

test('handleSubmit function works correctly', async () => {
const { getByPlaceholderText, getByText } = render(<WebScrape />);
const input = getByPlaceholderText('Enter URL');
fireEvent.change(input, { target: { value: 'https://validurl.com' } });
const button = getByText('Ingest');
fireEvent.click(button);
await new Promise((r) => setTimeout(r, 2000));
expect(getByText('Web scraping started')).toBeInTheDocument();
});

test('scrapeWeb function works correctly', async () => {
nock('https://flask-production-751b.up.railway.app')
.get('/web-scrape')
.reply(200, { data: 'scraped data' });
const { getByPlaceholderText, getByText } = render(<WebScrape />);
const input = getByPlaceholderText('Enter URL');
fireEvent.change(input, { target: { value: 'https://validurl.com' } });
const button = getByText('Ingest');
fireEvent.click(button);
await new Promise((r) => setTimeout(r, 2000));
expect(getByText('scraped data')).toBeInTheDocument();
});

test('downloadMITCourse function works correctly', async () => {
nock('https://flask-production-751b.up.railway.app')
.get('/mit-download')
.reply(200, { data: 'downloaded data' });
const { getByPlaceholderText, getByText } = render(<WebScrape />);
const input = getByPlaceholderText('Enter URL');
fireEvent.change(input, { target: { value: 'https://ocw.mit.edu' } });
const button = getByText('Ingest');
fireEvent.click(button);
await new Promise((r) => setTimeout(r, 2000));
expect(getByText('downloaded data')).toBeInTheDocument();
});
});
18 changes: 15 additions & 3 deletions src/components/UIUC-Components/WebScrape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const shouldShowFields = (inputUrl: string) => {
)
}

const validateUrl = (url: string) => {
export const validateUrl = (url: string) => {
const courseraRegex = /^https?:\/\/(www\.)?coursera\.org\/learn\/.+/
const mitRegex = /^https?:\/\/ocw\.mit\.edu\/.+/
const githubRegex = /^https?:\/\/(www\.)?github\.com\/.+/
Expand All @@ -51,7 +51,7 @@ const validateUrl = (url: string) => {
)
}

const formatUrl = (url: string) => {
export const formatUrl = (url: string) => {
if (!/^https?:\/\//i.test(url)) {
url = 'http://' + url
}
Expand Down Expand Up @@ -117,7 +117,19 @@ export const WebScrape = ({
}
}

const handleSubmit = async () => {
export const handleSubmit = async (
url: string,
courseName: string,
courseOptions: {
maxUrls: string,
maxDepth: string,
stayOnBaseUrl: boolean,
selectedCanvasOptions: string[],
},
isNewCourse: boolean,
user: { email: string },
router: any,
) => {
if (validateUrl(url)) {
setLoadingSpinner(true)
let data = null
Expand Down
Loading