Skip to content

Commit

Permalink
Writing tests and fixing erors
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaBadarawada committed Jun 12, 2024
1 parent a564937 commit 37a98f4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 42 deletions.
126 changes: 89 additions & 37 deletions src/__tests__/components/CommentsSection.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import React from "react";
import {render,fireEvent } from "@testing-library/react-native";
import {render,fireEvent,waitFor } from "@testing-library/react-native";
import { CommentsSection } from "../../components/ChartCard/ChatComments/CommentsSection";
import userLevelStore from "../../store/userLevel";
import { Role } from "../../utility/enums";

jest.mock("../../components/ChartCard/ChatCardEditMenu", () => () => {
return <mockChatCardEditMenu />;
});


jest.mock("@react-native-firebase/firestore", () => {
return jest.fn();
});

jest.mock('@react-native-firebase/storage', () => {
return {
ref: jest.fn(() => ({
Expand All @@ -20,32 +17,29 @@ jest.mock("@react-native-firebase/firestore", () => {
})),
};
});

jest.mock("@react-native-firebase/crashlytics", () => () => ({
crashlytics: jest.fn(),
}));

jest.mock("../../context/SuperAdminContext", () => ({
useSuperAdminFunction: () => ({
setGetAllUsers: jest.fn(),
userLevel: jest.fn(),
}),
}));


jest.mock("@react-native-firebase/crashlytics", () => () => ({
crashlytics: jest.fn(),
}));
jest.mock("../../components/ChartCard/ChatCardEditMenu", () => {
return ({ textWhatItemToDelete, onDeletePress }) => (
<mockChatCardEditMenu
textWhatItemToDelete={textWhatItemToDelete}
onDeletePress={onDeletePress}
/>
);
});

const mockAddComment = jest.fn();
const mockDeleteComment = jest.fn();

const superAdmin = () => {
userLevelStore.setUserLevel(Role.superadmin);
};

const user = () => {
userLevelStore.setUserLevel(Role.user);
};

const loggedInUser = {
id: "user1",
activitiesAndAccumulatedTime: [],
Expand All @@ -54,25 +48,24 @@ const loggedInUser = {
lastName: "Andersson",
statusActive: true
};

constdifferentUser = {
id: "user3",
activitiesAndAccumulatedTime: [],
connectedActivities: [],
firstName: "Jane",
lastName: "Smith",
statusActive: true
const anotherUser = {
id: "user4",
activitiesAndAccumulatedTime: [],
connectedActivities: [],
firstName: "Alex",
lastName: "Johnson",
statusActive: true
};



const comments = [
{ comment: 'First comment', userID: '2', userFirstName: 'John', userLastName: 'Doe' },
{ comment: 'Second comment', userID: '3', userFirstName: 'Jane', userLastName: 'Smith' },
{ comment: 'Third comment', userID: '1', userFirstName: 'Erik', userLastName: 'Andersson' }
{ comment: 'First comment', userID: 'user2', userFirstName: 'John', userLastName: 'Doe' },
{ comment: 'Second comment', userID: 'user3', userFirstName: 'Jane', userLastName: 'Smith' },
{ comment: 'Third comment', userID: 'user1', userFirstName: 'Erik', userLastName: 'Andersson' }
];


describe("Testing CommentsSection Component", () => {
user()
it('renders comments correctly', () => {
const { getByText } = render(
<CommentsSection
Expand All @@ -88,6 +81,8 @@ describe("Testing CommentsSection Component", () => {
expect(getByText('First comment')).toBeTruthy();
expect(getByText('Jane Smith')).toBeTruthy();
expect(getByText('Second comment')).toBeTruthy();
expect(getByText('Erik Andersson')).toBeTruthy();
expect(getByText('Third comment')).toBeTruthy();
});

it('should add a comment', () => {
Expand All @@ -114,18 +109,75 @@ describe("Testing CommentsSection Component", () => {
});
});

it('does not allow logged in user to see delete menu for others\' comments', () => {
const { queryByText } = render(
it('shows delete option only for logged in user comment', () => {
user()
const { getByTestId,getByText} = render(
<CommentsSection
comments={comments}
addComment={mockAddComment}
deleteComment={mockDeleteComment}
loggedInUser={loggedInUser}
postID="post1"
/>
);
const editMenu = getByTestId('chat-card-edit-menu');
expect(editMenu).toBeTruthy();
});

it('Superadmin can delete any comment', () => {
superAdmin();
const { getAllByTestId} = render(
<CommentsSection
comments={comments}
addComment={mockAddComment}
deleteComment={mockDeleteComment}
loggedInUser={loggedInUser}
postID="post1"
/>
);
const editMenu = getAllByTestId('chat-card-edit-menu');
expect(editMenu).toHaveLength(3);
});
it('Delete option not visible for others comments', () => {
user();
const { queryByTestId } = render(
<CommentsSection
comments={comments}
addComment={mockAddComment}
deleteComment={mockDeleteComment}
loggedInUser={differentUser}
loggedInUser={anotherUser}
postID="post1"
/>
);

const editMenu = queryByTestId('chat-card-edit-menu');
expect(editMenu).toBeFalsy();
});
it('Delete a comment', async () => {
user();
const { getByTestId, getByText,debug } = render(
<CommentsSection
comments={comments}
addComment={mockAddComment}
deleteComment={mockDeleteComment}
loggedInUser={loggedInUser}
postID="post1"
/>
);

const deleteMenu = getByTestId('chat-card-edit-menu');
fireEvent.press(deleteMenu);
const deleteButton = getByText("Ta bort");
fireEvent.press(deleteButton);
expect(mockDeleteComment).toBeTruthy();
await waitFor(() => {
expect(mockDeleteComment).toHaveBeenCalledWith({
comment: 'Third comment',
userID: loggedInUser.id,
userFirstName: loggedInUser.firstName,
userLastName: loggedInUser.lastName,
}, 'post1');
});
});

expect(get('...')).toBeFalsy();
});

});
5 changes: 3 additions & 2 deletions src/components/ChartCard/EmojiList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ const styles = StyleSheet.create({
},
modalContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
justifyContent: 'center',
alignItems: 'center',
marginTop: "60%",
},
modalContent: {
backgroundColor:colors.background,
Expand Down
7 changes: 4 additions & 3 deletions src/screens/Chat/useUserPostsActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import { AlertQuestion } from "../../components/Alerts/AlertQuestion ";
import { updatePostInFirestore } from "../../firebase-functions/updateTS/update";

const alertMessage =
"Vill du publicera det här inlägget i chatten? Alla DGGG-användare kommer att se detta inlägg.\n\
Den här inlägg raderas automatiskt efter ett år.";
const alertUpdateMessage= "Vill du spara ändringarna."
"Vill du publicera det här inlägget i chatten? Alla DGGG-användare kommer att se detta inlägg. nlägget raderas automatiskt efter ett år.";


const alertUpdateMessage= "Vill du spara ändringarna."

export const useUserPostsActions = () => {
const [loading, setLoading] = useState(false);
Expand Down

0 comments on commit 37a98f4

Please sign in to comment.