-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,390 additions
and
2,301 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"extends": ["plugin:vitest-globals/recommended"], | ||
"env": { | ||
"jest/globals": true | ||
"jest/globals": true, | ||
"vitest-globals/env": true | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,10 +6,11 @@ import { POST as UpdateUserNamePOST } from '@app/auth/update-user-name/route'; | |
import '@testing-library/jest-dom'; | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import fetch from '@tests/__mocks__/fetchMock'; | ||
import { renderServerComponent } from '@tests/__utils__/renderServerComponent'; | ||
import fetch from 'jest-fetch-mock'; | ||
import { headers } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
import { mockCaptchaSuccess } from './__mocks__/captchaMockUtils'; | ||
import { supabase } from './__mocks__/supabaseAuthHelpersMock'; | ||
import { | ||
mockSupabaseSession, | ||
|
@@ -29,7 +30,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
vi.restoreAllMocks(); | ||
restoreLocationObject(); | ||
}); | ||
|
||
|
@@ -38,25 +39,27 @@ describe('Account Settings page', () => { | |
await mockSupabaseSession(); | ||
await renderServerComponent(<AccountSettings />); | ||
expect( | ||
screen.getByRole('heading', { | ||
await screen.findByRole('heading', { | ||
name: 'Account Settings | Faith Dashboard' | ||
}) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should redirect to Sign In page if signed out', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
// Mock the value of the x-url header so that the source code can correctly | ||
// determine the URL to redirect to (after sign-in) | ||
jest | ||
.spyOn(headers(), 'get') | ||
.mockImplementation(() => 'https://localhost:3000/account'); | ||
vi.spyOn(headers(), 'get').mockImplementation( | ||
() => 'https://localhost:3000/account' | ||
); | ||
await renderServerComponent(<AccountSettings />); | ||
expect(redirect).toHaveBeenCalledWith( | ||
`/sign-in?redirect_to=${encodeURIComponent('/account')}` | ||
); | ||
}); | ||
|
||
it('should update name of user successfully', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
fetch.mockIf(/\/auth\/update-user-name/, async () => { | ||
|
@@ -70,7 +73,9 @@ describe('Account Settings page', () => { | |
}, | ||
{ clearFieldsFirst: true } | ||
); | ||
await userEvent.click(screen.getByRole('button', { name: 'Save Details' })); | ||
await userEvent.click( | ||
await screen.findByRole('button', { name: 'Save Details' }) | ||
); | ||
const [actualFetchUrl, actualFetchOptions] = fetch.mock.calls[0]; | ||
expect(actualFetchUrl).toEqual('/auth/update-user-name'); | ||
expect(actualFetchOptions?.method?.toUpperCase()).toEqual('POST'); | ||
|
@@ -85,6 +90,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should request email change successfully', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
fetch.mockIf(/\/auth\/request-email-change/, async () => { | ||
|
@@ -98,7 +104,9 @@ describe('Account Settings page', () => { | |
}, | ||
{ clearFieldsFirst: true } | ||
); | ||
await userEvent.click(screen.getByRole('button', { name: 'Change Email' })); | ||
await userEvent.click( | ||
await screen.findByRole('button', { name: 'Change Email' }) | ||
); | ||
const [actualFetchUrl, actualFetchOptions] = fetch.mock.calls[0]; | ||
expect(actualFetchUrl).toEqual('/auth/request-email-change'); | ||
expect(actualFetchOptions?.method?.toUpperCase()).toEqual('POST'); | ||
|
@@ -113,14 +121,15 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should validate that emails are not matching', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
await renderServerComponent(<AccountSettings />); | ||
await typeIntoFormFields({ | ||
'New Email': '[email protected]', | ||
'Confirm New Email': '[email protected]' | ||
}); | ||
expect(screen.getByLabelText('Confirm New Email')).toHaveProperty( | ||
expect(await screen.findByLabelText('Confirm New Email')).toHaveProperty( | ||
'validationMessage', | ||
'Emails must match' | ||
); | ||
|
@@ -131,16 +140,19 @@ describe('Account Settings page', () => { | |
await mockSupabaseSession(); | ||
await renderServerComponent(<AccountSettings />); | ||
const requiredFields = ['New Email', 'Confirm New Email']; | ||
await userEvent.click(screen.getByRole('button', { name: 'Change Email' })); | ||
requiredFields.forEach((labelText) => { | ||
expect(screen.getByLabelText(labelText)).toHaveProperty( | ||
await userEvent.click( | ||
await screen.findByRole('button', { name: 'Change Email' }) | ||
); | ||
requiredFields.forEach(async (labelText) => { | ||
expect(await screen.findByLabelText(labelText)).toHaveProperty( | ||
'validity.valueMissing', | ||
true | ||
); | ||
}); | ||
}); | ||
|
||
it('should cancel email change successfully', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser({ | ||
email: '[email protected]', | ||
new_email: '[email protected]', | ||
|
@@ -152,7 +164,7 @@ describe('Account Settings page', () => { | |
}); | ||
await renderServerComponent(<AccountSettings />); | ||
await userEvent.click( | ||
screen.getByRole('button', { name: 'Cancel Email Change' }) | ||
await screen.findByRole('button', { name: 'Cancel Email Change' }) | ||
); | ||
const [actualFetchUrl, actualFetchOptions] = fetch.mock.calls[0]; | ||
expect(actualFetchUrl).toEqual('/auth/cancel-email-change'); | ||
|
@@ -163,6 +175,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should change password successfully', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
fetch.mockIf(/\/auth\/change-password/, async () => { | ||
|
@@ -175,7 +188,7 @@ describe('Account Settings page', () => { | |
'Confirm New Password': 'CorrectHorseBatteryStaple' | ||
}); | ||
await userEvent.click( | ||
screen.getByRole('button', { name: 'Change Password' }) | ||
await screen.findByRole('button', { name: 'Change Password' }) | ||
); | ||
const [actualFetchUrl, actualFetchOptions] = fetch.mock.calls[0]; | ||
expect(actualFetchUrl).toEqual('/auth/change-password'); | ||
|
@@ -189,6 +202,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should validate that passwords are not matching', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
await renderServerComponent(<AccountSettings />); | ||
|
@@ -197,13 +211,14 @@ describe('Account Settings page', () => { | |
'New Password': 'CorrectHorseBatteryStaple', | ||
'Confirm New Password': 'CorrectHorseBatteryStale' | ||
}); | ||
expect(screen.getByLabelText('Confirm New Password')).toHaveProperty( | ||
expect(await screen.findByLabelText('Confirm New Password')).toHaveProperty( | ||
'validationMessage', | ||
'Passwords must match' | ||
); | ||
}); | ||
|
||
it('should require all Change Password fields to be populated', async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
await mockSupabaseUser(); | ||
await mockSupabaseSession(); | ||
await renderServerComponent(<AccountSettings />); | ||
|
@@ -213,18 +228,19 @@ describe('Account Settings page', () => { | |
'Confirm New Password' | ||
]; | ||
await userEvent.click( | ||
screen.getByRole('button', { name: 'Change Password' }) | ||
await screen.findByRole('button', { name: 'Change Password' }) | ||
); | ||
requiredFields.forEach((labelText) => { | ||
expect(screen.getByLabelText(labelText)).toHaveProperty( | ||
requiredFields.forEach(async (labelText) => { | ||
expect(await screen.findByLabelText(labelText)).toHaveProperty( | ||
'validity.valueMissing', | ||
true | ||
); | ||
}); | ||
}); | ||
|
||
it('should change user name on server side', async () => { | ||
jest.spyOn(supabase.auth, 'updateUser').mockImplementationOnce(async () => { | ||
mockCaptchaSuccess('mytoken'); | ||
vi.spyOn(supabase.auth, 'updateUser').mockImplementationOnce(async () => { | ||
return { data: {}, error: null } as any; | ||
}); | ||
const fields = { | ||
|
@@ -245,7 +261,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should change password on server side', async () => { | ||
jest.spyOn(supabase, 'rpc').mockImplementationOnce(() => { | ||
vi.spyOn(supabase, 'rpc').mockImplementationOnce(() => { | ||
return { data: {}, error: null } as any; | ||
}); | ||
const fields = { | ||
|
@@ -265,7 +281,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should request email change on server side', async () => { | ||
jest.spyOn(supabase.auth, 'updateUser').mockImplementationOnce(() => { | ||
vi.spyOn(supabase.auth, 'updateUser').mockImplementationOnce(() => { | ||
return { data: {}, error: null } as any; | ||
}); | ||
const fields = { | ||
|
@@ -283,7 +299,7 @@ describe('Account Settings page', () => { | |
}); | ||
|
||
it('should cancel email change on server side', async () => { | ||
jest.spyOn(supabase, 'rpc').mockImplementationOnce(() => { | ||
vi.spyOn(supabase, 'rpc').mockImplementationOnce(() => { | ||
return { data: {}, error: null } as any; | ||
}); | ||
await callRouteHandler({ | ||
|
Oops, something went wrong.