generated from well-known-components/template-server
-
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.
test: Get Sent Frienship Requests tests
- Loading branch information
1 parent
c701f75
commit cce1500
Showing
3 changed files
with
110 additions
and
17 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
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 | ||
}) |
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
79 changes: 79 additions & 0 deletions
79
test/unit/logic/adapters/rpc-server/services/get-sent-friendship-requests.spec.ts
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,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', '')] | ||
} | ||
} | ||
}) | ||
}) | ||
}) |