telegram-thread-switch.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Picking another customer in the Telegram inbox. The previous customer's
  3. * thread stayed on screen when the new one could not be loaded, under the
  4. * new customer's selection, with a composer that sends to the new customer.
  5. * An answer for an earlier pick that arrives late is dropped as well.
  6. */
  7. import { act, fireEvent, render, screen } from '@testing-library/react'
  8. import { beforeEach, describe, expect, it, vi } from 'vitest'
  9. const getTelegramConversation = vi.fn()
  10. vi.mock('next/navigation', () => ({
  11. useRouter: () => ({ replace: vi.fn(), refresh: vi.fn(), push: vi.fn() }),
  12. useSearchParams: () => new URLSearchParams(),
  13. }))
  14. vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }))
  15. vi.mock('@/features/telegram/Actions/telegramActions', () => ({
  16. getTelegramConversation: async (id: string) => getTelegramConversation(id),
  17. deleteTelegramConversation: vi.fn(),
  18. }))
  19. vi.mock('@/features/telegram/Actions/telegramThreadActions', () => ({
  20. getRecentTelegramThreads: vi.fn(),
  21. }))
  22. // The thread list and the composer have their own tests; here they only show
  23. // which customer is picked and whose thread is open.
  24. vi.mock('@/features/telegram/Components/TelegramThreadList', () => ({
  25. TelegramThreadList: ({
  26. threads,
  27. onSelectThread,
  28. }: {
  29. threads: { customerId: string; customerName: string }[]
  30. onSelectThread: (id: string) => void
  31. }) => (
  32. <div>
  33. {threads.map((th) => (
  34. <button key={th.customerId} type="button" onClick={() => onSelectThread(th.customerId)}>
  35. {th.customerName}
  36. </button>
  37. ))}
  38. </div>
  39. ),
  40. }))
  41. vi.mock('@/features/telegram/Components/TelegramConversation', () => ({
  42. TelegramConversation: ({
  43. customerId,
  44. customerName,
  45. }: {
  46. customerId: string
  47. customerName: string
  48. }) => <p>{`Thread of ${customerName}, replies go to ${customerId}`}</p>,
  49. }))
  50. import { TelegramMessagesClient } from '@/features/telegram/Components/TelegramMessagesClient'
  51. const thread = (customerId: string, customerName: string) => ({
  52. customerId,
  53. customerName,
  54. telegramChatId: `chat-${customerId}`,
  55. lastMessage: { id: `m-${customerId}`, body: 'Hi', direction: 'in', createdAt: new Date() },
  56. })
  57. const THREADS = [thread('a', 'Ada'), thread('b', 'Ben')]
  58. const ANSWER = { success: true, data: { messages: [], nextCursor: null } }
  59. async function pick(name: string) {
  60. await act(async () => {
  61. fireEvent.click(screen.getByRole('button', { name }))
  62. })
  63. }
  64. describe('switching customers in the Telegram inbox', () => {
  65. beforeEach(() => {
  66. getTelegramConversation.mockReset()
  67. })
  68. it("does not leave the previous customer's thread open when the next one cannot be loaded", async () => {
  69. getTelegramConversation.mockImplementation(async (id: string) =>
  70. id === 'a' ? ANSWER : { success: false, error: 'Forbidden' }
  71. )
  72. render(<TelegramMessagesClient initialThreads={THREADS} />)
  73. await pick('Ada')
  74. expect(screen.getByText('Thread of Ada, replies go to a')).toBeInTheDocument()
  75. await pick('Ben')
  76. expect(screen.queryByText(/Thread of Ada/)).not.toBeInTheDocument()
  77. expect(screen.queryByText(/replies go to b/)).not.toBeInTheDocument()
  78. })
  79. it('drops the answer for an earlier pick that arrives after the next one', async () => {
  80. let answerAda!: (value: unknown) => void
  81. getTelegramConversation.mockImplementation((id: string) =>
  82. id === 'a'
  83. ? new Promise((resolve) => {
  84. answerAda = resolve
  85. })
  86. : Promise.resolve(ANSWER)
  87. )
  88. render(<TelegramMessagesClient initialThreads={THREADS} />)
  89. await pick('Ada')
  90. await pick('Ben')
  91. expect(screen.getByText('Thread of Ben, replies go to b')).toBeInTheDocument()
  92. await act(async () => {
  93. answerAda(ANSWER)
  94. })
  95. expect(screen.getByText('Thread of Ben, replies go to b')).toBeInTheDocument()
  96. expect(screen.queryByText(/Thread of Ada/)).not.toBeInTheDocument()
  97. })
  98. })