Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library Page Tests #229

Merged
merged 4 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions __tests__/Library.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import React from 'react';
ademDurakovic marked this conversation as resolved.
Show resolved Hide resolved
ademDurakovic marked this conversation as resolved.
Show resolved Hide resolved
import { render, fireEvent, waitFor, act } from '@testing-library/react-native';
import Library from '../lib/screens/Library';
import { useTheme } from '../lib/components/ThemeProvider';
import ApiService from '../lib/utils/api_calls';
import ToastMessage from 'react-native-toast-message';
import { onAuthStateChanged } from 'firebase/auth';
import configureStore from 'redux-mock-store';
import { AddNoteProvider } from '../lib/context/AddNoteContext';
import moxios from 'moxios'


jest.mock('firebase/database', () => ({
getDatabase: jest.fn(),
}));

jest.mock('firebase/auth', () => ({
getAuth: jest.fn(),
initializeAuth: jest.fn(),
getReactNativePersistence: jest.fn(),
onAuthStateChanged: jest.fn(),
}));

jest.mock('expo-font', () => ({
loadAsync: jest.fn(() => Promise.resolve()),
isLoaded: jest.fn(() => true),
}));

jest.mock('react-native/Libraries/Image/Image', () => ({
...jest.requireActual('react-native/Libraries/Image/Image'),
resolveAssetSource: jest.fn(() => ({ uri: 'mocked-asset-uri' })),
}));

jest.mock('react-native/Libraries/Settings/NativeSettingsManager', () => ({
settings: {},
setValues: jest.fn(),
getConstants: jest.fn(() => ({
settings: {},
})),
}));

//mock carousel
jest.mock('react-native-reanimated-carousel', () => {
const React = require('react');
const { View } = require('react-native');
return (props) => <View>{props.children}</View>;
});

const mockStore = configureStore([]);
const store = mockStore({
navigation: {
navState: 'more',
},
theme: {
darkMode: false,
},
});

const mockToggleDarkmode = jest.fn();

jest.mock('../lib/components/ThemeProvider', () => ({
useTheme: jest.fn(() => ({
theme: {
primaryColor: '#ffffff',
text: '#000000',
secondaryColor: '#f0f0f0',
logout: '#ff0000',
logoutText: '#ffffff',
},
isDarkmode: false,
toggleDarkmode: mockToggleDarkmode,
})),
}));

// Mock expo-location module with TypeScript type support
jest.mock('expo-location', () => ({
getForegroundPermissionsAsync: jest.fn(),
requestForegroundPermissionsAsync: jest.fn(),
getCurrentPositionAsync: jest.fn(),
}));


// Silence console warnings during the test
beforeEach(() => {
jest.clearAllMocks();

jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
moxios.install()
});

afterEach(() => {
moxios.uninstall()
});

describe('Library Component', () => {
it('renders without crashing', async () => {
const navigationMock = {
navigate: jest.fn(),
goBack: jest.fn(),
push: jest.fn(),
};
const routeMock = { params: {} }; // Mock route prop
const { getByTestId } = render(
<Library navigation={navigationMock as any} route={routeMock as any} />
);
await waitFor(() => expect(getByTestId('Library')).toBeTruthy());
ademDurakovic marked this conversation as resolved.
Show resolved Hide resolved
});


it('renders search bar', async () => {
const navigationMock = {
navigate: jest.fn(),
goBack: jest.fn(),
push: jest.fn(),
};
const routeMock = { params: {} }; // Mock route prop
const { getByTestId } = render(
<Library navigation={navigationMock as any} route={routeMock as any} />
);
await waitFor(() => expect(getByTestId('SearchBar')).toBeTruthy());
});

it('renders filter bar', async () => {
ademDurakovic marked this conversation as resolved.
Show resolved Hide resolved
const navigationMock = {
navigate: jest.fn(),
goBack: jest.fn(),
push: jest.fn(),
};
const routeMock = { params: {} }; // Mock route prop
const { getByTestId } = render(
<Library navigation={navigationMock as any} route={routeMock as any} />
);
await waitFor(() => expect(getByTestId('Filter')).toBeTruthy());
});

});
7 changes: 3 additions & 4 deletions lib/screens/Library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const Library = ({ navigation, route }) => {
const [isModalVisible, setModalVisible] = useState(false);
const [userName, setUserName] = useState('')
const { theme, isDarkmode } = useTheme();
const { setNavigateToAddNote } = useAddNoteContext();
const [isSearchVisible, setIsSearchVisible] = useState(false);
const [isSortOpened, setIsSortOpened] = useState(false);
const [selectedSortOption, setSelectedSortOption] = useState(1);
Expand Down Expand Up @@ -289,7 +288,7 @@ const Library = ({ navigation, route }) => {
}

return (
<View style={{ flex: 1, backgroundColor: isDarkmode ? 'black' : '#e4e4e4' }}>
<View testID = "Library" style={{ flex: 1, backgroundColor: isDarkmode ? 'black' : '#e4e4e4' }}>
<StatusBar translucent backgroundColor="transparent" />
<View style={styles(theme, width).container}>
<View style={styles(theme, width).topView}>
Expand Down Expand Up @@ -327,7 +326,7 @@ const Library = ({ navigation, route }) => {

</View>

<View style={[styles(theme, width).toolContainer, { marginHorizontal: 20 }]}>
<View testID= "Filter" style={[styles(theme, width).toolContainer, { marginHorizontal: 20 }]}>
ademDurakovic marked this conversation as resolved.
Show resolved Hide resolved
{
!isSearchVisible && (
<View>
Expand All @@ -347,7 +346,7 @@ const Library = ({ navigation, route }) => {
</View>
)
}
<View style={[styles(theme, width).searchParentContainer, { width: isSearchVisible ? '95%' : 40 }]}>
<View testID="SearchBar" style={[styles(theme, width).searchParentContainer, { width: isSearchVisible ? '95%' : 40 }]}>

{/* Search Container */}
{isSearchVisible && (
Expand Down