-
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 review 1 #25
Bi 46 review 1 #25
Changes from 21 commits
3e9779c
0d204c5
ed6783f
26bf75c
8782dcb
90d53f8
097944c
7aca49e
4c5cf23
77a39bf
e852ff9
8e9a9bf
380ae5d
44dd677
2dce44c
0a6110c
f6a3de9
9593a08
f47f523
3c49fb3
06f0941
7ffaf79
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import '@testing-library/jest-dom' | ||
|
||
import client from '../client' | ||
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' | ||
|
||
import fetch from 'cross-fetch' | ||
import { gql } from '@apollo/client' | ||
|
||
const query = gql` | ||
|
@@ -14,48 +16,48 @@ const query = gql` | |
} | ||
} | ||
` | ||
|
||
let uri = 'https://web-app-client-b69l4yjrq-bacpacs-projects.vercel.app/api/graphql' | ||
console.log('graphql_uri', uri) | ||
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. We can use the console log as: console.log("GraphQL URI in tests:", uri) |
||
const client = new ApolloClient({ | ||
link: new HttpLink({ uri, fetch }), | ||
cache: new InMemoryCache(), | ||
}) | ||
async function fetchData() { | ||
try { | ||
const result = await client.query({ query }) | ||
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. Instead of using a try-catch, we can use this statement directly inside the tests. If we do that, then the tests will fail, if the fetching fails. |
||
return result | ||
} catch (error) { | ||
console.error('Error in fetching college data form mongodb:', error) | ||
console.log('Error in fetching college data form mongodb:', error) | ||
} | ||
} | ||
// test to check the data is not empty | ||
test('the data is of college', async () => { | ||
try { | ||
const result = await fetchData() | ||
expect(result?.data).toHaveProperty('universityList') | ||
expect(result?.data?.universityList).toBeInstanceOf(Array) | ||
expect(result?.data?.universityList?.length).toBeGreaterThan(0) | ||
} catch (e) { | ||
expect(e.message).toMatch('error') | ||
} | ||
const result = await fetchData() | ||
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. Here... |
||
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 () => { | ||
try { | ||
const result = await fetchData() | ||
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') | ||
}) | ||
} catch (e) { | ||
expect(e.message).toMatch('error') | ||
} | ||
const result = await fetchData() | ||
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. Here... |
||
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,19 @@ | ||
import searchAlgorithm from '../utils/searchAlgorithm' | ||
describe('searchAlgorithm', () => { | ||
it('returns filtered results when input and data are provided', () => { | ||
const input = 'mass' | ||
const colleges = [{ name: 'Massachusetts Institute of Technology (MIT)', country: 'United States', city: 'Cambridge' }] | ||
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. Let's reuse the |
||
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 resultWithoutData = searchAlgorithm(input, null) | ||
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. Let's separate the setup lines, act and assertions by adding blank lines below and above the act. 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. You might want to rename the variable |
||
expect(resultWithoutData).toEqual([]) | ||
}) | ||
it('returns an empty array when either input doesnot match with the college name,city or country', () => { | ||
const input = 'xyw' | ||
const resultWithoutData = searchAlgorithm(input, null) | ||
expect(resultWithoutData).toEqual([]) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,15 @@ import { gql } from 'graphql-tag' | |
import { startServerAndCreateNextHandler } from '@as-integrations/next' | ||
|
||
// The connection string for mongodb connection. | ||
const uri = process.env.MONGODB_URI || 'mongodb+srv://bacpactech:[email protected]' | ||
const uri = process.env.MONGODB_URI | ||
const client = new MongoClient(uri) | ||
|
||
async function getUniversityName(id) { | ||
try { | ||
const database = client.db('bacpac') | ||
const universities = database.collection('universities') | ||
const university = await universities.findOne({ id }) | ||
console.log('url', uri) | ||
return university.name | ||
const universityList = await universities.findOne({ id }) | ||
return universityList.name | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
|
@@ -24,8 +23,8 @@ async function getUniversityList() { | |
try { | ||
const database = client.db('bacpac') | ||
const universities = database.collection('universities') | ||
const university = await universities.find().toArray() | ||
return university | ||
const universityList = await universities.find().toArray() | ||
return universityList | ||
} catch (error) { | ||
console.log('this is error from get all unversity list side', error) | ||
} | ||
|
@@ -57,7 +56,7 @@ const typeDefs = gql` | |
` | ||
|
||
let plugins = [] | ||
const graphQLref = process.env.GRAPHQL_REF || 'bacpac-nbq1vs@current' | ||
const graphQLref = process.env.GRAPHQL_REF | ||
//Next.js auto assigns NODE_ENV value as development for 'next dev' command, and production for other commands | ||
if (process.env.NODE_ENV === 'production') { | ||
plugins = [ | ||
|
@@ -75,7 +74,6 @@ const server = new ApolloServer({ | |
typeDefs, | ||
plugins, | ||
}) | ||
|
||
const handler = startServerAndCreateNextHandler(server) | ||
|
||
//Exports the handler function to be used as a Next.js API route handler. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,24 @@ import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' | |
|
||
import fetch from 'cross-fetch' | ||
|
||
let uri = '/api/graphql' | ||
// switch (process.env.NEXT_PUBLIC_LOCAL_STATE) { | ||
// case 'development': | ||
// uri = 'http://localhost:3000/api/graphql' | ||
// break | ||
// case 'test': | ||
// uri = 'http://localhost:3000/api/graphql' | ||
// break | ||
// case 'production': | ||
// // uri = 'https://web-app-client-git-bi-46-review-1-bacpacs-projects.vercel.app/api/graphql' | ||
// uri = 'https://web-app-client-flame.vercel.app/api/graphql' | ||
// break | ||
// default: | ||
// break | ||
// } | ||
console.log('graphql_uri', uri) | ||
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. We can use the console log as: console.log("GraphQL URI in Client:", uri) Remove the commented out lines. |
||
const client = new ApolloClient({ | ||
link: new HttpLink({ uri: 'http://localhost:3000/api/graphql', fetch }), | ||
link: new HttpLink({ uri, fetch }), | ||
cache: new InMemoryCache(), | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { gql } from '@apollo/client' | ||
|
||
// export const query = gql` | ||
// query getUniversityName($id: ID!) { | ||
// university_name(id: $id) | ||
// } | ||
// ` | ||
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. You can remove these commented out lines. |
||
export const query = gql` | ||
query getUniversityList { | ||
universityList { | ||
id | ||
name | ||
score | ||
country | ||
city | ||
} | ||
} | ||
` |
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.
Add a comment such that:
// TODO: Figure out a way to _not_ use a deployed vercel instance for GraphQL