Skip to content

Commit 31c964d

Browse files
nellysegiianmannnkimcuomoFredAires
committed
jest testing started
Co-authored-by: ianmannn <[email protected]> Co-authored-by: kim <[email protected]> Co-authored-by: FredAires <[email protected]>
1 parent 8eafa09 commit 31c964d

12 files changed

+6216
-1046
lines changed

__tests__/Dashboard.tests.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// __tests__/Dashboard.test.js
2+
import React from 'react';
3+
import '@testing-library/jest-dom'
4+
import { render, screen } from '@testing-library/react';
5+
import Dashboard from '../src/app/dashboard/page';
6+
import withAuth from '../src/app/components/withAuth';
7+
8+
jest.mock('../src/app/dashboard/components/sidebar', () => () => <div>Mocked SideBar</div>);
9+
jest.mock('../src/app/dashboard/components/APIkey', () => () => <div>Mocked APIKey</div>);
10+
jest.mock('../src/app/dashboard/components/WebVitalsContainer', () => () => <div>Mocked WebVitalsContainer</div>);
11+
jest.mock('../src/app/dashboard/components/buildtimecontainer', () => () => <div>Mocked BuildTimeContainer</div>);
12+
13+
// Mocking withAuth higher-order component
14+
jest.mock('../src/app/components/withAuth', () => (Component) => (props) => <Component {...props} />);
15+
16+
describe('Dashboard Component', () => {
17+
const props = {
18+
searchParams: {
19+
username: 'testuser',
20+
},
21+
};
22+
23+
it('renders without crashing', () => {
24+
render(<Dashboard {...props} />);
25+
expect(screen.getByText('Mocked SideBar')).toBeInTheDocument();
26+
expect(screen.getByText('Mocked APIKey')).toBeInTheDocument();
27+
expect(screen.getByText('Mocked WebVitalsContainer')).toBeInTheDocument();
28+
expect(screen.getByText('Mocked BuildTimeContainer')).toBeInTheDocument();
29+
});
30+
31+
it('passes the correct username to the SideBar component', () => {
32+
render(<Dashboard {...props} />);
33+
const sideBarElement = screen.getByText('Mocked SideBar');
34+
expect(sideBarElement).toBeInTheDocument();
35+
});
36+
37+
it('passes the correct username to the APIKey component', () => {
38+
render(<Dashboard {...props} />);
39+
const apiKeyElement = screen.getByText('Mocked APIKey');
40+
expect(apiKeyElement).toBeInTheDocument();
41+
});
42+
43+
it('passes the correct username to the WebVitalsContainer component', () => {
44+
render(<Dashboard {...props} />);
45+
const webVitalsElement = screen.getByText('Mocked WebVitalsContainer');
46+
expect(webVitalsElement).toBeInTheDocument();
47+
});
48+
49+
it('passes the correct username to the BuildTimeContainer component', () => {
50+
render(<Dashboard {...props} />);
51+
const buildTimeElement = screen.getByText('Mocked BuildTimeContainer');
52+
expect(buildTimeElement).toBeInTheDocument();
53+
});
54+
});

__tests__/Homepage.tests.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// __tests__/Home.test.js
2+
import React from 'react';
3+
import { render, screen, act } from '@testing-library/react';
4+
import Home from '../src/app/page';
5+
import '@testing-library/jest-dom';
6+
7+
// Mock next/link
8+
jest.mock('next/link', () => {
9+
return ({ children }) => {
10+
return children;
11+
};
12+
});
13+
14+
// Mock next/image
15+
jest.mock('next/image', () => {
16+
return (props) => {
17+
return <img {...props} />;
18+
};
19+
});
20+
21+
describe('Home Component', () => {
22+
beforeEach(() => {
23+
// Mock the scroll event
24+
global.scrollY = 0;
25+
window.addEventListener = jest.fn();
26+
window.removeEventListener = jest.fn();
27+
});
28+
29+
it('renders without crashing', () => {
30+
render(<Home />);
31+
expect(screen.getByText('Take your application to the')).toBeInTheDocument();
32+
expect(screen.getByText('STEP ONE')).toBeInTheDocument();
33+
expect(screen.getByText('STEP TWO')).toBeInTheDocument();
34+
expect(screen.getByText('STEP THREE')).toBeInTheDocument();
35+
expect(screen.getByText('Tracked Metrics Include :')).toBeInTheDocument();
36+
});
37+
38+
it('adds "show-animate" class on mount', () => {
39+
const { container } = render(<Home />);
40+
const section = container.querySelector('.sec-1');
41+
expect(section).toHaveClass('show-animate');
42+
});
43+
44+
it('adds "show-animate" class on scroll', () => {
45+
const { container } = render(<Home />);
46+
const sections = container.querySelectorAll('section');
47+
48+
act(() => {
49+
window.scrollY = 400;
50+
window.dispatchEvent(new Event('scroll'));
51+
});
52+
53+
sections.forEach((section) => {
54+
expect(section).toHaveClass('show-animate');
55+
});
56+
});
57+
58+
it('removes event listener on unmount', () => {
59+
const { unmount } = render(<Home />);
60+
unmount();
61+
expect(window.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function));
62+
});
63+
});

__tests__/Login.tests.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import '@testing-library/jest-dom';
2+
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3+
import { signIn, useSession } from 'next-auth/react';
4+
import Login from '../src/app/login/page.js';
5+
import Spinner from '../src/app/components/Spinner.js';
6+
import React from 'react';
7+
8+
// Mock the necessary components and functions
9+
jest.mock('next-auth/react');
10+
jest.mock('../src/app/components/Spinner.js', () => () => <div>Loading...</div>);
11+
jest.mock('next/image', () => (props) => <img {...props} />);
12+
13+
describe('Login', () => {
14+
const mockSignIn = jest.fn();
15+
const mockUseSession = useSession;
16+
17+
beforeEach(() => {
18+
mockUseSession.mockReturnValue({
19+
data: null,
20+
status: 'unauthenticated'
21+
});
22+
signIn.mockImplementation(mockSignIn);
23+
});
24+
25+
afterEach(() => {
26+
jest.clearAllMocks();
27+
});
28+
29+
it('renders the login form', async () => {
30+
render(<Login initialLoading={false} />);
31+
32+
await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
33+
34+
expect(screen.getByPlaceholderText('Username')).toBeInTheDocument();
35+
expect(screen.getByPlaceholderText('Password')).toBeInTheDocument();
36+
expect(screen.getByText('Login')).toBeInTheDocument();
37+
expect(screen.getByText("Don't have an account?")).toBeInTheDocument();
38+
});
39+
40+
it('displays error message on login failure', async () => {
41+
mockSignIn.mockResolvedValueOnce({ error: 'Invalid credentials' });
42+
render(<Login initialLoading={false} />);
43+
44+
await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
45+
46+
fireEvent.change(screen.getByPlaceholderText('Username'), { target: { value: 'testuser' } });
47+
fireEvent.change(screen.getByPlaceholderText('Password'), { target: { value: 'password' } });
48+
fireEvent.click(screen.getByText('Login'));
49+
50+
await waitFor(() => expect(screen.getByText('Invalid credentials')).toBeInTheDocument());
51+
});
52+
53+
it('redirects to dashboard on successful login', async () => {
54+
// Mock window.location.href
55+
delete window.location;
56+
window.location = { href: '' };
57+
58+
mockSignIn.mockResolvedValueOnce({ error: null });
59+
render(<Login initialLoading={false} />);
60+
61+
await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
62+
63+
fireEvent.change(screen.getByPlaceholderText('Username'), { target: { value: 'testuser' } });
64+
fireEvent.change(screen.getByPlaceholderText('Password'), { target: { value: 'password' } });
65+
fireEvent.click(screen.getByText('Login'));
66+
67+
await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument());
68+
69+
await waitFor(() => expect(window.location.href).toBe('/dashboard/?username=testuser'));
70+
});
71+
72+
it('renders the spinner when loading', () => {
73+
render(<Login />);
74+
expect(screen.getByText('Loading...')).toBeInTheDocument();
75+
});
76+
77+
it('handles OAuth sign-in error', async () => {
78+
mockSignIn.mockResolvedValueOnce({ error: 'OAuth error' });
79+
render(<Login initialLoading={false} />);
80+
81+
await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
82+
83+
// Changed the selector to use aria-label
84+
fireEvent.click(screen.getByRole('button', { name: 'Sign in with Google' }));
85+
86+
await waitFor(() => expect(screen.getByText('Failed to sign in with google. Please try again.')).toBeInTheDocument());
87+
});
88+
89+
it('redirects to dashboard on successful OAuth sign-in', async () => {
90+
// Mock window.location.href
91+
delete window.location;
92+
window.location = { href: '' };
93+
94+
mockUseSession.mockReturnValue({
95+
data: { user: { email: 'testuser' } },
96+
status: 'authenticated',
97+
});
98+
99+
render(<Login initialLoading={false} />);
100+
101+
await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
102+
103+
// Changed the selector to use aria-label
104+
fireEvent.click(screen.getByRole('button', { name: 'Sign in with Google' }));
105+
106+
await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument());
107+
108+
await waitFor(() => expect(window.location.href).toBe('/dashboard/?username=testuser'));
109+
});
110+
111+
it('applies correct styles on mount and cleans up on unmount', () => {
112+
const { unmount } = render(<Login initialLoading={false} />);
113+
const bodyStyles = document.body.style;
114+
115+
expect(bodyStyles.fontFamily).toBe("'Poppins', sans-serif");
116+
expect(bodyStyles.display).toBe('flex');
117+
expect(bodyStyles.justifyContent).toBe('center');
118+
expect(bodyStyles.alignItems).toBe('center');
119+
expect(bodyStyles.minHeight).toBe('100vh');
120+
expect(bodyStyles.backgroundImage).toContain('https://getwallpapers.com/wallpaper/full/2/8/f/537844.jpg');
121+
expect(bodyStyles.backgroundRepeat).toBe('no-repeat');
122+
expect(bodyStyles.backgroundSize).toBe('cover');
123+
expect(bodyStyles.backgroundPosition).toBe('center');
124+
expect(bodyStyles.color).toBe('rgb(255, 255, 255)');
125+
126+
unmount();
127+
128+
expect(bodyStyles.fontFamily).toBe('');
129+
expect(bodyStyles.display).toBe('');
130+
expect(bodyStyles.justifyContent).toBe('');
131+
expect(bodyStyles.alignItems).toBe('');
132+
expect(bodyStyles.minHeight).toBe('');
133+
expect(bodyStyles.backgroundImage).toBe('');
134+
expect(bodyStyles.backgroundRepeat).toBe('no-repeat'); // Changed to check if 'no-repeat' is retained
135+
expect(bodyStyles.backgroundSize).toBe('auto');
136+
expect(bodyStyles.backgroundPosition).toBe('0% 0%');
137+
expect(bodyStyles.color).toBe('');
138+
});
139+
});
140+
141+
142+
143+
144+
145+
146+
147+

0 commit comments

Comments
 (0)