Skip to content

Commit

Permalink
Merge pull request #37 from atlp-rwanda/fix-notification
Browse files Browse the repository at this point in the history
Fix : extended notification component
  • Loading branch information
teerenzo authored Aug 1, 2024
2 parents 5308eaf + 07ea21d commit 6d43403
Show file tree
Hide file tree
Showing 17 changed files with 585 additions and 73 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)$":
"<rootDir>/src/__test__/__mock__/fileMock.ts",
"\\.(css|less)$": "identity-obj-proxy",
"~src/(.*)": "<rootDir>/src/$1",
},
};
46 changes: 15 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"gsap": "^3.12.5",
"install": "^0.13.0",
"jest-environment-jsdom": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"jest-mock-extended": "^3.0.7",
"jwt-decode": "^4.0.0",
"moment": "^2.30.1",
Expand Down Expand Up @@ -70,6 +69,7 @@
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@testing-library/dom": "^10.2.0",
"@testing-library/jest-dom": "^6.4.8",
"@types/jest": "^29.5.12",
"@types/jwt-decode": "^3.1.0",
"@types/node": "^20.14.8",
Expand Down Expand Up @@ -98,6 +98,7 @@
"husky": "^8.0.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"lint-staged": "^15.2.5",
"msw": "^2.3.1",
"postcss": "^8.4.38",
Expand Down
13 changes: 10 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
disconnectFromSocket,
getSocket,
} from "./utils/socket";
import { getUserNotifications } from "./redux/reducers/notificationSlice";
import {
getUserNotifications,
handleCurrentUser,
} from "./redux/reducers/notificationSlice";
import UpdatePasswordmod from "./components/password/updateModal";
import PasswordPopup from "./components/password/PasswordPopup";

Expand All @@ -29,14 +32,18 @@ const App: React.FC = () => {
};

initializeSocket();
dispatch(getUserNotifications());

return () => {
disconnectFromSocket();
};
}, [dispatch]);

React.useEffect(() => {
dispatch(getUserNotifications());
const token = localStorage.getItem("accessToken");
if (token) {
dispatch(handleCurrentUser());
dispatch(getUserNotifications());
}
}, [dispatch]);

const { isPasswordExpired } = useAppSelector((state) => state.updatePin);
Expand Down
41 changes: 41 additions & 0 deletions src/__test__/currentUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import api from "../redux/api/api";
import { ICurrentUser } from "../redux/reducers/notificationSlice";
import { getCurrentUser } from "../utils/currentuser";

jest.mock("../redux/api/api");

const mockUser: ICurrentUser = {
id: 1,
name: "John Doe",
username: "johndoe",
email: "[email protected]",
password: "securepassword",
lastPasswordUpdateTime: new Date("2023-01-01T00:00:00Z"),
roleId: 2,
isActive: true,
isVerified: true,
createdAt: new Date("2022-01-01T00:00:00Z"),
updatedAt: new Date("2023-01-01T00:00:00Z"),
};

describe("getCurrentUser", () => {
beforeEach(() => {
(api.get as jest.Mock).mockReset();
});

it("should return user data when API call is successful", async () => {
(api.get as jest.Mock).mockResolvedValue({ data: mockUser });

const result = await getCurrentUser();

expect(result).toEqual(mockUser);
});

it("should return null when API call fails", async () => {
(api.get as jest.Mock).mockRejectedValue(new Error("Network Error"));

const result = await getCurrentUser();

expect(result).toBeNull();
});
});
14 changes: 5 additions & 9 deletions src/__test__/notificationSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("notificationSlice", () => {
expect(state.unreadCount).toBe(1);
});

it.skip("should handle getUserNotifications thunk", async () => {
it("should handle getUserNotifications thunk", async () => {
const mockNotifications: INotificationR[] = [
{
id: 1,
Expand All @@ -113,17 +113,13 @@ describe("notificationSlice", () => {
data: { notifications: mockNotifications },
});

(connectSocketMock as jest.Mock).mockReturnValue(socketMock);

await store.dispatch(getUserNotifications());
const result = await store.dispatch(getUserNotifications());
console.log("Thunk result:", result);

const state = store.getState().notifications;
console.log("State after getUserNotifications:", state);
// expect(state.notifications).toEqual(mockNotifications);
expect(state.unreadCount).toBe(1);

expect(socketMock.emit).not.toHaveBeenCalled();
expect(socketMock.on).not.toHaveBeenCalled();
expect(state.notifications).toEqual(mockNotifications);
expect(state.unreadCount).toBe(2);
});

it("should handle readNotification thunk", async () => {
Expand Down
Loading

0 comments on commit 6d43403

Please sign in to comment.