forked from cardosofelipe/fast-next-template
Add tests for auth storage logic and i18n routing configuration
- Added comprehensive unit tests for `auth/storage` to handle SSR, E2E paths, storage method selection, and error handling. - Introduced tests for `i18n/routing` to validate locale configuration, navigation hooks, and link preservation. - Updated Jest coverage exclusions to include `
This commit is contained in:
@@ -213,4 +213,104 @@ describe('Storage Module', () => {
|
||||
await expect(clearTokens()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('SSR and guards', () => {
|
||||
const originalLocalStorage = global.localStorage;
|
||||
const originalWindow = global.window;
|
||||
|
||||
afterEach(() => {
|
||||
// Restore globals
|
||||
(global as any).window = originalWindow;
|
||||
(global as any).localStorage = originalLocalStorage;
|
||||
});
|
||||
|
||||
it('returns false on isStorageAvailable and null on getTokens when localStorage is unavailable', async () => {
|
||||
// Simulate SSR/unavailable localStorage by shadowing the global getter
|
||||
const descriptor = Object.getOwnPropertyDescriptor(global, 'localStorage');
|
||||
Object.defineProperty(global, 'localStorage', { value: undefined, configurable: true });
|
||||
|
||||
expect(isStorageAvailable()).toBe(false);
|
||||
await expect(getTokens()).resolves.toBeNull();
|
||||
|
||||
// Restore descriptor
|
||||
if (descriptor) Object.defineProperty(global, 'localStorage', descriptor);
|
||||
});
|
||||
|
||||
it('saveTokens throws when localStorage is unavailable', async () => {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(global, 'localStorage');
|
||||
Object.defineProperty(global, 'localStorage', { value: undefined, configurable: true });
|
||||
|
||||
await expect(
|
||||
saveTokens({ accessToken: 'a', refreshToken: 'r' })
|
||||
).rejects.toThrow('localStorage not available - cannot save tokens');
|
||||
|
||||
if (descriptor) Object.defineProperty(global, 'localStorage', descriptor);
|
||||
});
|
||||
});
|
||||
|
||||
describe('E2E mode path (skip encryption)', () => {
|
||||
const originalFlag = (global.window as any).__PLAYWRIGHT_TEST__;
|
||||
|
||||
beforeEach(() => {
|
||||
(global.window as any).__PLAYWRIGHT_TEST__ = true;
|
||||
localStorage.clear();
|
||||
clearEncryptionKey();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
(global.window as any).__PLAYWRIGHT_TEST__ = originalFlag;
|
||||
});
|
||||
|
||||
it('stores plain JSON and retrieves it when E2E flag is set', async () => {
|
||||
const tokens = { accessToken: 'plainA', refreshToken: 'plainR' };
|
||||
await saveTokens(tokens);
|
||||
|
||||
// Verify plain JSON persisted
|
||||
const raw = localStorage.getItem('auth_tokens');
|
||||
expect(raw).toContain('plainA');
|
||||
|
||||
const retrieved = await getTokens();
|
||||
expect(retrieved).toEqual(tokens);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Storage method selection and cookie mode', () => {
|
||||
it('falls back to localStorage when invalid method is stored', () => {
|
||||
localStorage.setItem('auth_storage_method', 'invalid');
|
||||
expect(getStorageMethod()).toBe('localStorage');
|
||||
});
|
||||
|
||||
it('does not persist tokens when method is cookie (client-side no-op)', async () => {
|
||||
setStorageMethod('cookie');
|
||||
expect(getStorageMethod()).toBe('cookie');
|
||||
|
||||
await saveTokens({ accessToken: 'A', refreshToken: 'R' });
|
||||
// Should be no-op for client-side
|
||||
expect(localStorage.getItem('auth_tokens')).toBeNull();
|
||||
|
||||
const retrieved = await getTokens();
|
||||
expect(retrieved).toBeNull();
|
||||
|
||||
// clearTokens should not throw in cookie mode
|
||||
await expect(clearTokens()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setStorageMethod error path', () => {
|
||||
it('logs an error if localStorage.setItem throws', () => {
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const originalSetItem = Storage.prototype.setItem;
|
||||
Storage.prototype.setItem = jest.fn(() => {
|
||||
throw new Error('boom');
|
||||
});
|
||||
|
||||
// Should not throw despite underlying error
|
||||
expect(() => setStorageMethod('localStorage')).not.toThrow();
|
||||
|
||||
// Restore and verify log
|
||||
Storage.prototype.setItem = originalSetItem;
|
||||
expect(spy).toHaveBeenCalled();
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
38
frontend/tests/lib/i18n/routing.test.ts
Normal file
38
frontend/tests/lib/i18n/routing.test.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Tests for i18n routing configuration
|
||||
*/
|
||||
|
||||
import { routing, Link, usePathname, useRouter } from '@/lib/i18n/routing';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
describe('i18n routing', () => {
|
||||
it('exposes supported locales and defaultLocale', () => {
|
||||
expect(routing.locales).toEqual(['en', 'it']);
|
||||
expect(routing.defaultLocale).toBe('en');
|
||||
// Using "always" strategy for clarity
|
||||
// @ts-expect-error - localePrefix not in typed export
|
||||
expect(routing.localePrefix).toBe('always');
|
||||
});
|
||||
|
||||
it('provides Link wrapper that preserves href', () => {
|
||||
render(
|
||||
<Link href="/en/about" data-testid="test-link">
|
||||
About
|
||||
</Link>
|
||||
);
|
||||
const el = screen.getByTestId('test-link') as HTMLAnchorElement;
|
||||
expect(el.tagName).toBe('A');
|
||||
expect(el.getAttribute('href')).toBe('/en/about');
|
||||
expect(screen.getByText('About')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('provides navigation hooks', () => {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
|
||||
expect(pathname).toBe('/en/test');
|
||||
expect(router).toEqual(
|
||||
expect.objectContaining({ push: expect.any(Function), replace: expect.any(Function) })
|
||||
);
|
||||
});
|
||||
});
|
||||
39
frontend/tests/lib/i18n/routing.test.tsx
Normal file
39
frontend/tests/lib/i18n/routing.test.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Tests for i18n routing configuration
|
||||
*/
|
||||
|
||||
import { routing, Link, usePathname, useRouter, redirect } from '@/lib/i18n/routing';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
describe('i18n routing', () => {
|
||||
it('exposes supported locales and defaultLocale', () => {
|
||||
expect(routing.locales).toEqual(['en', 'it']);
|
||||
expect(routing.defaultLocale).toBe('en');
|
||||
// Using "always" strategy for clarity (property exists in the config object)
|
||||
// @ts-expect-error typed export may not include localePrefix
|
||||
expect(routing.localePrefix).toBe('always');
|
||||
});
|
||||
|
||||
it('provides Link wrapper that preserves href and children', () => {
|
||||
render(
|
||||
<Link href="/en/about" data-testid="test-link">
|
||||
About
|
||||
</Link>
|
||||
);
|
||||
const el = screen.getByTestId('test-link') as HTMLAnchorElement;
|
||||
expect(el.tagName).toBe('A');
|
||||
expect(el.getAttribute('href')).toBe('/en/about');
|
||||
expect(screen.getByText('About')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('provides navigation hooks and redirect function', () => {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
|
||||
expect(pathname).toBe('/en/test');
|
||||
expect(typeof redirect).toBe('function');
|
||||
expect(router).toEqual(
|
||||
expect.objectContaining({ push: expect.any(Function), replace: expect.any(Function) })
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user