-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test for app/components/channel_info/avatar/index.tsx
- Loading branch information
1 parent
d54de5f
commit 091830a
Showing
1 changed file
with
48 additions
and
35 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 |
---|---|---|
|
@@ -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'; | ||
|
@@ -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(); | ||
}); | ||
}); |