Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Bewertung schreiben"
},
"hub_share_toast_message": {
"message": "Möchten Sie diesen Befehl mit dem Selection Command Hub teilen?"
},
"hub_share_toast_button": {
"message": "Teilen"
},
"hub_share_toast_later": {
"message": "Später"
},
"Option_shortcuts": {
"message": "Tastenkombinationen"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/extension/public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,17 @@
"review_request_button": {
"message": "Write a Review"
},
"hub_share_toast_message": {
"message": "Would you like to share this command with the Selection Command Hub?",
"description": "Toast message shown once, right after a new command is created, inviting the user to share it to the Hub."
},
"hub_share_toast_button": {
"message": "Share",
"description": "Button label to share the newly created command to the Hub from the toast."
},
"hub_share_toast_later": {
"message": "Later"
},
"Option_shortcuts": {
"message": "Shortcut Keys"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Escribir una Reseña"
},
"hub_share_toast_message": {
"message": "¿Quieres compartir este comando con Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Compartir"
},
"hub_share_toast_later": {
"message": "Luego"
},
"Option_shortcuts": {
"message": "Teclas de Acceso Rápido"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Écrire un Avis"
},
"hub_share_toast_message": {
"message": "Souhaitez-vous partager cette commande avec Selection Command Hub ?"
},
"hub_share_toast_button": {
"message": "Partager"
},
"hub_share_toast_later": {
"message": "Plus tard"
},
"Option_shortcuts": {
"message": "Raccourcis Clavier"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/hi/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "समीक्षा लिखें"
},
"hub_share_toast_message": {
"message": "क्या आप इस कमांड को Selection Command Hub पर साझा करना चाहेंगे?"
},
"hub_share_toast_button": {
"message": "साझा करें"
},
"hub_share_toast_later": {
"message": "बाद में"
},
"Option_shortcuts": {
"message": "शॉर्टकट"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/id/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Tulis Ulasan"
},
"hub_share_toast_message": {
"message": "Ingin membagikan perintah ini ke Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Bagikan"
},
"hub_share_toast_later": {
"message": "Nanti"
},
"Option_shortcuts": {
"message": "Tombol Pintas"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/it/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Scrivi recensione"
},
"hub_share_toast_message": {
"message": "Vuoi condividere questo comando con Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Condividi"
},
"hub_share_toast_later": {
"message": "Dopo"
},
"Option_shortcuts": {
"message": "Scorciatoie"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,15 @@
"review_request_button": {
"message": "レビューする"
},
"hub_share_toast_message": {
"message": "コマンドをSelection Command Hubへ共有しませんか?"
},
"hub_share_toast_button": {
"message": "共有する"
},
"hub_share_toast_later": {
"message": "後で"
},
"Option_shortcuts": {
"message": "ショートカットキー"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/ko/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "리뷰 작성"
},
"hub_share_toast_message": {
"message": "이 명령을 Selection Command Hub에 공유하시겠습니까?"
},
"hub_share_toast_button": {
"message": "공유하기"
},
"hub_share_toast_later": {
"message": "나중에"
},
"Option_shortcuts": {
"message": "단축키"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/ms/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Tulis Ulasan"
},
"hub_share_toast_message": {
"message": "Adakah anda ingin berkongsi arahan ini ke Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Kongsi"
},
"hub_share_toast_later": {
"message": "Kemudian"
},
"Option_shortcuts": {
"message": "Kekunci Pintas"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/pt_BR/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Escrever uma Avaliação"
},
"hub_share_toast_message": {
"message": "Deseja compartilhar este comando com o Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Compartilhar"
},
"hub_share_toast_later": {
"message": "Mais tarde"
},
"Option_shortcuts": {
"message": "Teclas de Atalho"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/pt_PT/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Escrever uma Avaliação"
},
"hub_share_toast_message": {
"message": "Deseja partilhar este comando com o Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Partilhar"
},
"hub_share_toast_later": {
"message": "Mais tarde"
},
"Option_shortcuts": {
"message": "Teclas de Atalho"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "Написать отзыв"
},
"hub_share_toast_message": {
"message": "Хотите поделиться этой командой в Selection Command Hub?"
},
"hub_share_toast_button": {
"message": "Поделиться"
},
"hub_share_toast_later": {
"message": "Позже"
},
"Option_shortcuts": {
"message": "Горячие клавиши"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,15 @@
"review_request_button": {
"message": "写评论"
},
"hub_share_toast_message": {
"message": "要将此命令分享到 Selection Command Hub 吗?"
},
"hub_share_toast_button": {
"message": "分享"
},
"hub_share_toast_later": {
"message": "稍后"
},
"Option_shortcuts": {
"message": "快捷键"
},
Expand Down
152 changes: 152 additions & 0 deletions packages/extension/src/components/option/HubShareToast.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { render, screen } from "@testing-library/react"
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
import userEvent from "@testing-library/user-event"
import { toast } from "sonner"
import { showHubShareToast } from "./HubShareToast"
import { sendEvent } from "@/services/analytics"
import { shareCommandToHub } from "@/services/hubShare"
import { OPEN_MODE } from "@/const"
import type { SearchCommand } from "@/types"

vi.mock("sonner", () => ({
toast: {
custom: vi.fn(),
dismiss: vi.fn(),
},
}))

vi.mock("@/services/i18n", () => ({
t: vi.fn((key: string) => {
const translations: Record<string, string> = {
hub_share_toast_message: "Share this command to the Hub?",
hub_share_toast_later: "Later",
hub_share_toast_button: "Share",
}
return translations[key] || key
}),
}))

vi.mock("@/services/analytics", () => ({
ANALYTICS_EVENTS: {
OPEN_DIALOG: "open_dialog",
COMMAND_SHARE: "command_share",
},
sendEvent: vi.fn(),
}))

vi.mock("@/services/hubShare", () => ({
shareCommandToHub: vi.fn(),
}))

const mockToastCustom = vi.mocked(toast.custom)
const mockToastDismiss = vi.mocked(toast.dismiss)
const mockSendEvent = vi.mocked(sendEvent)
const mockShareCommandToHub = vi.mocked(shareCommandToHub)

const command: SearchCommand = {
id: "cmd-1",
title: "Test Command",
iconUrl: "https://example.com/icon.png",
openMode: OPEN_MODE.POPUP,
searchUrl: "https://google.com/search?q=%s",
}

// Renders the JSX passed to toast.custom() so its buttons can be interacted with.
function renderToastBody(toastId: string | number = "toast-1") {
const [renderFn] = mockToastCustom.mock.calls[0]
return render(<>{renderFn(toastId)}</>)
}

describe("showHubShareToast", () => {
beforeEach(() => {
vi.clearAllMocks()
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it("HST-01: sends an open_dialog analytics event after the show delay", () => {
showHubShareToast(command, vi.fn())
expect(mockSendEvent).not.toHaveBeenCalled()

vi.advanceTimersByTime(2000)

expect(mockSendEvent).toHaveBeenCalledWith(
"open_dialog",
{ event_label: "hub_share_toast" },
expect.anything(),
)
})

it("HST-02: calls toast.custom with a 60 second duration after the show delay", () => {
showHubShareToast(command, vi.fn())
expect(mockToastCustom).not.toHaveBeenCalled()

vi.advanceTimersByTime(2000)

expect(mockToastCustom).toHaveBeenCalledWith(
expect.any(Function),
expect.objectContaining({ duration: 60 * 1000 }),
)
})

it("HST-03: renders the message and both buttons", () => {
showHubShareToast(command, vi.fn())
vi.advanceTimersByTime(2000)
renderToastBody()

expect(
screen.getByText("Share this command to the Hub?"),
).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Later" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: /Share/ })).toBeInTheDocument()
})

it("HST-04: clicking 'Later' dismisses the toast and calls onShown without sharing", async () => {
const onShown = vi.fn()
showHubShareToast(command, onShown)
vi.advanceTimersByTime(2000)
vi.useRealTimers()
const user = userEvent.setup()
renderToastBody("toast-1")

await user.click(screen.getByRole("button", { name: "Later" }))

expect(mockToastDismiss).toHaveBeenCalledWith("toast-1")
expect(onShown).toHaveBeenCalledOnce()
expect(mockShareCommandToHub).not.toHaveBeenCalled()
})

it("HST-05: clicking 'Share' shares the command, dismisses the toast, and calls onShown", async () => {
const onShown = vi.fn()
showHubShareToast(command, onShown)
vi.advanceTimersByTime(2000)
vi.useRealTimers()
const user = userEvent.setup()
renderToastBody("toast-1")

await user.click(screen.getByRole("button", { name: /Share/ }))

expect(mockShareCommandToHub).toHaveBeenCalledWith(command)
expect(mockSendEvent).toHaveBeenCalledWith(
"command_share",
{ event_label: "hub-share-toast" },
expect.anything(),
)
expect(mockToastDismiss).toHaveBeenCalledWith("toast-1")
expect(onShown).toHaveBeenCalledOnce()
})

it("HST-06: calls onShown when the toast auto-closes via timeout", () => {
const onShown = vi.fn()
showHubShareToast(command, onShown)
vi.advanceTimersByTime(2000)

const [, options] = mockToastCustom.mock.calls[0]
options?.onAutoClose?.({} as never)

expect(onShown).toHaveBeenCalledOnce()
})
})
Loading
Loading