-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathResultDisplay.test.tsx
More file actions
99 lines (86 loc) · 3.7 KB
/
ResultDisplay.test.tsx
File metadata and controls
99 lines (86 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/preact';
import { h } from 'preact';
import { ResultDisplay } from '../components/ResultDisplay';
describe('ResultDisplay', () => {
const mockOnCreateAnother = vi.fn();
const mockResult = {
id: 'test-id',
name: 'Test Feed',
url: 'https://example.com',
strategy: 'faraday',
feed_token: 'test-feed-token',
public_url: 'https://example.com/feed.xml',
json_public_url: 'https://example.com/feed.json',
};
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(window, 'fetch').mockResolvedValue({
ok: true,
json: async () => ({
items: [
{
title: 'Item One',
content_text: '<p>First preview item with <strong>markup</strong>.</p>',
url: 'https://example.com/item-one',
date_published: '2024-01-01T00:00:00Z',
},
{
content_text: '56 points by canpan 1 hour ago | hide | 18 comments',
date_published: '2024-01-02T00:00:00Z',
},
{
content_text: '2. Item Two ( example.com )',
url: 'https://example.com/item-two',
date_published: '2024-01-03T00:00:00Z',
},
],
}),
} as Response);
});
it('renders the success state actions and richer preview cards', async () => {
render(<ResultDisplay result={mockResult} onCreateAnother={mockOnCreateAnother} />);
expect(screen.getByText('Your feed is ready')).toBeInTheDocument();
expect(screen.getByText('Test Feed')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Copy feed URL' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Open feed' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Open JSON Feed' })).toHaveAttribute(
'href',
'https://example.com/feed.json'
);
await waitFor(() => {
expect(screen.getByText('Item One')).toBeInTheDocument();
expect(screen.getByText('First preview item with markup.')).toBeInTheDocument();
expect(screen.getAllByText('Open original')).toHaveLength(2);
expect(screen.getByText(/points by canpan/i)).toBeInTheDocument();
expect(screen.getByText('Item Two')).toBeInTheDocument();
expect(screen.getByText('Latest items from this feed')).toBeInTheDocument();
});
expect(window.fetch).toHaveBeenCalledWith('https://example.com/feed.xml', {
headers: { Accept: 'application/feed+json' },
});
});
it('surfaces preview fetch failures as a result-state message', async () => {
vi.mocked(window.fetch).mockResolvedValueOnce({
ok: false,
json: async () => ({}),
} as Response);
render(<ResultDisplay result={mockResult} onCreateAnother={mockOnCreateAnother} />);
await waitFor(() => {
expect(screen.getByText('Preview unavailable right now.')).toBeInTheDocument();
expect(screen.getByText('Latest items from this feed')).toBeInTheDocument();
});
});
it('calls onCreateAnother when the reset button is clicked', () => {
render(<ResultDisplay result={mockResult} onCreateAnother={mockOnCreateAnother} />);
fireEvent.click(screen.getByRole('button', { name: 'Create another feed' }));
expect(mockOnCreateAnother).toHaveBeenCalled();
});
it('copies feed URL to clipboard when copy button is clicked', async () => {
render(<ResultDisplay result={mockResult} onCreateAnother={mockOnCreateAnother} />);
fireEvent.click(screen.getByRole('button', { name: 'Copy feed URL' }));
await waitFor(() => {
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('https://example.com/feed.xml');
});
});
});