Skip to content

Commit b7ca630

Browse files
mistercrunchclaude
authored andcommitted
fix(SqlLab): address code review — clipboard safety, duplicate copy button, test coverage
- Fix clipboard guard: chain ?.then() so undefined clipboard doesn't throw - SyntaxHighlighterCopy already has its own copy button (with toasts via src/utils/copy); pass showCopyButton={false} to suppress the built-in one and avoid duplicate controls in query preview modals - Add CodeSyntaxHighlighter tests: copy button shown/hidden, no-throw when clipboard API is unavailable - Add HighlightedSql test: rawSql === sql shows single block with no tabs Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 1590929 commit b7ca630

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎superset-frontend/packages/superset-ui-core/src/components/CodeSyntaxHighlighter/index.test.tsx‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { render, screen } from '../../spec';
19+
import { render, screen, fireEvent } from '../../spec';
2020
import CodeSyntaxHighlighter from './index';
2121

2222
// Simple mock that just returns the content
@@ -153,4 +153,43 @@ describe('CodeSyntaxHighlighter', () => {
153153

154154
expect(screen.getByText('SELECT * FROM users;')).toBeInTheDocument();
155155
});
156+
157+
test('shows copy button by default', () => {
158+
render(
159+
<CodeSyntaxHighlighter language="sql">SELECT 1;</CodeSyntaxHighlighter>,
160+
);
161+
162+
expect(screen.getByTitle('Copy to clipboard')).toBeInTheDocument();
163+
});
164+
165+
test('hides copy button when showCopyButton is false', () => {
166+
render(
167+
<CodeSyntaxHighlighter language="sql" showCopyButton={false}>
168+
SELECT 1;
169+
</CodeSyntaxHighlighter>,
170+
);
171+
172+
expect(screen.queryByTitle('Copy to clipboard')).not.toBeInTheDocument();
173+
});
174+
175+
test('copy button does not throw when clipboard API is unavailable', () => {
176+
const originalClipboard = navigator.clipboard;
177+
Object.defineProperty(navigator, 'clipboard', {
178+
value: undefined,
179+
configurable: true,
180+
});
181+
182+
render(
183+
<CodeSyntaxHighlighter language="sql">SELECT 1;</CodeSyntaxHighlighter>,
184+
);
185+
186+
expect(() =>
187+
fireEvent.click(screen.getByTitle('Copy to clipboard')),
188+
).not.toThrow();
189+
190+
Object.defineProperty(navigator, 'clipboard', {
191+
value: originalClipboard,
192+
configurable: true,
193+
});
194+
});
156195
});

‎superset-frontend/packages/superset-ui-core/src/components/CodeSyntaxHighlighter/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const CodeSyntaxHighlighter: React.FC<CodeSyntaxHighlighterProps> = ({
106106
);
107107

108108
const handleCopy = useCallback(() => {
109-
navigator.clipboard?.writeText(children).then(() => {
109+
navigator.clipboard?.writeText(children)?.then(() => {
110110
setCopied(true);
111111
copyTimeoutRef.current = setTimeout(() => setCopied(false), 1500);
112112
});

‎superset-frontend/src/SqlLab/components/HighlightedSql/HighlightedSql.test.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ test('renders a ModalTrigger component with shrink prop and maxWidth prop set to
3636
);
3737
expect(getByTestId('span-modal-trigger')).toBeInTheDocument();
3838
});
39+
test('renders single SQL block with no tabs when rawSql equals sql', () => {
40+
const { getByRole, queryByRole, getByTestId, queryByText } = render(
41+
<HighlightedSql sql={sql} rawSql={sql} shrink maxWidth={5} />,
42+
);
43+
expect(queryByRole('dialog')).not.toBeInTheDocument();
44+
fireEvent.click(getByTestId('span-modal-trigger'));
45+
expect(queryByRole('dialog')).toBeInTheDocument();
46+
expect(queryByText('Executed SQL')).not.toBeInTheDocument();
47+
expect(queryByText('Source SQL')).toBeInTheDocument();
48+
});
49+
3950
test('renders tabs when rawSql differs from sql', () => {
4051
const { getByRole, queryByRole, getByTestId, getByText } = render(
4152
<HighlightedSql sql={sql} rawSql="SELECT * FROM foo" shrink maxWidth={5} />,

‎superset-frontend/src/features/queries/SyntaxHighlighterCopy.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ export default function SyntaxHighlighterCopy({
124124
onClick={handleCopyClick}
125125
onKeyDown={handleKeyDown}
126126
/>
127-
<CodeSyntaxHighlighter language={language} {...syntaxHighlighterProps}>
127+
<CodeSyntaxHighlighter
128+
language={language}
129+
showCopyButton={false}
130+
{...syntaxHighlighterProps}
131+
>
128132
{children}
129133
</CodeSyntaxHighlighter>
130134
</SyntaxHighlighterWrapper>

0 commit comments

Comments
 (0)