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

Dev to Main sync #1273

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 31 additions & 5 deletions __tests__/Unit/Components/Navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { screen, render, fireEvent } from '@testing-library/react';
import { screen, render, fireEvent, waitFor } from '@testing-library/react';
import NavBar from '../../../../src/components/navBar/index';
import * as authHooks from '@/hooks/useAuthenticated';
import { renderWithProviders } from '@/test-utils/renderWithProvider';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import handlers from '../../../../__mocks__/handlers';

const server = setupServer(...handlers);
Expand All @@ -15,11 +16,36 @@ afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('Navbar', () => {
test('user whether loggedIn or not', async () => {
test('shows skeleton loader first, then displays user info if logged in', async () => {
renderWithProviders(<NavBar />);
const navbar = await screen.findByTestId('navbar');
expect(navbar).toBeInTheDocument();
expect(screen.getByText('Sign In With Github'));
const loadingSkeleton = screen.getByTestId('loading-skeleton');
expect(loadingSkeleton).toBeInTheDocument();

await waitFor(() => {
const userGreet = screen.getByText(/Hello, Mahima/i);
expect(userGreet).toBeInTheDocument();
});
});

test('shows "Sign In With Github" button when not logged in', async () => {
server.use(
rest.get(
`${process.env.NEXT_PUBLIC_BASE_URL}/users/self`,
(_, res, ctx) => {
return res(
ctx.status(401),
ctx.json({ error: 'Not Authenticated' })
);
}
)
);
renderWithProviders(<NavBar />);
await waitFor(() => {
const signInButton = screen.getByRole('button', {
name: /Sign In With Github/i,
});
expect(signInButton).toBeInTheDocument();
});
});

test('renders the hamburger icon', async () => {
Expand Down
19 changes: 16 additions & 3 deletions src/components/navBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import Link from 'next/link';
import Image from 'next/image';

Expand All @@ -20,11 +20,16 @@ import useAuthenticated from '@/hooks/useAuthenticated';

const NavBar = () => {
const { isLoggedIn } = useAuthenticated();
const [isProfileLoading, setIsProfileLoading] = useState(true);

const [toggleDropdown, setToggleDropdown] = useState(false);
const [showMenu, setShowMenu] = useState(false);

const { data: userData } = useUserData();
const { data: userData, isLoading } = useUserData();

useEffect(() => {
setIsProfileLoading(isLoading);
}, [isLoading]);

return (
<nav data-testid="navbar" className={styles.navBar}>
Expand Down Expand Up @@ -77,7 +82,15 @@ const NavBar = () => {
</ul>
</div>
<div className={styles.userProfile}>
{!isLoggedIn ? (
{isProfileLoading ? (
<div
data-testid="loading-skeleton"
className={styles.skeletonContainer}
>
<div className={styles.skeletonGreetMsg}></div>
<div className={styles.skeletonProfilePic}></div>
</div>
) : !isLoggedIn ? (
<Link href={LOGIN_URL}>
<button className={styles.signInLink}>
Sign In With Github
Expand Down
34 changes: 34 additions & 0 deletions src/components/navBar/navBar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ $thickness: 3px;
color: $white;
font-weight: 600;
}
.skeletonContainer {
display: flex;
align-items: center;
gap: 0.625rem;
padding: ($offset + $thickness) + 7;
}

.skeletonGreetMsg {
width: 100px;
height: 20px;
background-color: lighten($theme-primary, 20%);
border-radius: 4px;
animation: skeleton-loading 1.2s infinite ease-in-out;
}

.skeletonProfilePic {
width: 32px;
height: 32px;
background-color: lighten($theme-primary, 20%);
border-radius: 50%;
animation: skeleton-loading 1.2s infinite ease-in-out;
}

@keyframes skeleton-loading {
0% {
background-color: lighten($theme-primary, 20%);
}
50% {
background-color: lighten($theme-primary, 30%);
}
100% {
background-color: lighten($theme-primary, 20%);
}
}

.hamburgerIcon {
display: none;
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useUserData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useGetUserQuery } from '@/app/services/userApi';

const useUserData = () => {
const { data, isSuccess } = useGetUserQuery();
const { data, isSuccess, isLoading } = useGetUserQuery();
const adminData = data?.roles.admin;
const superUserData = data?.roles.super_user;
const isUserAuthorized = !!adminData || !!superUserData;
return { data, isUserAuthorized, isSuccess };
return { data, isUserAuthorized, isSuccess, isLoading };
};

export default useUserData;
Loading