Skip to content

Commit

Permalink
test: Get Sent Frienship Requests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed Jan 13, 2025
1 parent c701f75 commit cce1500
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 17 deletions.
23 changes: 23 additions & 0 deletions test/mocks/friendship-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FriendshipRequest } from '../../src/types'

/**
* Creates a mock friendship request from given parameters.
*/
export const createMockFriendshipRequest = (
address: string,
timestamp: string,
message?: string
): FriendshipRequest => ({
address,
timestamp,
metadata: message ? { message } : undefined
})

/**
* Creates the expected mapped response for a friendship request.
*/
export const createMockExpectedFriendshipRequest = (address: string, createdAt: string, message: string) => ({
user: { address },
createdAt: new Date(createdAt).getTime(),
message
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { mockDb, mockLogs } from '../../../../../mocks/components'
import { getPendingFriendshipRequestsService } from '../../../../../../src/adapters/rpc-server/services/get-pending-friendship-requests'
import { RpcServerContext, FriendshipRequest, AppComponents } from '../../../../../../src/types'
import { RpcServerContext, AppComponents } from '../../../../../../src/types'
import { FriendshipRequestsResponse } from '@dcl/protocol/out-ts/decentraland/social_service_v2/social_service.gen'
import { emptyRequest } from '../../../../../mocks/empty-request'
import {
createMockFriendshipRequest,
createMockExpectedFriendshipRequest
} from '../../../../../mocks/friendship-request'

describe('getPendingFriendshipRequestsService', () => {
let components: jest.Mocked<Pick<AppComponents, 'db' | 'logs'>>
Expand Down Expand Up @@ -33,8 +37,8 @@ describe('getPendingFriendshipRequestsService', () => {
$case: 'requests',
requests: {
requests: [
createMockExpectedRequest('0x456', '2025-01-01T00:00:00Z', 'Hi!'),
createMockExpectedRequest('0x789', '2025-01-02T00:00:00Z', '')
createMockExpectedFriendshipRequest('0x456', '2025-01-01T00:00:00Z', 'Hi!'),
createMockExpectedFriendshipRequest('0x789', '2025-01-02T00:00:00Z', '')
]
}
}
Expand Down Expand Up @@ -67,22 +71,9 @@ describe('getPendingFriendshipRequestsService', () => {
response: {
$case: 'requests',
requests: {
requests: [createMockExpectedRequest('0x456', '2025-01-01T00:00:00Z', '')]
requests: [createMockExpectedFriendshipRequest('0x456', '2025-01-01T00:00:00Z', '')]
}
}
})
})

// Helpers
const createMockFriendshipRequest = (address: string, timestamp: string, message?: string): FriendshipRequest => ({
address,
timestamp,
metadata: message ? { message } : undefined
})

const createMockExpectedRequest = (address: string, createdAt: string, message: string) => ({
user: { address },
createdAt: new Date(createdAt).getTime(),
message
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { mockDb, mockLogs } from '../../../../../mocks/components'
import { getSentFriendshipRequestsService } from '../../../../../../src/adapters/rpc-server/services/get-sent-friendship-requests'
import { RpcServerContext, AppComponents } from '../../../../../../src/types'
import { emptyRequest } from '../../../../../mocks/empty-request'
import {
createMockFriendshipRequest,
createMockExpectedFriendshipRequest
} from '../../../../../mocks/friendship-request'
import { FriendshipRequestsResponse } from '@dcl/protocol/out-ts/decentraland/social_service_v2/social_service.gen'

describe('getSentFriendshipRequestsService', () => {
let components: jest.Mocked<Pick<AppComponents, 'db' | 'logs'>>
let getSentRequests: ReturnType<typeof getSentFriendshipRequestsService>

const rpcContext: RpcServerContext = {
address: '0x123',
subscribers: undefined
}

beforeEach(() => {
components = { db: mockDb, logs: mockLogs }
getSentRequests = getSentFriendshipRequestsService({ components })
})

it('should return the correct list of sent friendship requests', async () => {
const mockSentRequests = [
createMockFriendshipRequest('0x456', '2025-01-01T00:00:00Z', 'Hello!'),
createMockFriendshipRequest('0x789', '2025-01-02T00:00:00Z')
]

mockDb.getSentFriendshipRequests.mockResolvedValueOnce(mockSentRequests)

const result: FriendshipRequestsResponse = await getSentRequests(emptyRequest, rpcContext)

expect(result).toEqual({
response: {
$case: 'requests',
requests: {
requests: [
createMockExpectedFriendshipRequest('0x456', '2025-01-01T00:00:00Z', 'Hello!'),
createMockExpectedFriendshipRequest('0x789', '2025-01-02T00:00:00Z', '')
]
}
}
})
})

it('should handle database errors gracefully', async () => {
mockDb.getSentFriendshipRequests.mockImplementationOnce(() => {
throw new Error('Database error')
})

const result: FriendshipRequestsResponse = await getSentRequests(emptyRequest, rpcContext)

expect(result).toEqual({
response: {
$case: 'internalServerError',
internalServerError: {}
}
})
})

it('should map metadata.message to an empty string if undefined', async () => {
const mockSentRequests = [createMockFriendshipRequest('0x456', '2025-01-01T00:00:00Z')]

mockDb.getSentFriendshipRequests.mockResolvedValueOnce(mockSentRequests)

const result: FriendshipRequestsResponse = await getSentRequests(emptyRequest, rpcContext)

expect(result).toEqual({
response: {
$case: 'requests',
requests: {
requests: [createMockExpectedFriendshipRequest('0x456', '2025-01-01T00:00:00Z', '')]
}
}
})
})
})

0 comments on commit cce1500

Please sign in to comment.