test(core): strengthen app and auth assertions
This commit is contained in:
parent
9a746dfffa
commit
8ad3e1be34
@ -32,8 +32,8 @@ describe('createServiceApp', () => {
|
|||||||
expect(body.service).toBe('my-service');
|
expect(body.service).toBe('my-service');
|
||||||
expect(body.version).toBe('1.2.3');
|
expect(body.version).toBe('1.2.3');
|
||||||
expect(body.description).toBe('A test service');
|
expect(body.description).toBe('A test service');
|
||||||
expect(body.timestamp).toBeTruthy();
|
expect(body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
|
||||||
expect(body.requestId).toBeTruthy();
|
expect(body.requestId).toBe(res.headers['x-request-id']);
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
@ -72,6 +72,8 @@ describe('createServiceApp', () => {
|
|||||||
expect(res.headers['x-request-id']).toMatch(
|
expect(res.headers['x-request-id']).toMatch(
|
||||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
||||||
);
|
);
|
||||||
|
const body = JSON.parse(res.payload);
|
||||||
|
expect(body.requestId).toBe(res.headers['x-request-id']);
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
@ -92,7 +94,7 @@ describe('createServiceApp', () => {
|
|||||||
expect(res.statusCode).toBe(404);
|
expect(res.statusCode).toBe(404);
|
||||||
const body = JSON.parse(res.payload);
|
const body = JSON.parse(res.payload);
|
||||||
expect(body.error).toBe('User not found');
|
expect(body.error).toBe('User not found');
|
||||||
expect(body.requestId).toBeTruthy();
|
expect(body.requestId).toBe(res.headers['x-request-id']);
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
@ -114,7 +116,7 @@ describe('createServiceApp', () => {
|
|||||||
const body = JSON.parse(res.payload);
|
const body = JSON.parse(res.payload);
|
||||||
expect(body.error).toBe('Validation failed');
|
expect(body.error).toBe('Validation failed');
|
||||||
expect(body.details).toEqual({ field: 'email' });
|
expect(body.details).toEqual({ field: 'email' });
|
||||||
expect(body.requestId).toBeTruthy();
|
expect(body.requestId).toBe(res.headers['x-request-id']);
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
@ -134,7 +136,7 @@ describe('createServiceApp', () => {
|
|||||||
expect(res.statusCode).toBe(500);
|
expect(res.statusCode).toBe(500);
|
||||||
const body = JSON.parse(res.payload);
|
const body = JSON.parse(res.payload);
|
||||||
expect(body.error).toBe('Internal server error');
|
expect(body.error).toBe('Internal server error');
|
||||||
expect(body.requestId).toBeTruthy();
|
expect(body.requestId).toBe(res.headers['x-request-id']);
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
@ -166,6 +168,8 @@ describe('createServiceApp', () => {
|
|||||||
expect(JSON.parse(before.payload)).toMatchObject({
|
expect(JSON.parse(before.payload)).toMatchObject({
|
||||||
status: 'not_ready',
|
status: 'not_ready',
|
||||||
service: 'ready-test',
|
service: 'ready-test',
|
||||||
|
version: '1.0.0',
|
||||||
|
requestId: before.headers['x-request-id'],
|
||||||
});
|
});
|
||||||
|
|
||||||
app.setReadyState(true);
|
app.setReadyState(true);
|
||||||
@ -175,6 +179,8 @@ describe('createServiceApp', () => {
|
|||||||
expect(JSON.parse(after.payload)).toMatchObject({
|
expect(JSON.parse(after.payload)).toMatchObject({
|
||||||
status: 'ready',
|
status: 'ready',
|
||||||
service: 'ready-test',
|
service: 'ready-test',
|
||||||
|
version: '1.0.0',
|
||||||
|
requestId: after.headers['x-request-id'],
|
||||||
});
|
});
|
||||||
|
|
||||||
await app.close();
|
await app.close();
|
||||||
|
|||||||
@ -67,8 +67,7 @@ describe('createAuthProvider', () => {
|
|||||||
<div data-testid="child">Hello</div>
|
<div data-testid="child">Hello</div>
|
||||||
</AuthProvider>
|
</AuthProvider>
|
||||||
);
|
);
|
||||||
expect(screen.getByTestId('child')).toBeDefined();
|
expect(screen.getByTestId('child').textContent).toBe('Hello');
|
||||||
expect(screen.getByText('Hello')).toBeDefined();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('starts unauthenticated with no stored user', () => {
|
it('starts unauthenticated with no stored user', () => {
|
||||||
@ -159,6 +158,13 @@ describe('createAuthProvider', () => {
|
|||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
expect(screen.getByTestId('auth').textContent).toBe('true');
|
expect(screen.getByTestId('auth').textContent).toBe('true');
|
||||||
expect(screen.getByTestId('email').textContent).toBe('test@example.com');
|
expect(screen.getByTestId('email').textContent).toBe('test@example.com');
|
||||||
|
expect(mockFetch).toHaveBeenCalledWith(
|
||||||
|
'/api/auth/login',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: 'test@example.com', password: 'pass123' }),
|
||||||
|
})
|
||||||
|
);
|
||||||
expect(localStorageMock.setItem).toHaveBeenCalledWith(
|
expect(localStorageMock.setItem).toHaveBeenCalledWith(
|
||||||
'test_auth_user',
|
'test_auth_user',
|
||||||
expect.stringContaining('test@example.com')
|
expect.stringContaining('test@example.com')
|
||||||
@ -179,9 +185,14 @@ describe('createAuthProvider', () => {
|
|||||||
let loginFn: (email: string, password: string) => Promise<boolean>;
|
let loginFn: (email: string, password: string) => Promise<boolean>;
|
||||||
|
|
||||||
function LoginComponent() {
|
function LoginComponent() {
|
||||||
const { login, isAuthenticated } = useAuth();
|
const { login, isAuthenticated, error } = useAuth();
|
||||||
loginFn = login;
|
loginFn = login;
|
||||||
return <span data-testid="auth">{String(isAuthenticated)}</span>;
|
return (
|
||||||
|
<div>
|
||||||
|
<span data-testid="auth">{String(isAuthenticated)}</span>
|
||||||
|
<span data-testid="error">{error ?? 'none'}</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render(
|
render(
|
||||||
@ -197,6 +208,7 @@ describe('createAuthProvider', () => {
|
|||||||
|
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
expect(screen.getByTestId('auth').textContent).toBe('false');
|
expect(screen.getByTestId('auth').textContent).toBe('false');
|
||||||
|
expect(screen.getByTestId('error').textContent).toBe('Unauthorized');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('logout clears user and storage', async () => {
|
it('logout clears user and storage', async () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user