-
Notifications
You must be signed in to change notification settings - Fork 12
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
[Test] Login Frontend #54
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
08543d3
feat: add test
ca4f63c
feat: add layouts test
4b35820
feat: fix test and add Login.test.tsx
e3fff6c
feat: add ErrorPage.test.tsx
453c539
feat: add ToasterContext test
9c15529
setup e2e testing
Xiawpohr edcc951
setup e2e testing
Xiawpohr db6ab0e
fix: add assertion
Xiawpohr ae27d05
merge from Peter's main
fa9b3f7
feat: change test file from Peter's refactor
b70893f
Merge remote-tracking branch 'origin/test/post-frontend' into feature…
UranusLin d165da2
feat: merge from post e2e test
UranusLin 928a144
feat: fix yarn build failed
UranusLin aafa5c0
Merge remote-tracking branch 'origin/release/integrate_0713' into fea…
a0e81ed
feat: fix merge error
20fcbe3
feat: run lint: fix and yarn-deduplicate
2e2ff88
feat: fix github ci jest
fc75eea
feat: fix merge error
64b61e2
feat: try to fix github ci
1a63349
feat: try to fix github ci
7468ac7
feat: try to fix github ci
99ebe66
feat: try to fix github ci
25bf6e6
feat: try to fix github ci
88d3d66
feat: try to fix github ci
2cccbac
feat: modify frontend test command
b791a08
feat: modify frontend test command
57606e6
feat: add LoadingContext test
6c138cc
feat: add PostForm, User test
UranusLin 0383e8a
feat: lint fix
UranusLin 549a573
Merge remote-tracking branch 'origin/main' into feature/Add-frontend-…
4d6f191
feat: lint fix
b1d2e14
Merge branch 'main' into feature/Add-frontend-test
UranusLin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Playwright Tests | ||
on: | ||
push: | ||
branches: [main, master] | ||
pull_request: | ||
branches: [main, master] | ||
jobs: | ||
test: | ||
timeout-minutes: 60 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Install Playwright Browsers | ||
run: yarn playwright install --with-deps | ||
- name: Run Playwright tests | ||
run: yarn playwright test | ||
- uses: actions/upload-artifact@v3 | ||
if: always() | ||
with: | ||
name: playwright-report | ||
path: playwright-report/ | ||
retention-days: 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
build | ||
build | ||
node_modules/ | ||
/test-results/ | ||
/playwright-report/ | ||
/playwright/.cache/ | ||
|
||
coverage/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', { targets: { node: 'current' } }], | ||
'@babel/preset-typescript', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
moduleNameMapper: { | ||
'@testing-library/react': | ||
'<rootDir>/node_modules/@testing-library/react', | ||
'@types/jest': 'jest', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { defineConfig, devices } from '@playwright/test' | ||
|
||
/** | ||
* Read environment variables from file. | ||
* https://github.com/motdotla/dotenv | ||
*/ | ||
// require('dotenv').config(); | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: './tests', | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: process.env.CI ? 2 : 0, | ||
/* Opt out of parallel tests on CI. */ | ||
workers: process.env.CI ? 1 : undefined, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: 'html', | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Base URL to use in actions like `await page.goto('/')`. */ | ||
baseURL: 'http://127.0.0.1:3000', | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: 'on-first-retry', | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { ...devices['Desktop Chrome'] }, | ||
}, | ||
|
||
// { | ||
// name: 'firefox', | ||
// use: { ...devices['Desktop Firefox'] }, | ||
// }, | ||
|
||
// { | ||
// name: 'webkit', | ||
// use: { ...devices['Desktop Safari'] }, | ||
// }, | ||
|
||
/* Test against mobile viewports. */ | ||
{ | ||
name: 'Mobile Chrome', | ||
use: { ...devices['Pixel 5'] }, | ||
}, | ||
// { | ||
// name: 'Mobile Safari', | ||
// use: { ...devices['iPhone 12'] }, | ||
// }, | ||
|
||
/* Test against branded browsers. */ | ||
// { | ||
// name: 'Microsoft Edge', | ||
// use: { ...devices['Desktop Edge'], channel: 'msedge' }, | ||
// }, | ||
// { | ||
// name: 'Google Chrome', | ||
// use: { ...devices['Desktop Chrome'], channel: 'chrome' }, | ||
// }, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
// webServer: { | ||
// command: 'npm run start', | ||
// url: 'http://127.0.0.1:3000', | ||
// reuseExistingServer: !process.env.CI, | ||
// }, | ||
}) |
52 changes: 52 additions & 0 deletions
52
packages/frontend/src/__tests__/components/PostForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react' | ||
import { render, fireEvent, screen } from '@testing-library/react' | ||
import PostForm from '../../components/PostForm' | ||
import '@testing-library/jest-dom' | ||
import { expect } from '@jest/globals' | ||
|
||
jest.mock('@uidotdev/usehooks', () => ({ | ||
useIsFirstRender: jest.fn().mockReturnValue(false), | ||
})) | ||
|
||
test('<PostForm /> should render and handle interactions', () => { | ||
const mockOnCancel = jest.fn() | ||
const mockOnSaveDraft = jest.fn() | ||
const mockOnFetchDraft = jest.fn() | ||
const mockOnSubmit = jest.fn() | ||
|
||
render( | ||
<PostForm | ||
onCancel={mockOnCancel} | ||
onSaveDraft={mockOnSaveDraft} | ||
onFetchDraft={mockOnFetchDraft} | ||
onSubmit={mockOnSubmit} | ||
/> | ||
) | ||
|
||
// @ts-ignore | ||
expect(screen.getByTitle('cancel a post')).toBeInTheDocument() | ||
// @ts-ignore | ||
expect(screen.getByTitle('save a draft')).toBeInTheDocument() | ||
// @ts-ignore | ||
expect(screen.getByTitle('view a draft')).toBeInTheDocument() | ||
// @ts-ignore | ||
expect(screen.getByTitle('submit a post')).toBeInTheDocument() | ||
|
||
// Simulate button clicks | ||
fireEvent.click(screen.getByTitle('cancel a post')) | ||
expect(mockOnCancel).toHaveBeenCalled() | ||
|
||
fireEvent.click(screen.getByTitle('save a draft')) | ||
expect(mockOnSaveDraft).toHaveBeenCalled() | ||
|
||
fireEvent.click(screen.getByTitle('view a draft')) | ||
expect(mockOnFetchDraft).toHaveBeenCalled() | ||
|
||
// Simulate form submission | ||
// Assuming RichTextEditor sets value in contenteditable div | ||
const editableContent = screen.getByRole('textbox', { | ||
name: /post editor/i, | ||
}) | ||
editableContent.textContent = 'Test Content' | ||
fireEvent.input(editableContent) | ||
}) |
59 changes: 59 additions & 0 deletions
59
packages/frontend/src/__tests__/contexts/LoadingContext.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react' | ||
import { render, act } from '@testing-library/react' | ||
import { | ||
LoadingProvider, | ||
useLoading, | ||
LoadingStatus, | ||
} from '../../contexts/LoadingContext' | ||
import '@testing-library/jest-dom' | ||
|
||
describe('LoadingProvider', () => { | ||
it('provides the loading status value', () => { | ||
const TestComponent: React.FC = () => { | ||
const { status } = useLoading() | ||
return <div>{status}</div> | ||
} | ||
|
||
const { getByText } = render( | ||
<LoadingProvider> | ||
<TestComponent /> | ||
</LoadingProvider> | ||
) | ||
|
||
// @ts-ignore | ||
expect(getByText('loading')).toBeInTheDocument() | ||
}) | ||
|
||
it('allows updating the loading status', () => { | ||
const TestComponent: React.FC = () => { | ||
const { status, setStatus } = useLoading() | ||
|
||
return ( | ||
<div> | ||
<div>{status}</div> | ||
<button onClick={() => setStatus('success')}> | ||
Update Status | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const { getByText, getByRole } = render( | ||
<LoadingProvider> | ||
<TestComponent /> | ||
</LoadingProvider> | ||
) | ||
|
||
// init loading | ||
// @ts-ignore | ||
expect(getByText('loading')).toBeInTheDocument() | ||
|
||
// update status success | ||
act(() => { | ||
getByRole('button').click() | ||
}) | ||
|
||
// @ts-ignore | ||
expect(getByText('success')).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { render, act, waitFor } from '@testing-library/react' | ||
import { UserProvider, useUser } from '../../contexts/User' | ||
import '@testing-library/jest-dom' | ||
import { expect } from '@jest/globals' | ||
|
||
it('provides context to children', () => { | ||
const TestComponent = () => { | ||
const userContext = useUser() | ||
expect(userContext).toBeTruthy() // or other specific checks | ||
return null | ||
} | ||
|
||
render( | ||
<UserProvider> | ||
<TestComponent /> | ||
</UserProvider> | ||
) | ||
}) | ||
|
||
it('throws error when useUser is called outside of UserProvider', () => { | ||
const TestComponent = () => { | ||
expect(useUser).toThrowError( | ||
'useUser must be used within a UserProvider' | ||
) | ||
return null | ||
} | ||
|
||
render(<TestComponent />) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react' | ||
import BaseLayout from '../../layouts/BaseLayout' | ||
import { render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { expect } from '@jest/globals' | ||
|
||
test('BaseLayout should render', () => { | ||
render(<BaseLayout />) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we create tests for the functions in the context and make it safer?
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.
Yes, we need add more test about dom and state change