-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add some basic tests with actions and pre-commit updates. (#15)
1. Added some basic tests: - When the user is not logged in: - Home screen should be protected by ProtectedRoute. - Footer should still work to route cross pages. - When user loged in: - Home screen should be able to show the 'New Goal' on top of the screen. 3. Update the ci workflow accordingly: - Now all new commits will need to pass the test first. - All Firebase deployments and previews need to pass the test in order to run.
- Loading branch information
Showing
8 changed files
with
226 additions
and
62 deletions.
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
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 |
---|---|---|
|
@@ -13,11 +13,22 @@ jobs: | |
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: 🛎️ Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Determine PR size and add label | ||
id: size-label | ||
uses: "pascalgn/[email protected].4" | ||
uses: pascalgn/[email protected].5 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
IGNORED: | | ||
yarn.lock | ||
package-lock.json | ||
.pnp.* | ||
dist/** | ||
build/** | ||
.cache/** | ||
with: | ||
sizes: > | ||
{ | ||
|
@@ -43,7 +54,7 @@ jobs: | |
XXL: 'e99695', // Light coral | ||
}; | ||
const sizeLabel = core.getInput('size-label-output') || '${{ steps.size-label.outputs.sizeLabel }}'; | ||
const sizeLabel = ${{ steps.size-label.outputs.sizeLabel }}; | ||
const color = labelsToColor[sizeLabel]; | ||
if (sizeLabel) { | ||
|
@@ -66,7 +77,7 @@ jobs: | |
} | ||
- name: Comment on large PRs | ||
if: ${{ contains(steps.size-label.outputs.sizeLabel, 'XL') || contains(steps.size-label.outputs.sizeLabel, 'XXL') }} | ||
if: ${{ contains(steps.size-label.outputs.sizeLabel, 'XL') }} | ||
uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
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,2 @@ | ||
npx lint-staged | ||
npm run test:ci |
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 |
---|---|---|
@@ -1,18 +1,76 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { UserProvider } from '@contexts/UserContext'; | ||
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import App from './App'; | ||
|
||
describe('counter tests', () => { | ||
test('Counter should be 0 at the start', () => { | ||
render(<App />); | ||
expect(screen.getByText('count is: 0')).toBeDefined(); | ||
describe('Check data before user logged in', () => { | ||
test('Should show message when user is not logged in', async () => { | ||
render( | ||
<UserProvider> | ||
<App /> | ||
</UserProvider>, | ||
); | ||
|
||
// Use `waitFor` to ensure the message appears after any async updates. | ||
await waitFor(() => { | ||
const message = screen.getByText('Please sign in to view this page'); | ||
expect(message).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
test('Should redirect to Streak page when clicked on Streak tab', async () => { | ||
render( | ||
<UserProvider> | ||
<App /> | ||
</UserProvider>, | ||
); | ||
|
||
// Wait for the loading spinner to disappear | ||
await waitFor(() => { | ||
expect(screen.queryByRole('progressbar')).toBeNull(); | ||
}); | ||
|
||
// Click the "Streak" tab | ||
await act(async () => { | ||
fireEvent.click(screen.getByText(/streak/i)); | ||
}); | ||
|
||
// Verify that the URL has changed to "/streak" | ||
await waitFor(() => { | ||
expect(window.location.pathname).to.equal('/streak'); | ||
}); | ||
}); | ||
|
||
test('Counter should increment by one when clicked', async () => { | ||
render(<App />); | ||
const counter = screen.getByRole('button'); | ||
fireEvent.click(counter); | ||
expect(await screen.getByText('count is: 1')).toBeDefined(); | ||
test('Should able to redirect back to Home page from Streak page', async () => { | ||
render( | ||
<UserProvider> | ||
<App /> | ||
</UserProvider>, | ||
); | ||
|
||
// Wait for the loading spinner to disappear | ||
await waitFor(() => { | ||
expect(screen.queryByRole('progressbar')).toBeNull(); | ||
}); | ||
|
||
// Click the "Streak" tab | ||
await act(async () => { | ||
fireEvent.click(screen.getByText(/streak/i)); | ||
}); | ||
|
||
// Verify that the URL has changed to "/streak" | ||
await waitFor(() => { | ||
expect(window.location.pathname).to.equal('/streak'); | ||
}); | ||
|
||
// Click the "Home" tab | ||
await act(async () => { | ||
fireEvent.click(screen.getByText(/home/i)); | ||
}); | ||
|
||
// Verify that the URL has changed to "/" | ||
await waitFor(() => { | ||
expect(window.location.pathname).to.equal('/'); | ||
}); | ||
}); | ||
}); |
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 { UserProvider } from '@contexts/UserContext'; | ||
import Home from '@pages/Home'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { describe, expect, test, vi } from 'vitest'; | ||
|
||
// Mock `@contexts/UserContext` to control user profile and updates | ||
vi.mock('@contexts/UserContext', async () => { | ||
const actual = await vi.importActual('@contexts/UserContext'); | ||
let mockUserProfile = { | ||
uid: '123', | ||
profilePic: '', | ||
name: 'Test User', | ||
goals: [], | ||
streak: [], | ||
}; | ||
|
||
return { | ||
...actual, | ||
useUser: () => ({ | ||
user: mockUserProfile, | ||
loading: false, | ||
updateProfile: vi.fn((updates) => { | ||
mockUserProfile = { ...mockUserProfile, ...updates }; | ||
}), | ||
}), | ||
}; | ||
}); | ||
|
||
describe('Home Screen - No Goals', () => { | ||
beforeEach(() => { | ||
// Reset mockUserProfile for each test | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test('Displays only "New Goal" field when there are no goals', async () => { | ||
await act(async () => { | ||
render( | ||
<UserProvider> | ||
<Home /> | ||
</UserProvider>, | ||
); | ||
}); | ||
|
||
// Check if "New Goal" text field is present | ||
const newGoalInput = screen.getByLabelText('New Goal'); | ||
expect(newGoalInput).not.to.be.null; | ||
|
||
// Ensure "New Microgoal" and "New Task" fields are not present initially | ||
expect(screen.queryByLabelText('New Microgoal')).to.be.null; | ||
expect(screen.queryByLabelText('New Task')).to.be.null; | ||
}); | ||
}); |
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