-
Notifications
You must be signed in to change notification settings - Fork 0
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
BI-46: Fetch University Data from MongoDB #22
Changes from all commits
e58b6ec
7987311
36cf4e0
6aa2e65
f118309
6f391ae
7e366c8
58db507
dcb4c1c
b4d2a70
e3c349b
11672f4
0914a02
4d6afdd
023d0ca
d4ad1dd
82f3a81
49e04db
5c65285
ca2c3c0
29db724
2232ebe
134c694
b32c347
ca19aa1
55a9e20
b1e7175
5bf66fa
b56362d
2e35b7e
0e955e0
ab6e00a
5ae6ff6
aafb764
e870cb0
13439dc
22edb7d
45870d4
ec3b57b
b3bd827
958af65
d77d523
0ae929f
0c7e04f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {} | ||
require('dotenv').config(); | ||
const nextConfig = { | ||
async headers() { | ||
return [ | ||
{ | ||
// matching all API routes | ||
source: "/api/:path*", | ||
headers: [ | ||
{ key: "Access-Control-Allow-Credentials", value: "true" }, | ||
{ key: "Access-Control-Allow-Origin", value: "*" }, | ||
{ key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" }, | ||
{ key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }, | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
module.exports = nextConfig |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' | ||
|
||
import fetch from 'cross-fetch' | ||
import { gql } from '@apollo/client' | ||
|
||
const query = gql` | ||
query getUniversityList { | ||
universityList { | ||
id | ||
name | ||
score | ||
country | ||
city | ||
} | ||
} | ||
` | ||
|
||
let uri = 'https://web-app-client-b69l4yjrq-bacpacs-projects.vercel.app/api/graphql' | ||
console.log('GraphQL URI in tests:', uri) | ||
const client = new ApolloClient({ | ||
link: new HttpLink({ uri, fetch }), | ||
cache: new InMemoryCache(), | ||
}) | ||
// test to check the data is not empty | ||
test('the data is of college', async () => { | ||
const result = await client.query({ query }) | ||
|
||
expect(result?.data).not.toBeNull() | ||
expect(result?.data).toHaveProperty('universityList') | ||
expect(result?.data?.universityList).toBeInstanceOf(Array) | ||
expect(result?.data?.universityList?.length).toBeGreaterThan(0) | ||
}) | ||
// test to check the format of data recived by the query | ||
test('the data has specific properties', async () => { | ||
const result = await client.query({ query }) | ||
|
||
result?.data?.universityList?.forEach((college) => { | ||
// check for id property | ||
expect(college).toHaveProperty('id') | ||
expect(college?.id).not.toBeNull() | ||
expect(typeof college?.id).toBe('string') | ||
// check for name property | ||
expect(college).toHaveProperty('name') | ||
expect(college?.name).not.toBeNull() | ||
expect(typeof college?.name).toBe('string') | ||
// check for score property | ||
expect(college).toHaveProperty('score') | ||
expect(college?.score).not.toBeNull() | ||
expect(typeof college?.score).toBe('string') | ||
// check for country property | ||
expect(college).toHaveProperty('country') | ||
expect(college?.country).not.toBeNull() | ||
expect(typeof college?.country).toBe('string') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import searchAlgorithm from '../utils/searchAlgorithm' | ||
describe('searchAlgorithm', () => { | ||
const colleges = [{ name: 'Massachusetts Institute of Technology (MIT)', country: 'United States', city: 'Cambridge' }] | ||
it('returns filtered results when input and data are provided', () => { | ||
const input = 'mass' | ||
|
||
const result = searchAlgorithm(input, colleges) | ||
|
||
expect(result).toEqual([{ name: 'Massachusetts Institute of Technology (MIT)', country: 'United States', city: 'Cambridge' }]) | ||
}) | ||
it('returns an empty array when either input is not provided', () => { | ||
const input = '' | ||
|
||
const result = searchAlgorithm(input, colleges) | ||
|
||
expect(result).toEqual([]) | ||
}) | ||
it('returns an empty array when either input doesnot match with the college name,city or country', () => { | ||
const input = 'xyw' | ||
|
||
const result = searchAlgorithm(input, colleges) | ||
|
||
expect(result).toEqual([]) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Link from 'next/link' | ||
|
||
function CollegeResult(props) { | ||
return ( | ||
<div className="mb-3.5"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' | ||
|
||
import fetch from 'cross-fetch' | ||
|
||
let uri = '/api/graphql' | ||
console.log('GraphQL URI in Client:', uri) | ||
const client = new ApolloClient({ | ||
link: new HttpLink({ uri, fetch }), | ||
cache: new InMemoryCache(), | ||
}) | ||
|
||
export default client |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { useState } from 'react' | ||
import { AiOutlineSearch } from 'react-icons/ai' | ||
import CollegeResult from '../app/components/CollegeResult' | ||
import searchAlgorithm from '@/utils/searchAlgorithm' | ||
const SearchBar = ({ data }) => { | ||
import searchAlgorithm from '../utils/searchAlgorithm' | ||
import { useState } from 'react' | ||
|
||
const SearchBar = ({ data, loading }) => { | ||
const [open, setOpen] = useState(false) | ||
const [filterData, setFilterData] = useState([]) | ||
|
||
const handleSearch = (e) => { | ||
let input = e.target.value.trim().toLowerCase() | ||
const filterData = searchAlgorithm(input) | ||
const filterData = searchAlgorithm(input, data).sort((a, b) => b.score - a.score) | ||
setOpen(input.length !== 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit skeptical with the |
||
setFilterData(filterData) | ||
} | ||
|
||
let searchResults = filterData?.map((item, index) => <CollegeResult info={item} serialNo={index} key={index} />) | ||
|
||
if (searchResults.length === 0) searchResults = <div>No results found</div> | ||
|
||
if (!loading && searchResults.length === 0) searchResults = <div>No results found</div> | ||
if (loading) searchResults = <div>Loading....</div> | ||
return ( | ||
<div className="search-box mt-4 w-5/12 h-12 rounded-2xl"> | ||
<div className="search-icon w-12 absolute h-12 flex justify-center items-center"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { gql } from '@apollo/client' | ||
export const query = gql` | ||
query getUniversityList { | ||
universityList { | ||
id | ||
name | ||
score | ||
country | ||
city | ||
} | ||
} | ||
` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { matchSorter } from 'match-sorter' | ||
import colleges from '../../data/university_data.json' | ||
|
||
const searchAlgorithm = (input) => { | ||
return matchSorter(colleges, input, { keys: ['name', 'address'] }) | ||
const searchAlgorithm = (input, data) => { | ||
if (input && data) { | ||
return matchSorter(data, input, { keys: ['name', 'country', 'city'] }) | ||
} else { | ||
return [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I checked in the codecov, it turns out, this line wasn't tested at the moment. So, let's write a simple test for it, which can be triggered by empty data. |
||
} | ||
} | ||
export default searchAlgorithm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: If you want to try the describe statement, give it a shot.
You can fetch the data inside the describe and use the same variable inside both the tests. Here's an example (I copied it from ChatGPT):