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
76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import {getCurrentPrivateUser} from 'api/get-current-private-user'
|
|
import {AuthedUser} from 'api/helpers/endpoint'
|
|
import {sqlMatch} from 'common/test-utils'
|
|
import {tryCatch} from 'common/util/try-catch'
|
|
import * as supabaseInit from 'shared/supabase/init'
|
|
|
|
jest.mock('shared/supabase/init')
|
|
jest.mock('common/util/try-catch')
|
|
|
|
describe('getCurrentPrivateUser', () => {
|
|
let mockPg = {} as 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 get current private user', async () => {
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockProps = {} as any
|
|
const mockReq = {} as any
|
|
const mockData = {
|
|
data: {mockData: 'mockDataValue'},
|
|
id: 'mockId',
|
|
}
|
|
;(mockPg.oneOrNone as jest.Mock).mockResolvedValue(null)
|
|
;(tryCatch as jest.Mock).mockResolvedValue({data: mockData, error: null})
|
|
|
|
const result = await getCurrentPrivateUser(mockProps, mockAuth, mockReq)
|
|
|
|
expect(result).toBe(mockData.data)
|
|
expect(mockPg.oneOrNone).toBeCalledWith(
|
|
sqlMatch('select * from private_users where id = $1'),
|
|
[mockAuth.uid],
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('when an error occurs', () => {
|
|
it('should throw if unable to get users private data', async () => {
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockProps = {} as any
|
|
const mockReq = {} as any
|
|
const mockData = {
|
|
data: {mockData: 'mockDataValue'},
|
|
id: 'mockId',
|
|
}
|
|
;(mockPg.oneOrNone as jest.Mock).mockResolvedValue(null)
|
|
;(tryCatch as jest.Mock).mockResolvedValue({data: mockData, error: Error})
|
|
|
|
expect(getCurrentPrivateUser(mockProps, mockAuth, mockReq)).rejects.toThrow(
|
|
'Error fetching private user data: ',
|
|
)
|
|
})
|
|
|
|
it('should throw if unable to find user account', async () => {
|
|
const mockAuth = {uid: '321'} as AuthedUser
|
|
const mockProps = {} as any
|
|
const mockReq = {} as any
|
|
|
|
;(mockPg.oneOrNone as jest.Mock).mockResolvedValue(null)
|
|
;(tryCatch as jest.Mock).mockResolvedValue({data: null, error: null})
|
|
|
|
expect(getCurrentPrivateUser(mockProps, mockAuth, mockReq)).rejects.toThrow(
|
|
'Your account was not found',
|
|
)
|
|
})
|
|
})
|
|
})
|