|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { mountSuspended } from '@nuxt/test-utils/runtime'; |
| 3 | +import { createI18n } from 'vue-i18n'; |
| 4 | +import messages from '~~/i18n/locales/en.json'; |
| 5 | +import QuoteDialog from '~/components/tweet/composer/QuoteTweetDialog.vue'; |
| 6 | +import TweetComposer from '~/components/tweet/composer/TweetComposer.vue'; |
| 7 | +import TweetDefaultCard from '~/components/tweet/TweetDefaultCard.vue'; |
| 8 | +import { useUserStore } from '@/stores/user'; |
| 9 | + |
| 10 | +const i18n = createI18n({ |
| 11 | + locale: 'en', |
| 12 | + messages: { en: messages }, |
| 13 | +}); |
| 14 | + |
| 15 | +vi.mock('@/stores/user', () => ({ |
| 16 | + useUserStore: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('QuoteDialog', () => { |
| 20 | + const mockUser = { |
| 21 | + id: '1', |
| 22 | + username: 'testuser', |
| 23 | + avatarUrl: 'https://example.com/avatar.jpg', |
| 24 | + }; |
| 25 | + |
| 26 | + const mockReplyToTweet = { |
| 27 | + id: 'tweet-123', |
| 28 | + content: 'Original tweet content', |
| 29 | + author: { |
| 30 | + id: '2', |
| 31 | + username: 'originalauthor', |
| 32 | + displayName: 'Original Author', |
| 33 | + }, |
| 34 | + createdAt: '2024-01-01T00:00:00Z', |
| 35 | + }; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + (useUserStore as ReturnType<typeof vi.fn>).mockReturnValue({ |
| 40 | + user: mockUser, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders TweetComposer with correct reply-to-tweet-id', async () => { |
| 45 | + const wrapper = await mountSuspended(QuoteDialog, { |
| 46 | + props: { |
| 47 | + open: true, |
| 48 | + replyToTweet: mockReplyToTweet, |
| 49 | + }, |
| 50 | + global: { |
| 51 | + plugins: [i18n], |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + const composer = wrapper.findComponent(TweetComposer); |
| 56 | + expect(composer.exists()).toBe(true); |
| 57 | + expect(composer.props('replyToTweetId')).toBe('tweet-123'); |
| 58 | + expect(composer.props('type')).toBe('quote'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders TweetDefaultCard in reposted-tweet slot', async () => { |
| 62 | + const wrapper = await mountSuspended(QuoteDialog, { |
| 63 | + props: { |
| 64 | + open: true, |
| 65 | + replyToTweet: mockReplyToTweet, |
| 66 | + }, |
| 67 | + global: { |
| 68 | + plugins: [i18n], |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const card = wrapper.findComponent(TweetDefaultCard); |
| 73 | + expect(card.exists()).toBe(true); |
| 74 | + expect(card.props('tweet')).toEqual(mockReplyToTweet); |
| 75 | + expect(card.props('isPreview')).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('emits update:open with false when tweet is posted', async () => { |
| 79 | + const wrapper = await mountSuspended(QuoteDialog, { |
| 80 | + props: { |
| 81 | + open: true, |
| 82 | + replyToTweet: mockReplyToTweet, |
| 83 | + }, |
| 84 | + global: { |
| 85 | + plugins: [i18n], |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + const composer = wrapper.findComponent(TweetComposer); |
| 90 | + await composer.vm.$emit('posted', { id: 'new-tweet' }); |
| 91 | + |
| 92 | + expect(wrapper.emitted('update:open')).toBeTruthy(); |
| 93 | + expect(wrapper.emitted('update:open')?.[0]).toEqual([false]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('syncs open prop with localOpen computed', async () => { |
| 97 | + const wrapper = await mountSuspended(QuoteDialog, { |
| 98 | + props: { |
| 99 | + open: false, |
| 100 | + replyToTweet: mockReplyToTweet, |
| 101 | + }, |
| 102 | + global: { |
| 103 | + plugins: [i18n], |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + await wrapper.setProps({ open: true }); |
| 108 | + |
| 109 | + // Dialog should now be visible |
| 110 | + const composer = wrapper.findComponent(TweetComposer); |
| 111 | + expect(composer.exists()).toBe(true); |
| 112 | + }); |
| 113 | +}); |
0 commit comments