-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathResultDisplay.test.tsx
More file actions
140 lines (124 loc) · 4.67 KB
/
ResultDisplay.test.tsx
File metadata and controls
140 lines (124 loc) · 4.67 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 = {
feed: {
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',
},
preview: {
items: [
{
title: 'Item One',
excerpt: 'First preview item with markup.',
url: 'https://example.com/item-one',
publishedLabel: 'Jan 1, 2024',
},
{
title: '56 points by canpan 1 hour ago | hide | 18 comments',
excerpt: '',
publishedLabel: 'Jan 2, 2024',
},
{
title: 'Item Two',
excerpt: '',
url: 'https://example.com/item-two',
publishedLabel: 'Jan 3, 2024',
},
],
error: null,
isLoading: false,
},
retry: null,
};
beforeEach(() => {
vi.clearAllMocks();
});
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: 'Subscribe in reader' })).toHaveAttribute(
'href',
'feed:https://example.com/feed.xml'
);
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();
});
});
it('surfaces preview failures as a result-state message', async () => {
render(
<ResultDisplay
result={{
...mockResult,
preview: { items: [], error: 'Preview unavailable right now.', isLoading: false },
}}
onCreateAnother={mockOnCreateAnother}
/>
);
await waitFor(() => {
expect(screen.getByText('Preview unavailable right now.')).toBeInTheDocument();
expect(screen.getByText('Latest items from this feed')).toBeInTheDocument();
});
});
it('keeps the result state visible while preview is still loading', async () => {
render(
<ResultDisplay
result={{ ...mockResult, preview: { items: [], error: null, isLoading: true } }}
onCreateAnother={mockOnCreateAnother}
/>
);
await waitFor(() => {
expect(screen.getByText('Your feed is ready')).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Open feed' })).toBeInTheDocument();
expect(screen.getByText('Loading preview…')).toBeInTheDocument();
});
});
it('shows an automatic retry notice when fallback strategy succeeded', async () => {
render(
<ResultDisplay
result={{
...mockResult,
retry: { automatic: true, from: 'faraday', to: 'browserless' },
}}
onCreateAnother={mockOnCreateAnother}
/>
);
await waitFor(() => {
expect(
screen.getByText('Retried automatically with browserless after faraday could not finish the page.')
).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');
});
});
});