mirror of
https://github.com/apache/skywalking-booster-ui.git
synced 2025-10-14 11:21:29 +00:00
fix: optimize appearing the wrong prompt by pop-up for the HTTP environments in copy function (#491)
This commit is contained in:
@@ -29,6 +29,11 @@ const mockClipboard = {
|
|||||||
writeText: vi.fn(),
|
writeText: vi.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Mock location
|
||||||
|
const mockLocation = {
|
||||||
|
protocol: "https:",
|
||||||
|
};
|
||||||
|
|
||||||
describe("copy utility function", () => {
|
describe("copy utility function", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
@@ -38,13 +43,19 @@ describe("copy utility function", () => {
|
|||||||
value: mockClipboard,
|
value: mockClipboard,
|
||||||
writable: true,
|
writable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mock location
|
||||||
|
Object.defineProperty(window, "location", {
|
||||||
|
value: mockLocation,
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should copy text successfully and show success notification", async () => {
|
it("should copy text successfully and show success notification in HTTPS", async () => {
|
||||||
const testText = "test text to copy";
|
const testText = "test text to copy";
|
||||||
mockClipboard.writeText.mockResolvedValue(undefined);
|
mockClipboard.writeText.mockResolvedValue(undefined);
|
||||||
|
|
||||||
@@ -61,6 +72,43 @@ describe("copy utility function", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should show error notification for HTTP protocol", () => {
|
||||||
|
const testText = "test text to copy";
|
||||||
|
|
||||||
|
// Set protocol to HTTP
|
||||||
|
Object.defineProperty(window, "location", {
|
||||||
|
value: { protocol: "http:" },
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
copy(testText);
|
||||||
|
|
||||||
|
expect(ElNotification).toHaveBeenCalledWith({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported in HTTP environments",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
|
expect(mockClipboard.writeText).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should show error notification when clipboard is not available", () => {
|
||||||
|
const testText = "test text to copy";
|
||||||
|
|
||||||
|
// Remove clipboard from navigator
|
||||||
|
Object.defineProperty(navigator, "clipboard", {
|
||||||
|
value: undefined,
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
copy(testText);
|
||||||
|
|
||||||
|
expect(ElNotification).toHaveBeenCalledWith({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should handle clipboard error and show error notification", async () => {
|
it("should handle clipboard error and show error notification", async () => {
|
||||||
const testText = "test text to copy";
|
const testText = "test text to copy";
|
||||||
const errorMessage = "Clipboard permission denied";
|
const errorMessage = "Clipboard permission denied";
|
||||||
@@ -73,7 +121,7 @@ describe("copy utility function", () => {
|
|||||||
|
|
||||||
expect(mockClipboard.writeText).toHaveBeenCalledWith(testText);
|
expect(mockClipboard.writeText).toHaveBeenCalledWith(testText);
|
||||||
expect(ElNotification).toHaveBeenCalledWith({
|
expect(ElNotification).toHaveBeenCalledWith({
|
||||||
title: "Error",
|
title: "Warning",
|
||||||
message: errorMessage,
|
message: errorMessage,
|
||||||
type: "warning",
|
type: "warning",
|
||||||
});
|
});
|
||||||
@@ -157,18 +205,47 @@ describe("copy utility function", () => {
|
|||||||
expect(ElNotification).toHaveBeenCalledTimes(3);
|
expect(ElNotification).toHaveBeenCalledTimes(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle clipboard not available", async () => {
|
it("should handle HTTP protocol and clipboard not available", () => {
|
||||||
const testText = "test text";
|
const testText = "test text";
|
||||||
|
|
||||||
// Remove clipboard from navigator
|
// Set protocol to HTTP
|
||||||
|
Object.defineProperty(window, "location", {
|
||||||
|
value: { protocol: "http:" },
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
copy(testText);
|
||||||
|
|
||||||
|
// Should show HTTP error, not clipboard error
|
||||||
|
expect(ElNotification).toHaveBeenCalledWith({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported in HTTP environments",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle file protocol", () => {
|
||||||
|
const testText = "test text";
|
||||||
|
|
||||||
|
// Set protocol to file and ensure clipboard is not available
|
||||||
|
Object.defineProperty(window, "location", {
|
||||||
|
value: { protocol: "file:" },
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove clipboard from navigator for this test
|
||||||
Object.defineProperty(navigator, "clipboard", {
|
Object.defineProperty(navigator, "clipboard", {
|
||||||
value: undefined,
|
value: undefined,
|
||||||
writable: true,
|
writable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Should not throw error
|
copy(testText);
|
||||||
expect(() => {
|
|
||||||
copy(testText);
|
// Should show clipboard not supported error
|
||||||
}).not.toThrow();
|
expect(ElNotification).toHaveBeenCalledWith({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -18,8 +18,20 @@
|
|||||||
import { ElNotification } from "element-plus";
|
import { ElNotification } from "element-plus";
|
||||||
|
|
||||||
export default (text: string): void => {
|
export default (text: string): void => {
|
||||||
|
if (location.protocol === "http:") {
|
||||||
|
ElNotification({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported in HTTP environments",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!navigator.clipboard) {
|
if (!navigator.clipboard) {
|
||||||
console.error("Clipboard is not supported");
|
ElNotification({
|
||||||
|
title: "Warning",
|
||||||
|
message: "Clipboard is not supported",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
navigator.clipboard
|
navigator.clipboard
|
||||||
@@ -33,7 +45,7 @@ export default (text: string): void => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
ElNotification({
|
ElNotification({
|
||||||
title: "Error",
|
title: "Warning",
|
||||||
message: err,
|
message: err,
|
||||||
type: "warning",
|
type: "warning",
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user