-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from bounswe/feature/MB-post-stock-connection…
…-and-graphics Feature/mb post stock connection and graphics
- Loading branch information
Showing
12 changed files
with
641 additions
and
99 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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, act } from '@testing-library/react-native'; | ||
import Community from '../src/pages/Community'; | ||
import { useAuth } from '../src/pages/context/AuthContext'; | ||
import { NavigationContainer } from '@react-navigation/native'; | ||
|
||
// Mock the useAuth hook | ||
jest.mock('../src/pages/context/AuthContext', () => ({ | ||
useAuth: jest.fn(), | ||
})); | ||
|
||
jest.mock('../src/pages/config/config', () => ({ | ||
default: { baseURL: 'http://localhost:3000' }, | ||
})); | ||
|
||
global.fetch = jest.fn(); | ||
|
||
const mockNavigation = { | ||
navigate: jest.fn(), | ||
}; | ||
|
||
describe('Community Component', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
useAuth.mockReturnValue({ | ||
userId: 1, | ||
accessToken: 'mock-access-token', | ||
}); | ||
}); | ||
|
||
test('renders correctly', () => { | ||
const { getByTestId, getByPlaceholderText, getByText } = render( | ||
<NavigationContainer> | ||
<Community navigation={mockNavigation} /> | ||
</NavigationContainer> | ||
); | ||
|
||
expect(getByText('Community')).toBeTruthy(); | ||
expect(getByText('Create A Post')).toBeTruthy(); | ||
expect(getByPlaceholderText('Search posts...')).toBeTruthy(); | ||
}); | ||
|
||
test('fetches posts and users on focus', async () => { | ||
fetch.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve([ | ||
{ id: 1, title: 'Test Post', author: 1, content: 'Test Content', tags: [], liked_by: [], comments: 0 }, | ||
]), | ||
}) | ||
); | ||
|
||
fetch.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve([ | ||
{ id: 1, username: 'TestUser' }, | ||
]), | ||
}) | ||
); | ||
|
||
const { getByText } = render( | ||
<NavigationContainer> | ||
<Community navigation={mockNavigation} /> | ||
</NavigationContainer> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Test Post')).toBeTruthy(); | ||
expect(getByText('Test Content')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
test('navigates to CreatePost screen when "Create A Post" is clicked', () => { | ||
const { getByText } = render( | ||
<NavigationContainer> | ||
<Community navigation={mockNavigation} /> | ||
</NavigationContainer> | ||
); | ||
|
||
fireEvent.press(getByText('Create A Post')); | ||
|
||
expect(mockNavigation.navigate).toHaveBeenCalledWith('CreatePost'); | ||
}); | ||
|
||
test('alerts and navigates to Login when user is not logged in and tries to create a post', () => { | ||
useAuth.mockReturnValueOnce({ | ||
userId: null, | ||
accessToken: null, | ||
}); | ||
|
||
const { getByText } = render( | ||
<NavigationContainer> | ||
<Community navigation={mockNavigation} /> | ||
</NavigationContainer> | ||
); | ||
|
||
fireEvent.press(getByText('Create A Post')); | ||
|
||
expect(mockNavigation.navigate).toHaveBeenCalledWith('Login&Register'); | ||
}); | ||
|
||
test('handles adding a comment correctly', async () => { | ||
fetch.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({}), | ||
}) | ||
); | ||
|
||
const { getByTestId, getByPlaceholderText, getByText } = render( | ||
<NavigationContainer> | ||
<Community navigation={mockNavigation} /> | ||
</NavigationContainer> | ||
); | ||
|
||
|
||
fireEvent.press(getByTestId('add-comment-button')); | ||
fireEvent.changeText(getByPlaceholderText('Write a comment...'), 'This is a test comment'); | ||
fireEvent.press(getByText('Submit')); | ||
|
||
|
||
|
||
await waitFor(() => { | ||
expect(fetch).toHaveBeenCalledWith( | ||
'http://localhost:3000/comments/', | ||
expect.objectContaining({ | ||
method: 'POST', | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import Login from '../src/pages/Login'; | ||
import { AuthProvider } from '../src/pages/context/AuthContext'; | ||
|
||
test('renders correctly', () => { | ||
const tree = renderer.create(<Login />).toJSON(); | ||
const tree = renderer.create( | ||
<AuthProvider> | ||
<Login /> | ||
</AuthProvider> | ||
).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import Profile from '../src/pages/Profile'; | ||
import Profile from '../src/pages/ProfilePage'; | ||
|
||
// Mock the useAuth hook | ||
jest.mock('../src/pages/context/AuthContext', () => ({ | ||
useAuth: () => ({ | ||
username: 'testuser', | ||
userId: '12345', | ||
logout: jest.fn(), | ||
}), | ||
})); | ||
|
||
test('renders correctly', () => { | ||
const tree = renderer.create(<Profile />).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
const tree = renderer.create(<Profile />).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Oops, something went wrong.