Skip to content

Commit 1b29a68

Browse files
authored
Add optional prop to conditionally render doctl command in GeneratedCommands component (#105)
1 parent 087ff74 commit 1b29a68

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed

src/__tests__/GeneratedCommands.test.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,49 @@ describe('GeneratedCommands', () => {
1515
mockOnCopy.mockClear();
1616
});
1717

18-
it('renders both commands', () => {
18+
it('renders only curl command by default', () => {
1919
render(<GeneratedCommands commands={mockCommands} />);
2020

21-
// Use a more specific selector for the command headers
21+
expect(screen.getByRole('heading', { name: /generated.*curl.*command/i })).toBeInTheDocument();
22+
expect(screen.getByText(mockCommands.curl)).toBeInTheDocument();
23+
24+
// doctl command should not be visible
25+
expect(screen.queryByRole('heading', { name: /generated.*doctl.*command/i })).not.toBeInTheDocument();
26+
expect(screen.queryByText(mockCommands.doctl)).not.toBeInTheDocument();
27+
});
28+
29+
it('renders both commands when showDoctlCommand is true', () => {
30+
render(<GeneratedCommands commands={mockCommands} showDoctlCommand={true} />);
31+
2232
expect(screen.getByRole('heading', { name: /generated.*curl.*command/i })).toBeInTheDocument();
2333
expect(screen.getByRole('heading', { name: /generated.*doctl.*command/i })).toBeInTheDocument();
2434
expect(screen.getByText(mockCommands.curl)).toBeInTheDocument();
2535
expect(screen.getByText(mockCommands.doctl)).toBeInTheDocument();
2636
});
2737

28-
it('copies commands to clipboard when clicking copy button', async () => {
38+
it('copies curl command to clipboard when clicking copy button', async () => {
2939
render(<GeneratedCommands commands={mockCommands} onCopy={mockOnCopy} />);
3040

41+
const copyButton = screen.getByRole('button', { name: /copy/i });
42+
43+
// Test copying curl command
44+
await fireEvent.click(copyButton);
45+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(mockCommands.curl);
46+
expect(mockOnCopy).toHaveBeenCalled();
47+
});
48+
49+
it('copies both commands when showDoctlCommand is true', async () => {
50+
render(<GeneratedCommands commands={mockCommands} onCopy={mockOnCopy} showDoctlCommand={true} />);
51+
3152
const copyButtons = screen.getAllByRole('button', { name: /copy/i });
3253

3354
// Test copying curl command
3455
await fireEvent.click(copyButtons[0]);
3556
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(mockCommands.curl);
3657
expect(mockOnCopy).toHaveBeenCalled();
3758

59+
mockOnCopy.mockClear();
60+
3861
// Test copying doctl command
3962
await fireEvent.click(copyButtons[1]);
4063
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(mockCommands.doctl);
@@ -44,8 +67,8 @@ describe('GeneratedCommands', () => {
4467
it('shows "Copied!" text after copying', async () => {
4568
render(<GeneratedCommands commands={mockCommands} />);
4669

47-
const copyButtons = screen.getAllByRole('button', { name: /copy/i });
48-
await fireEvent.click(copyButtons[0]);
70+
const copyButton = screen.getByRole('button', { name: /copy/i });
71+
await fireEvent.click(copyButton);
4972

5073
// Wait for the "Copied!" text to appear
5174
await waitFor(() => {

src/components/GeneratedCommands.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ interface GeneratedCommandsProps {
77
doctl: string;
88
};
99
onCopy?: () => void;
10+
showDoctlCommand?: boolean;
1011
}
1112

12-
const GeneratedCommands = ({ commands, onCopy }: GeneratedCommandsProps) => {
13+
const GeneratedCommands = ({ commands, onCopy, showDoctlCommand = false }: GeneratedCommandsProps) => {
1314
const [curlCopied, setCurlCopied] = useState(false);
1415
const [doctlCopied, setDoctlCopied] = useState(false);
1516

@@ -47,22 +48,24 @@ const GeneratedCommands = ({ commands, onCopy }: GeneratedCommandsProps) => {
4748
</pre>
4849
</div>
4950

50-
{/* doctl Command */}
51-
<div>
52-
<div className="flex justify-between items-center mb-4">
53-
<h2 className="text-lg font-medium">Generated <code>doctl</code> command</h2>
54-
<button
55-
onClick={() => handleCopy(commands.doctl, 'doctl')}
56-
className="flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors"
57-
>
58-
<Copy size={16} />
59-
{doctlCopied ? 'Copied!' : 'Copy'}
60-
</button>
51+
{/* doctl Command - conditionally rendered */}
52+
{showDoctlCommand && (
53+
<div>
54+
<div className="flex justify-between items-center mb-4">
55+
<h2 className="text-lg font-medium">Generated <code>doctl</code> command</h2>
56+
<button
57+
onClick={() => handleCopy(commands.doctl, 'doctl')}
58+
className="flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors"
59+
>
60+
<Copy size={16} />
61+
{doctlCopied ? 'Copied!' : 'Copy'}
62+
</button>
63+
</div>
64+
<pre className="bg-gray-900 text-white p-6 rounded-lg overflow-x-auto">
65+
{commands.doctl}
66+
</pre>
6167
</div>
62-
<pre className="bg-gray-900 text-white p-6 rounded-lg overflow-x-auto">
63-
{commands.doctl}
64-
</pre>
65-
</div>
68+
)}
6669
</div>
6770
</div>
6871
);

src/utils/generateCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const generateCommands = (databaseId: string, config: any, engine: string
22
if (!databaseId || Object.keys(config).length === 0) return { curl: '', doctl: '' };
33

44
const jsonBody = JSON.stringify({ config }, null, 2);
5-
const compactJsonBody = JSON.stringify({ config });
5+
const configJsonBody = JSON.stringify(config);
66

77
return {
88
curl: `curl -X PATCH \\
@@ -11,6 +11,6 @@ export const generateCommands = (databaseId: string, config: any, engine: string
1111
-d '${jsonBody}' \\
1212
"https://api.digitalocean.com/v2/databases/${databaseId}/config"`,
1313

14-
doctl: `doctl databases configuration update ${databaseId} --engine ${engine} --config-json '${compactJsonBody}'`
14+
doctl: `doctl databases configuration update ${databaseId} --engine ${engine} --config-json '${configJsonBody}'`
1515
};
1616
};

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This file is automatically updated by our release process
2-
export const VERSION = '1.5.0';
2+
export const VERSION = '1.5.2';
33
export const BUILD_DATE = new Date().toISOString();

0 commit comments

Comments
 (0)