Skip to content
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
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default tseslint.config(
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
...jsxA11y.configs.recommended.rules,
'no-console': ['error', { allow: ['error'] }],
'no-empty': 'off',
'no-empty': 'warn',
'no-warning-comments': ['error', { terms: [''], location: 'anywhere' }],
'quote-props': ['error', 'always'],
'prefer-const': 'error',
Expand Down
56 changes: 56 additions & 0 deletions src/app/[locale]/(auth)/login/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { render, screen } from '@testing-library/react';

import type { SecondaryAction } from '@/types/types';

import LoginPage from './page';

const mockTranslations = (key: string) => {
const translations: Record<string, string> = {
login: 'Login',
haveAccount: "Don't have an account?",
signUp: 'Sign Up',
};

return translations[key] || key;
};

vi.mock('next-intl', () => ({
useTranslations: vi.fn(() => mockTranslations),
}));

vi.mock('@/config/routes', () => ({
ROUTES: {
SIGN_UP: '/sign-up',
},
}));

vi.mock('@/features/auth-form/components/auth-form', () => ({
AuthForm: ({ heading, secondaryAction }: { heading: string; secondaryAction: SecondaryAction }) => (
<div data-testid="auth-form">
<h1>{heading}</h1>
<div data-testid="secondary-action">
<span>{secondaryAction.intro}</span>
<a href={secondaryAction.link}>{secondaryAction.linkText}</a>
</div>
</div>
),
}));

describe('LoginPage', () => {
it('renders AuthForm with correct heading', () => {
render(<LoginPage />);

expect(screen.getByTestId('auth-form')).toBeInTheDocument();
expect(screen.getByText('Login')).toBeInTheDocument();
});

it('renders secondary action with correct props', () => {
render(<LoginPage />);

const secondaryAction = screen.getByTestId('secondary-action');

expect(secondaryAction).toBeInTheDocument();
expect(screen.getByText("Don't have an account?")).toBeInTheDocument();
expect(screen.getByText('Sign Up')).toBeInTheDocument();
});
});
54 changes: 54 additions & 0 deletions src/app/[locale]/(auth)/sign-up/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { render, screen } from '@testing-library/react';

import type { SecondaryAction } from '@/types/types';

import SignUpPage from './page';

const mockTranslations = (key: string) => {
const translations: Record<string, string> = {
login: 'Login',
haveAccount: "Don't have an account?",
alreadyHaveAccount: 'Already have an account?',
signUp: 'Sign Up',
};

return translations[key] || key;
};

vi.mock('next-intl', () => ({
useTranslations: vi.fn(() => mockTranslations),
}));

vi.mock('@/config/routes', () => ({
ROUTES: {
SIGN_UP: '/sign-up',
},
}));

vi.mock('@/features/auth-form/components/auth-form', () => ({
AuthForm: ({ heading, secondaryAction }: { heading: string; secondaryAction: SecondaryAction }) => (
<div data-testid="auth-form">
<h1>{heading}</h1>
<div data-testid="secondary-action">
<span>{secondaryAction.intro}</span>
<a href={secondaryAction.link}>{secondaryAction.linkText}</a>
</div>
</div>
),
}));

describe('SignUpPage', () => {
it('renders AuthForm with correct heading', () => {
render(<SignUpPage />);
expect(screen.getByTestId('auth-form')).toBeInTheDocument();
expect(screen.getByText('Sign Up')).toBeInTheDocument();
});
it('renders secondary action with correct props', () => {
render(<SignUpPage />);
const secondaryAction = screen.getByTestId('secondary-action');

expect(secondaryAction).toBeInTheDocument();
expect(screen.getByText('Already have an account?')).toBeInTheDocument();
expect(screen.getByText('Login')).toBeInTheDocument();
});
});
39 changes: 39 additions & 0 deletions src/app/[locale]/(dashboard)/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { render, screen } from '@testing-library/react';

import HomePage from './page';

vi.mock('next-intl/server', () => ({
getLocale: vi.fn(() => Promise.resolve('en')),
}));

vi.mock('@/features/home-screen/utils/get-day-of-week', () => ({
getDayOfWeek: vi.fn((_locale: string) => 'Monday'),
}));

vi.mock('@/features/home-screen/greeting', () => ({
Greeting: ({ dayOfWeek }: { dayOfWeek: string }) => <div data-testid="greeting">Hello, today is {dayOfWeek}</div>,
}));

vi.mock('@/features/home-screen/home-screen', () => ({
HomeScreen: () => <div data-testid="home-screen">Home Screen Content</div>,
}));

describe('HomePage', () => {
it('renders Greeting component with day of week', async () => {
render(await HomePage());

const greeting = screen.getByTestId('greeting');

expect(greeting).toBeInTheDocument();
expect(greeting).toHaveTextContent('Hello, today is Monday');
});

it('renders HomeScreen component', async () => {
render(await HomePage());

const homeScreen = screen.getByTestId('home-screen');

expect(homeScreen).toBeInTheDocument();
expect(homeScreen).toHaveTextContent('Home Screen Content');
});
});
35 changes: 35 additions & 0 deletions src/app/layout.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { render, screen } from '@testing-library/react';

import RootLayout from './layout';

describe('RootLayout', () => {
it('renders children correctly', () => {
const testContent = 'Test content';

render(
<RootLayout>
<div>{testContent}</div>
</RootLayout>
);

expect(screen.getByText(testContent)).toBeInTheDocument();
});

it('passes through multiple children', () => {
render(
<RootLayout>
<div>First child</div>
<div>Second child</div>
</RootLayout>
);

expect(screen.getByText('First child')).toBeInTheDocument();
expect(screen.getByText('Second child')).toBeInTheDocument();
});

it('handles empty children', () => {
const { container } = render(<RootLayout>{null}</RootLayout>);

expect(container).toBeInTheDocument();
});
Comment on lines +30 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The assertion expect(container).toBeInTheDocument() is not very meaningful as it will always pass for a rendered component. To make this test more robust, you should assert that the container has no child nodes when null is passed as children.

Suggested change
it('handles empty children', () => {
const { container } = render(<RootLayout>{null}</RootLayout>);
expect(container).toBeInTheDocument();
});
it('handles empty children', () => {
const { container } = render(<RootLayout>{null}</RootLayout>);
expect(container.firstChild).toBeNull();
});

});
29 changes: 29 additions & 0 deletions src/app/not-found.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { render, screen } from '@testing-library/react';

import { renderWithProviders } from '@/testing/utils/render-with-providers';

import GlobalNotFound from './not-found';

vi.mock('@/app/[locale]/_components/root-pages-layout', () => ({
RootPagesLayout: ({ children }: { children: React.ReactNode }) => (
<div data-testid="root-pages-layout">{children}</div>
),
}));

describe('GlobalNotFound', () => {
it('renders html with correct lang attribute', () => {
renderWithProviders(<GlobalNotFound />);
const htmlElement = document.documentElement;

expect(htmlElement).toHaveAttribute('lang', 'en');
});

it('renders RootPagesLayout and NotFoundComponent with correct props', () => {
render(<GlobalNotFound />);

expect(screen.getByTestId('root-pages-layout')).toBeInTheDocument();
expect(screen.getByText('Page Not Found')).toBeInTheDocument();
expect(screen.getByText('global non localized')).toBeInTheDocument();
expect(screen.getByText('Go Home')).toBeInTheDocument();
});
});
25 changes: 25 additions & 0 deletions src/testing/setup-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import { beforeAll, afterEach, afterAll } from 'vitest';

import { server } from '@/testing/mocks/server';

vi.mock('next/navigation', () => ({
useRouter: () => ({
push: vi.fn(),
replace: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
refresh: vi.fn(),
prefetch: vi.fn(),
}),
useSearchParams: () => new URLSearchParams(),
usePathname: () => '/',
notFound: vi.fn(),
redirect: vi.fn(),
permanentRedirect: vi.fn(),
}));

vi.mock('next/headers', () => ({
cookies: () => ({
get: vi.fn(),
set: vi.fn(),
delete: vi.fn(),
getAll: vi.fn(() => []),
}),
}));

afterEach(() => {
cleanup();
vi.clearAllMocks();
Expand Down
4 changes: 3 additions & 1 deletion src/utils/supabase/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export async function createClient() {
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
} catch {}
} catch (error) {
console.error('Failed to set cookies:', error);
}
},
},
}
Expand Down
1 change: 0 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default defineConfig({
'src/i18n/**/*',
'**/*/fonts.ts',
'src/utils/supabase/**/*',
'src/app/**/*',
],
},
},
Expand Down