nxtgauge-frontend-solid/tests/vitest/components/AiChatWidget.test.tsx
Tracewebstudio Dev 4efe848f3a
All checks were successful
build-and-release / build (push) Successful in 2m15s
fix: local dev proxy, TypeScript errors, and test quality
- middleware.ts: default GATEWAY_URL to localhost:9100 (was K8s hostname);
  proxy all /api/* to gateway instead of falling through to SolidStart renderer
- signup/index.tsx: fix kebab-case style property (align-items)
- dashboard/CreditsPage.tsx: fix IIFE closing in Show children
- AskAsh/index.ts: use ~ alias to fix bundler module resolution
- vite.config.ts: add string type to rewrite callback
- global.d.ts: add Window.__captchaCode and __testMode declarations
- help-center/article/[slug].tsx: add missing ContentBlock import
- test/setup.ts: add vitest/globals reference, fix IntersectionObserver mock
- tests/e2e: fix implicit any, string|null/undefined type errors in e2e specs
- tests/tsconfig.json: add tests-specific tsconfig with node types

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-17 00:47:32 +02:00

89 lines
No EOL
2.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor } from "@solidjs/testing-library";
import { AiChatWidget } from "~/components/AiChatWidget";
global.fetch = vi.fn();
function createFetchMock(response: unknown, ok = true) {
return vi.mocked(fetch).mockResolvedValueOnce({
ok,
json: () => Promise.resolve(response),
} as Response);
}
describe("AiChatWidget", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders the floating button", () => {
render(() => <AiChatWidget />);
const button = screen.getByTitle("AI Assistant");
expect(button).toBeTruthy();
});
it("opens chat window on button click", async () => {
render(() => <AiChatWidget />);
const button = screen.getByTitle("AI Assistant");
fireEvent.click(button);
await waitFor(() => {
const header = screen.getByText("AI Assistant");
expect(header).toBeTruthy();
});
});
it("shows initial greeting message", () => {
render(() => <AiChatWidget />);
const button = screen.getByTitle("AI Assistant");
fireEvent.click(button);
const greeting = screen.getByText(/I'm your AI assistant/i);
expect(greeting).toBeTruthy();
});
it("shows error message when API fails", async () => {
vi.mocked(fetch).mockRejectedValueOnce(new Error("Network error"));
render(() => <AiChatWidget />);
const button = screen.getByTitle("AI Assistant");
fireEvent.click(button);
await waitFor(() => {
const input = screen.getByPlaceholderText(/Ask/i);
fireEvent.change(input, { target: { value: "Test" } });
fireEvent.keyDown(input, { key: "Enter" });
});
await waitFor(
() => {
expect(screen.getByText(/contact support/i)).toBeTruthy();
},
{ timeout: 5000 }
);
});
it("displays user message after sending", async () => {
createFetchMock({
message: "Hello from AI",
conversation_id: "conv-123",
intent: "general",
confidence: 0.9,
});
render(() => <AiChatWidget />);
const button = screen.getByTitle("AI Assistant");
fireEvent.click(button);
await waitFor(() => {
const input = screen.getByPlaceholderText(/Ask/i);
fireEvent.change(input, { target: { value: "Hello" } });
fireEvent.keyDown(input, { key: "Enter" });
});
await waitFor(() => {
expect(screen.getByText("Hello")).toBeTruthy();
});
});
});