Skip to content

Commit

Permalink
test: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis3797 committed Jun 20, 2024
1 parent 288b9ef commit ac3a213
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 43 deletions.
66 changes: 66 additions & 0 deletions __test__/components/StreamPreviewImage.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import { fireEvent } from '@testing-library/react-native';
import StreamPreviewImage from '../../src/components/StreamPreviewImage'; // Adjust the path as necessary
import { customRender } from '../test_utils/customRender';

describe('StreamPreviewImage', () => {
const handleAddImageMock = jest.fn();
const clearImageMock = jest.fn();

beforeEach(() => {
handleAddImageMock.mockClear();
clearImageMock.mockClear();
});

it('customRenders correctly without image', () => {
const { getByTestId, queryByTestId } = customRender(
<StreamPreviewImage
uri={null}
handleAddImage={handleAddImageMock}
clearImage={clearImageMock}
/>
);

expect(queryByTestId('add-image-button')).toBeTruthy();
expect(queryByTestId('clear-image-button')).toBeFalsy();
});

it('customRenders correctly with image', () => {
const { getByTestId, queryByTestId } = customRender(
<StreamPreviewImage
uri="https://example.com/image.jpg"
handleAddImage={handleAddImageMock}
clearImage={clearImageMock}
/>
);

expect(queryByTestId('add-image-button')).toBeFalsy();
expect(queryByTestId('clear-image-button')).toBeTruthy();
});

it('calls handleAddImage when add image button is pressed', () => {
const { getByTestId } = customRender(
<StreamPreviewImage
uri={null}
handleAddImage={handleAddImageMock}
clearImage={clearImageMock}
/>
);

fireEvent.press(getByTestId('add-image-button'));
expect(handleAddImageMock).toHaveBeenCalledTimes(1);
});

it('calls clearImage when clear image button is pressed', () => {
const { getByTestId } = customRender(
<StreamPreviewImage
uri="https://example.com/image.jpg"
handleAddImage={handleAddImageMock}
clearImage={clearImageMock}
/>
);

fireEvent.press(getByTestId('clear-image-button'));
expect(clearImageMock).toHaveBeenCalledTimes(1);
});
});
59 changes: 59 additions & 0 deletions __test__/modules/settings/SettingsList.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { fireEvent } from '@testing-library/react-native';
import React from 'react';

import useAuth from '../../../src/modules/auth/useAuth';
import SettingsList from '../../../src/modules/settings/SettingsList'; // Adjust the path as necessary
import { settingSectionsMap } from '../../../src/modules/settings/settings'; // Adjust the path as necessary
import { customRender } from '../../test_utils/customRender';

// Mock the useAuth hook
jest.mock('@modules/auth/useAuth', () => ({
__esModule: true,
default: jest.fn(),
}));

describe('SettingsList', () => {
const mockSignOut = jest.fn();
const originalConsoleLog = console.log;

beforeEach(() => {
(useAuth as jest.Mock).mockReturnValue({ signOut: mockSignOut });
console.log = jest.fn(); // Mock console.log
});

afterEach(() => {
jest.clearAllMocks();
console.log = originalConsoleLog; // Restore console.log
});
it('renders all settings sections and items correctly', () => {
const { getByTestId } = customRender(<SettingsList />);

Object.entries(settingSectionsMap).forEach(([key, items]) => {
// Check if the section header is rendered
expect(getByTestId(`${key}-header`)).toBeTruthy();

// Check if all items in the section are rendered
items.forEach((item) => {
expect(getByTestId(`${item}-button`)).toBeTruthy();
});
});
});

it('triggers the correct action on item press', () => {
const { getByTestId } = customRender(<SettingsList />);

Object.entries(settingSectionsMap).forEach(([_, items]) => {
items.forEach((item) => {
// Trigger the onPress event
fireEvent.press(getByTestId(`${item}-button`));

// Check console log or signOut action
if (item === 'Logout') {
expect(mockSignOut).toHaveBeenCalled();
} else {
expect(console.log).toHaveBeenCalled();
}
});
});
});
});
53 changes: 53 additions & 0 deletions __test__/modules/settings/settings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { settingSectionsMap } from '../../../src/modules/settings/settings';

describe('settingSectionMap', () => {
it('Should have expected Keys', () => {
expect(Object.keys(settingSectionsMap)).toHaveLength(8);
expect(Object.keys(settingSectionsMap)).toEqual([
'Account',
'Livestream Settings',
'Privacy and Safety',
'Notifications',
'Support and Feedback',
'Legal',
'About',
'Logout',
]);
});

it('Key value should have expected length', () => {
const valueLength = [4, 2, 3, 2, 3, 3, 4, 1];

Object.keys(settingSectionsMap).forEach((key, index) => {
expect(settingSectionsMap[key]).toHaveLength(valueLength[index]);
});
});

it('Key should have expected value', () => {
expect(settingSectionsMap.Account).toEqual(['Profile', 'Security', 'Shares', 'Permissions']);
expect(settingSectionsMap['Livestream Settings']).toEqual(['Preferences', 'Chat Options']);
expect(settingSectionsMap['Privacy and Safety']).toEqual([
'Privacy',
'Content Moderation',
'Blocked Users',
]);
expect(settingSectionsMap.Notifications).toEqual(['General', 'Livestream']);
expect(settingSectionsMap['Support and Feedback']).toEqual([
'Help Center',
'Report a Problem',
'Rate Us',
]);
expect(settingSectionsMap.Legal).toEqual([
'Terms of Service',
'Privacy Policy',
'Community Guidelines',
]);
expect(settingSectionsMap.About).toEqual([
'Version',
'Developer Info',
'Release Notes',
'Join Beta Program',
]);
expect(settingSectionsMap.Logout).toEqual(['Logout']);
});
});
2 changes: 1 addition & 1 deletion src/components/UserFollowerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const UserFollowerCard: React.FC<{
<XStack space="$3" alignItems="center">
<Avatar circular size="$5">
<Avatar.Image
accessibilityLabel="Nate Wienert"
accessibilityLabel={username}
src={
avatar_url ??
'https://lyveblobstorage.blob.core.windows.net/images/avatar_placeholder.png'
Expand Down
22 changes: 0 additions & 22 deletions src/components/settings/SettingButton.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/components/settings/SettingStack.tsx

This file was deleted.

9 changes: 5 additions & 4 deletions src/modules/settings/SettingsList.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Feather } from '@expo/vector-icons';
import { ScrollView } from 'react-native';
import useAuth from '@modules/auth/useAuth';
import React from 'react';
import { settingSectionsMap } from './settings';
import { ScrollView } from 'react-native';
import { H3, ListItem, Separator, YGroup, YStack } from 'tamagui';

import { settingSectionsMap } from './settings';
import { SettingTitle } from './types';
import useAuth from '@modules/auth/useAuth';

const SettingsList: React.FC = () => {
const { signOut } = useAuth();
Expand Down Expand Up @@ -38,7 +39,7 @@ const SettingsList: React.FC = () => {
<ScrollView showsVerticalScrollIndicator={false}>
{Object.entries(settingSectionsMap).map(([key, val]) => {
return (
<YStack key={key} paddingBottom="$5">
<YStack key={key} paddingBottom="$5" testID={`${key}-header`}>
<H3 ml="$2" mb="$3">
{key}
</H3>
Expand Down

0 comments on commit ac3a213

Please sign in to comment.