Skip to content

Commit

Permalink
added test for app/components/channel_info/avatar/index.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat-Dabade committed Nov 26, 2024
1 parent d54de5f commit 091830a
Showing 1 changed file with 48 additions and 35 deletions.
83 changes: 48 additions & 35 deletions app/components/channel_info/avatar/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import {render} from '@testing-library/react-native';
import React from 'react';

import {buildProfileImageUrlFromUser} from '@actions/remote/user';

import Avatar from './';

import type UserModel from '@typings/database/models/servers/user';
Expand All @@ -14,56 +12,71 @@ jest.mock('@actions/remote/user', () => ({
}));

jest.mock('@actions/remote/file', () => ({
buildAbsoluteUrl: (serverUrl: string, uri: string) => `${serverUrl}${uri}`,
}));

jest.mock('@utils/theme', () => ({
changeOpacity: (color: string, opacity: number) => `rgba(${color}, ${opacity})`,
buildAbsoluteUrl: jest.fn(),
}));

jest.mock('@context/server', () => ({
useServerUrl: jest.fn(),
}));

describe('Avatar Component', () => {
const mockServerUrl = 'mock.server.url';
const mockAuthor = {
id: 'user123',
username: 'testuser',
email: '[email protected]',
firstName: 'Test',
lastName: 'User',
nickname: 'test',
locale: 'en',
lastPictureUpdate: 123456789,
updateAt: 123456789,
deleteAt: 0,
} as UserModel;
const mockBuildAbsoluteUrl = require('@actions/remote/file').buildAbsoluteUrl;
const mockBuildProfileImageUrlFromUser = require('@actions/remote/user').buildProfileImageUrlFromUser;
const mockUseServerUrl = require('@context/server').useServerUrl;

describe('Avatar Component', () => {
beforeEach(() => {
jest.resetAllMocks();
require('@context/server').useServerUrl.mockReturnValue(mockServerUrl);
jest.clearAllMocks();
});

it('renders the user profile image if available', () => {
(buildProfileImageUrlFromUser as jest.Mock).mockReturnValue('/profile/image/url');
it('renders the avatar image when URI is available', () => {
const mockServerUrl = 'base.url.com';
const mockUri = '/api/v4/users/mock-user-id/image';
const mockAuthor = {id: 'mock-user-id'} as UserModel;

const {getByTestId} = render(
<Avatar author={mockAuthor}/>,
);
mockUseServerUrl.mockReturnValue(mockServerUrl);
mockBuildProfileImageUrlFromUser.mockImplementation((_: string, author: UserModel) => {
return author ? mockUri : '';
});
mockBuildAbsoluteUrl.mockImplementation((serverUrl: string, uri: string) => `${serverUrl}${uri}`);

const image = getByTestId('avatar-image');
expect(image.props.source).toEqual({uri: `${mockServerUrl}/profile/image/url`});
const {queryByTestId} = render(<Avatar author={mockAuthor}/>);

expect(mockBuildProfileImageUrlFromUser).toHaveBeenCalledWith(mockServerUrl, mockAuthor);
expect(mockBuildAbsoluteUrl).toHaveBeenCalledWith(mockServerUrl, mockUri);

expect(queryByTestId('avatar-icon')).toBeNull();
});

it('renders the default icon if profile image URL is not available', () => {
(buildProfileImageUrlFromUser as jest.Mock).mockReturnValue('');
it('renders the fallback icon when URI is not available', () => {
const mockServerUrl = 'base.url.com';
const mockAuthor = {id: 'mock-user-id'} as UserModel;

mockUseServerUrl.mockReturnValue(mockServerUrl);
mockBuildProfileImageUrlFromUser.mockReturnValue('');
mockBuildAbsoluteUrl.mockReturnValue('');

const {getByTestId, queryByTestId} = render(<Avatar author={mockAuthor}/>);

expect(mockBuildProfileImageUrlFromUser).toHaveBeenCalledWith(mockServerUrl, mockAuthor);
expect(mockBuildAbsoluteUrl).not.toHaveBeenCalled();

const icon = getByTestId('avatar-icon');
expect(icon.props.name).toBe('account-outline');
expect(queryByTestId('avatar-image')).toBeNull();
});

it('renders the fallback icon when author is not provided', () => {
const mockServerUrl = 'base.url.com';

mockUseServerUrl.mockReturnValue(mockServerUrl);

const {getByTestId, queryByTestId} = render(<Avatar author={null as unknown as UserModel}/>);

const {getByTestId} = render(
<Avatar author={mockAuthor}/>,
);
expect(mockBuildProfileImageUrlFromUser).not.toHaveBeenCalled();
expect(mockBuildAbsoluteUrl).not.toHaveBeenCalled();

const icon = getByTestId('avatar-icon');
expect(icon.props.name).toBe('account-outline');
expect(queryByTestId('avatar-image')).toBeNull();
});
});

0 comments on commit 091830a

Please sign in to comment.