Skip to content

Commit

Permalink
fix: failing test due to limited jest support for focus-trap-react
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Nov 21, 2024
1 parent 4f16595 commit 14f7a67
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/components/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { App } from "./App";
import { render, screen } from "@testing-library/react";

describe("test load app", () => {
it("renders without crashing", () => {
render(<App/>);
const mockHandleSetActiveTrap = jest.fn();
it("renders without crashing", async () => {
render(<App activeTrap={false} handleSetActiveTrap={mockHandleSetActiveTrap}/>);
expect(screen.getByText("(Data Analysis through Voice and Artificial Intelligence)")).toBeDefined();
});
});
55 changes: 25 additions & 30 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ const mockAiResponse = (): ChatMessage => {
return response;
};

export const App = () => {
interface IAppProps {
activeTrap: boolean;
handleSetActiveTrap: (active: boolean) => void;
}

export const App = ({activeTrap, handleSetActiveTrap}: IAppProps) => {
const greeting = "Hello! I'm DAVAI, your Data Analysis through Voice and Artificial Intelligence partner.";
const [chatTranscript, setChatTranscript] = useState<ChatTranscript>({messages: [{speaker: "DAVAI", content: greeting, timestamp: timeStamp()}]});
const [readAloudEnabled, setReadAloudEnabled] = useState(false);
const [playbackSpeed, setPlaybackSpeed] = useState(1);
const [activeTrap, setActiveTrap] = useState(false);

useEffect(() => {
initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions});
setActiveTrap(true);
}, []);

const handleSetReadAloudEnabled = () => {
Expand All @@ -58,33 +61,25 @@ export const App = () => {
};

return (
<FocusTrap
active={activeTrap}
focusTrapOptions={{
allowOutsideClick: true,
escapeDeactivates: true,
onDeactivate: () => setActiveTrap(false),
onActivate: () => setActiveTrap(true)
}}
<div
onFocus={() => handleSetActiveTrap(true)}
role="main"
className={`App ${activeTrap && "isActive"}`}
>
<div
onFocus={() => setActiveTrap(true)}
role="main"
className={`App ${activeTrap && "isActive"}`}
>
<h1>
DAVAI
<span>(Data Analysis through Voice and Artificial Intelligence)</span>
</h1>
<ChatTranscriptComponent chatTranscript={chatTranscript} />
<ChatInputComponent onSubmit={handleChatInputSubmit} />
<ReadAloudMenu
enabled={readAloudEnabled}
onToggle={handleSetReadAloudEnabled}
playbackSpeed={playbackSpeed}
onPlaybackSpeedSelect={handleSetPlaybackSpeed}
/>
</div>
</FocusTrap>
<h1>
DAVAI
<span>(Data Analysis through Voice and Artificial Intelligence)</span>
</h1>
<ChatTranscriptComponent chatTranscript={chatTranscript} />
<ChatInputComponent onSubmit={handleChatInputSubmit} />
<ReadAloudMenu
enabled={readAloudEnabled}
onToggle={handleSetReadAloudEnabled}
playbackSpeed={playbackSpeed}
onPlaybackSpeedSelect={handleSetPlaybackSpeed}
/>
<button onClick={() => handleSetActiveTrap(true)}>activate focus trap</button>
<button onClick={() => handleSetActiveTrap(false)}>exit focus trap</button>
</div>
);
};
30 changes: 30 additions & 0 deletions src/components/focus-trap-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useEffect, useState } from "react";
import FocusTrap from "focus-trap-react";
import { App } from "./App";

export const FocusTrapWrapper: React.FC = () => {
const [activeTrap, setActiveTrap] = useState(false);

useEffect(() => {
setActiveTrap(true);
}, []);

const handleSetActiveTrap = (active: boolean) => {
setActiveTrap(active);
}

Check warning on line 14 in src/components/focus-trap-wrapper.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Missing semicolon

Check warning on line 14 in src/components/focus-trap-wrapper.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

Missing semicolon

Check warning on line 14 in src/components/focus-trap-wrapper.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

Missing semicolon

Check warning on line 14 in src/components/focus-trap-wrapper.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

Missing semicolon

return (
<FocusTrap
active={activeTrap}
focusTrapOptions={{
allowOutsideClick: true,
escapeDeactivates: true,
onDeactivate: () => setActiveTrap(false),
onActivate: () => setActiveTrap(true),
}}
>
<App activeTrap={activeTrap} handleSetActiveTrap={handleSetActiveTrap}/>
</FocusTrap>
);
};

4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from "react";
import { createRoot } from "react-dom/client";
import { App } from "./components/App";
import { FocusTrapWrapper } from "./components/focus-trap-wrapper";

import "./index.scss";

const container = document.getElementById("app");
if (container) {
const root = createRoot(container);

root.render(<App />);
root.render(<FocusTrapWrapper />);
}

0 comments on commit 14f7a67

Please sign in to comment.