Skip to content

Commit

Permalink
fix: Fixes frontend tests (#363)
Browse files Browse the repository at this point in the history
**QA Steps:**
1. Create a scaf project
2. Cd into frontend folder
3. run `npm run test` and expect to see success (make sure `npm i` is
run to install packages)

---------

Co-authored-by: sedatbasar <[email protected]>
  • Loading branch information
sedatbasar and sedatbasar authored Sep 4, 2024
1 parent 2fa7276 commit 4a2a5b2
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 88 deletions.
5 changes: 5 additions & 0 deletions {{cookiecutter.project_slug}}/frontend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"name": "@apollo/client/core",
"importNames": ["gql"],
"message": "Use the @/__generated__/gql to get proper typings!"
},
{
"name": "@testing-library/react",
"importNames": ["*"],
"message": "Use the imports from test-utils instead!"
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions {{cookiecutter.project_slug}}/frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ This project uses GraphQL for the backend, and Apollo Client for the frontend. T
## Environment Variables

| Variable Name | Explanation |
| ---------------------------- | ---------------------------------------------------------- | ----------------------------------------------------------------------- |
| IS_PRE_PUSH_HOOKS_ENABLED | Controls husky pre-push hooks for frontend folder | |
| ---------------------------- | ---------------------------------------------------------- | --- |
| IS_PRE_PUSH_HOOKS_ENABLED | Controls husky pre-push hooks for frontend folder | |
| NEXT_PUBLIC_GRAPHQL_ENDPOINT | The public graphql endpoint url |
| NEXT_GRAPHQL_ENDPOINT | The graphql endpoint url to be used for serverside queries | |
| NEXT_GRAPHQL_ENDPOINT | The graphql endpoint url to be used for serverside queries | |

## Suggested Tools

Expand Down
23 changes: 19 additions & 4 deletions {{cookiecutter.project_slug}}/frontend/__tests__/about.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import AboutPage from '../pages/about'
import { GET_ME } from '@/pages'
import { render, screen } from '@/utils/test-utils'

test('AboutPage', () => {
render(<AboutPage />)
expect(screen.getByRole('heading', { level: 1, name: 'About Page' })).toBeDefined()
const mocks = [
{
request: {
query: GET_ME
},
result: {
data: {
me: { id: '1', name: 'John Doe' }
}
}
}
]

test('AboutPage', async () => {
render(<AboutPage />, { mocks })
expect(await screen.findByRole('heading', { level: 1, name: 'About Page' })).toBeInTheDocument()
expect(await screen.findByText('John Doe')).toBeInTheDocument()
})
5 changes: 4 additions & 1 deletion {{cookiecutter.project_slug}}/frontend/lib/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const errorLink = onError(({ graphQLErrors, networkError }) => {
})

const httpLink = new HttpLink({
uri: typeof window === 'undefined' ? process.env.NEXT_GRAPHQL_ENDPOINT || 'http://backend:8000/graphql/' : process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT || 'http://localhost:8000/graphql/',
uri:
typeof window === 'undefined'
? process.env.NEXT_GRAPHQL_ENDPOINT || 'http://backend:8000/graphql/'
: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT || 'http://localhost:8000/graphql/',
credentials: 'include'
})

Expand Down
Loading

0 comments on commit 4a2a5b2

Please sign in to comment.