mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 10:02:27 -04:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
jest.mock('shared/supabase/init')
|
|
jest.mock('shared/supabase/utils')
|
|
jest.mock('common/util/try-catch')
|
|
jest.mock('shared/profiles/parse-photos')
|
|
jest.mock('shared/supabase/users')
|
|
|
|
import {AuthedUser} from 'api/helpers/endpoint'
|
|
import {updateProfile} from 'api/update-profile'
|
|
import {sqlMatch} from 'common/test-utils'
|
|
import {tryCatch} from 'common/util/try-catch'
|
|
import {removePinnedUrlFromPhotoUrls} from 'shared/profiles/parse-photos'
|
|
import * as supabaseInit from 'shared/supabase/init'
|
|
import * as supabaseUsers from 'shared/supabase/users'
|
|
import * as supabaseUtils from 'shared/supabase/utils'
|
|
|
|
describe('updateProfiles', () => {
|
|
let mockPg: any
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
mockPg = {
|
|
oneOrNone: jest.fn(),
|
|
}
|
|
;(supabaseInit.createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg)
|
|
})
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
describe('when given valid input', () => {
|
|
it('should update an existing profile when provided the user id', async () => {
|
|
const mockProps = {
|
|
pinned_url: 'mockAvatarUrl',
|
|
}
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockReq = {} as any
|
|
const mockData = 'success'
|
|
|
|
;(tryCatch as jest.Mock)
|
|
.mockResolvedValueOnce({data: true})
|
|
.mockResolvedValueOnce({data: mockData, error: null})
|
|
|
|
const result = await updateProfile(mockProps, mockAuth, mockReq)
|
|
|
|
expect(result).toBe(mockData)
|
|
expect(mockPg.oneOrNone).toBeCalledTimes(1)
|
|
expect(mockPg.oneOrNone).toBeCalledWith(
|
|
sqlMatch('select * from profiles where user_id = $1'),
|
|
[mockAuth.uid],
|
|
)
|
|
expect(removePinnedUrlFromPhotoUrls).toBeCalledTimes(1)
|
|
expect(removePinnedUrlFromPhotoUrls).toBeCalledWith(mockProps)
|
|
expect(supabaseUsers.updateUser).toBeCalledTimes(1)
|
|
expect(supabaseUsers.updateUser).toBeCalledWith(expect.any(Object), mockAuth.uid, {
|
|
avatarUrl: mockProps.pinned_url,
|
|
})
|
|
expect(supabaseUtils.update).toBeCalledTimes(1)
|
|
expect(supabaseUtils.update).toBeCalledWith(
|
|
expect.any(Object),
|
|
'profiles',
|
|
'user_id',
|
|
expect.any(Object),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('when an error occurs', () => {
|
|
it('should throw if the profile does not exist', async () => {
|
|
const mockProps = {
|
|
avatar_url: 'mockAvatarUrl',
|
|
}
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockReq = {} as any
|
|
|
|
;(tryCatch as jest.Mock).mockResolvedValue({data: false})
|
|
|
|
expect(updateProfile(mockProps, mockAuth, mockReq)).rejects.toThrow('Profile not found')
|
|
})
|
|
|
|
it('should throw if unable to update the profile', async () => {
|
|
const mockProps = {
|
|
avatar_url: 'mockAvatarUrl',
|
|
}
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockReq = {} as any
|
|
|
|
;(tryCatch as jest.Mock)
|
|
.mockResolvedValueOnce({data: true})
|
|
.mockResolvedValueOnce({data: null, error: Error})
|
|
|
|
expect(updateProfile(mockProps, mockAuth, mockReq)).rejects.toThrow('Error updating profile')
|
|
})
|
|
})
|
|
})
|