Skip to content

Commit

Permalink
Create tests for FollowScreen (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnkhee committed May 28, 2024
1 parent 06066fa commit 2c2a593
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 45 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"profile": "jest qa-tests/Profile.test.js",
"signup": "jest qa-tests/Signup.test.js",
"timeline": "jest qa-tests/Timeline.test.js",
"follow": "jest qa-tests/Follow.test.js",
"coverage": "jest --coverage"
},
"husky": {
Expand Down
136 changes: 136 additions & 0 deletions qa-tests/Follow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React from 'react';
import FollowScreen from '../src/screens/FollowScreen';
import {
render,
fireEvent,
waitFor,
screen,
} from '@testing-library/react-native';
import { supabase } from '../src/config/supabaseClient';
import store from '../src/store/storeConfig';
import { Alert } from 'react-native';

const mockUser = {
id: 'user2',
username: 'testuser',
profile_image: 'https://example.com/image.jpg',
};

// Mock modules and functions
jest.mock('../src/config/supabaseClient', () => ({
supabase: {
auth: {
signInWithPassword: jest.fn(),
signUp: jest.fn(),
startAutoRefresh: jest.fn(),
stopAutoRefresh: jest.fn(),
},
from: jest.fn(),
},
}));

jest.mock('../src/store/storeConfig', () => ({
getState: jest.fn(),
}));

jest.spyOn(Alert, 'alert');

describe('FollowScreen Component', () => {
beforeEach(() => {
store.getState.mockReturnValue({
user: { session: { user: { id: 'user1' } } },
});
supabase.from.mockClear();
Alert.alert.mockClear();
});

test('should render FollowScreen component', () => {
const { getByPlaceholderText } = render(<FollowScreen />);
expect(getByPlaceholderText('Search by username')).toBeTruthy();
});

test('fetchFollowingList should fetch following list on session change', async () => {
supabase.from.mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockResolvedValue({
data: [{ following: mockUser }],
error: null,
}),
}),
});

const { getByText } = render(<FollowScreen />);

await waitFor(() =>
expect(supabase.from).toHaveBeenCalledWith('Following')
);
await waitFor(() => expect(getByText('testuser')).toBeTruthy());
});

test('fetchFollowingList should handle error', async () => {
supabase.from.mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockResolvedValue({
data: null,
error: new Error('Fetch error'),
}),
}),
});

render(<FollowScreen />);
await waitFor(() =>
expect(Alert.alert).toHaveBeenCalledWith('Error', 'Fetch error')
);
});

test('handleSearch should fetch search results based on query', async () => {
const { getByPlaceholderText, getByDisplayValue } = render(
<FollowScreen />
);

supabase.from.mockReturnValue({
select: jest.fn().mockReturnValue({
ilike: jest.fn().mockResolvedValue({
data: { mockUser },
error: null,
}),
}),
});

fireEvent.changeText(
getByPlaceholderText('Search by username'),
'testuser'
);
await waitFor(() =>
expect(supabase.from).toHaveBeenCalledWith('Following')
);
await waitFor(() =>
expect(screen.getByDisplayValue('testuser')).toBeTruthy()
);
});

test('fetchFollowingList should handle error', async () => {
supabase.from.mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockResolvedValue({
data: null,
error: new Error('Fetch error'),
}),
}),
});

render(<FollowScreen />);
await waitFor(() =>
expect(Alert.alert).toHaveBeenCalledWith('Error', 'Fetch error')
);
});

test('handleSearch should clear search results for empty query', async () => {
const { getByPlaceholderText, queryByText } = render(<FollowScreen />);

fireEvent.changeText(getByPlaceholderText('Search by username'), 'test');
fireEvent.changeText(getByPlaceholderText('Search by username'), '');

await waitFor(() => expect(queryByText('Search Results')).toBeNull());
});
});
59 changes: 14 additions & 45 deletions qa-tests/Habits.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import { Provider } from 'react-redux'; // Import Provider
import store from '../src/store/storeConfig'; // Import your Redux store
import { Provider } from 'react-redux';
import store from '../src/store/storeConfig';
import Habits from '../src/screens/habits/Habits';
import { supabase } from '../src/config/supabaseClient';

jest.mock('../src/config/supabaseClient', () => ({
supabase: {
Expand All @@ -11,21 +12,19 @@ jest.mock('../src/config/supabaseClient', () => ({
signUp: jest.fn(),
startAutoRefresh: jest.fn(),
stopAutoRefresh: jest.fn(),
signOut: jest.fn(),
},
from: () => ({
select: () => ({
eq: () => ({
data: [
{
habit_id: 'mock_habit_id',
habit_title: 'Mock Habit',
habit_description: 'Mock Habit Description',
},
],
error: null,
}),
from: jest.fn(() => ({
single: jest.fn().mockResolvedValue({
data: {
habit_id: 'test-habit-id',
habit_title: 'Mock Habit',
habit_description: 'Mock Habit Description',
},
}),
}),
single: jest.fn().mockResolvedValue({}),
upsert: jest.fn().mockResolvedValue({}),
})),
},
}));

Expand Down Expand Up @@ -55,34 +54,4 @@ describe('Habits Component', () => {
getByText('My Habits');
getByText('ADD HABIT');
});

// test('renders loading indicator initially', () => {
// const { getByTestId } = render(
// <Provider store={store}>
// <Habits />
// </Provider>
// );
// expect(getByTestId('loading-indicator')).toBeTruthy();
// });

// test('renders habit items after loading', async () => {
// const { getByText } = render(
// <Provider store={store}>
// <Habits />
// </Provider>
// );
// await waitFor(() => {
// getByText('Mock Habit');
// getByText('Mock Habit Description');
// });
// });

// test('navigates to AddHabit screen on button press', () => {
// const { getByText } = render(
// <Provider store={store}>
// <Habits />
// </Provider>
// );
// fireEvent.press(getByText('ADD HABIT'));
// });
});

0 comments on commit 2c2a593

Please sign in to comment.