Skip to content

Commit

Permalink
Changes to integrate rate-limit headers for Commands left component…
Browse files Browse the repository at this point in the history
… UI (#67)
  • Loading branch information
aasifkhan7 authored Oct 18, 2024
1 parent 022d113 commit 02369c1
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 427 deletions.
52 changes: 0 additions & 52 deletions apps/playground-web/components/CLI/CLI.tsx

This file was deleted.

110 changes: 0 additions & 110 deletions apps/playground-web/components/CLI/__tests__/index.test.tsx

This file was deleted.

124 changes: 0 additions & 124 deletions apps/playground-web/components/CLI/hooks/useCli.tsx

This file was deleted.

7 changes: 4 additions & 3 deletions apps/playground-web/components/Playground/TerminalUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { useState } from 'react';
import Tooltip from '../Overlays/Tooltip';
export function TerminalUI({ initialCommandsLeft = 1000 }) {
const [commandsLeft, setCommandsLeft] = useState(initialCommandsLeft);
const decreaseCommandsLeft = () => {
setCommandsLeft((prev) => (prev > 0 ? prev - 1 : 0));
const handleCommandExecuted = (commands: number) => {
setCommandsLeft(commands);
};

return (
<>
<div className="bg-gray-900 rounded-lg" data-testid="terminal-container">
Expand All @@ -23,7 +24,7 @@ export function TerminalUI({ initialCommandsLeft = 1000 }) {
className="h-64 md:h-[30rem] bg-gray-100 rounded-lg overflow-hidden shadow-md"
data-testid="shell-container"
>
<Shell decreaseCommandsLeft={decreaseCommandsLeft} />
<Shell onCommandExecuted={handleCommandExecuted} />
</div>
</div>
<TerminalCounter commandsLeft={commandsLeft} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import userEvent from '@testing-library/user-event';
import { TerminalUI } from '../TerminalUI';
import { formatTime } from '@/shared/utils/commonUtils';

// Shared variable to store initialCommandsLeft
let sharedInitialCommandsLeft = 1000;

jest.mock('@/shared/hooks/useTimer', () => ({
useTimer: jest.fn(() => ({ timeLeft: 15 * 60 })), // Mock 15 minutes
}));

jest.mock('@/shared/utils/commonUtils', () => ({
formatTime: jest.fn(
(seconds) => `${Math.floor(seconds / 60)}:${seconds % 60}`,
),
}));

const setupTest = (initialCommandsLeft = 1000) => {
sharedInitialCommandsLeft = initialCommandsLeft;

const user = userEvent.setup();
const utils = render(
<TerminalUI initialCommandsLeft={initialCommandsLeft} />,
Expand All @@ -39,6 +38,21 @@ const setupTest = (initialCommandsLeft = 1000) => {
};
};

jest.mock('@/shared/utils/commonUtils', () => ({
formatTime: jest.fn(
(seconds) => `${Math.floor(seconds / 60)}:${seconds % 60}`,
),
handleResult: jest.fn(
({ result, newOutput, setOutput, onCommandExecuted }) => {
const mockData = result?.body?.data ? result.body.data : 'mock error';
setOutput([newOutput, mockData]);

const commandsLeft = Math.max(0, sharedInitialCommandsLeft - 1);
onCommandExecuted(commandsLeft);
},
),
}));

describe('TerminalUI Component', () => {
it('renders TerminalUI', () => {
const { terminalContainer, diceIcons, shellContainer, cliInputElement } =
Expand Down
6 changes: 3 additions & 3 deletions apps/playground-web/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import { useShell } from './hooks/useShell';

interface ShellProps {
decreaseCommandsLeft: () => void;
onCommandExecuted: (commandsLeft: number) => void;
}

export default function Shell({ decreaseCommandsLeft }: ShellProps) {
export default function Shell({ onCommandExecuted }: ShellProps) {
const {
handleInputChange,
handleKeyDown,
terminalRef,
inputRef,
output,
command,
} = useShell(decreaseCommandsLeft);
} = useShell(onCommandExecuted);
return (
<div
ref={terminalRef}
Expand Down
6 changes: 2 additions & 4 deletions apps/playground-web/components/Shell/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Shell from '../Shell';

const decreaseCommandsLeftMock = jest.fn();
const onCommandExecutedMock = jest.fn();

const dummyCommands = [
'set abc 100',
Expand All @@ -16,9 +16,7 @@ const dummyCommands = [

const setupTest = () => {
const user = userEvent.setup();
const utils = render(
<Shell decreaseCommandsLeft={decreaseCommandsLeftMock} />,
);
const utils = render(<Shell onCommandExecuted={onCommandExecutedMock} />);

const terminalElement = screen.getByTestId('terminal');
const cliInputElement = screen.getByTestId<HTMLInputElement>('shell-input');
Expand Down
Loading

0 comments on commit 02369c1

Please sign in to comment.