|
| 1 | +import React from 'react'; |
| 2 | +import { screen, fireEvent } from '@testing-library/react-native'; |
| 3 | +import renderWithProvider from '../../../../util/test/renderWithProvider'; |
| 4 | +import ArticleRow from './ArticleRow'; |
| 5 | +import type { Article } from '@metamask/ai-controllers'; |
| 6 | + |
| 7 | +const mockArticle: Article = { |
| 8 | + title: 'Bitcoin ETF inflows hit record high', |
| 9 | + url: 'https://coindesk.com/news/btc-etf-inflows', |
| 10 | + source: 'CoinDesk', |
| 11 | + date: '2026-03-15T10:00:00.000Z', |
| 12 | +}; |
| 13 | + |
| 14 | +const mockArticleNoDate: Article = { |
| 15 | + title: 'Ethereum upgrade expected next quarter', |
| 16 | + url: 'https://theblock.co/news/eth-upgrade', |
| 17 | + source: 'The Block', |
| 18 | + date: '', |
| 19 | +}; |
| 20 | + |
| 21 | +describe('ArticleRow', () => { |
| 22 | + it('renders the article title', () => { |
| 23 | + renderWithProvider( |
| 24 | + <ArticleRow article={mockArticle} onPress={jest.fn()} />, |
| 25 | + ); |
| 26 | + expect(screen.getByText(mockArticle.title)).toBeOnTheScreen(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders the article source', () => { |
| 30 | + renderWithProvider( |
| 31 | + <ArticleRow article={mockArticle} onPress={jest.fn()} />, |
| 32 | + ); |
| 33 | + expect(screen.getByText(mockArticle.source)).toBeOnTheScreen(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders relative date when article has a date', () => { |
| 37 | + renderWithProvider( |
| 38 | + <ArticleRow article={mockArticle} onPress={jest.fn()} />, |
| 39 | + ); |
| 40 | + // A separator dot is shown between source and date |
| 41 | + expect(screen.getByText('•')).toBeOnTheScreen(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('does not render date separator when article has no date', () => { |
| 45 | + renderWithProvider( |
| 46 | + <ArticleRow article={mockArticleNoDate} onPress={jest.fn()} />, |
| 47 | + ); |
| 48 | + expect(screen.queryByText('•')).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('calls onPress with the article URL when tapped', () => { |
| 52 | + const onPress = jest.fn(); |
| 53 | + renderWithProvider(<ArticleRow article={mockArticle} onPress={onPress} />); |
| 54 | + fireEvent.press(screen.getByText(mockArticle.title)); |
| 55 | + expect(onPress).toHaveBeenCalledTimes(1); |
| 56 | + expect(onPress).toHaveBeenCalledWith(mockArticle.url); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders a bottom border when isLastItem is false', () => { |
| 60 | + const { toJSON } = renderWithProvider( |
| 61 | + <ArticleRow |
| 62 | + article={mockArticle} |
| 63 | + onPress={jest.fn()} |
| 64 | + isLastItem={false} |
| 65 | + />, |
| 66 | + ); |
| 67 | + // Tailwind compiles 'border-b border-muted' to resolved style props at render time. |
| 68 | + const json = JSON.stringify(toJSON()); |
| 69 | + expect(json).toContain('"borderBottomWidth":1'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('does not render a bottom border when isLastItem is true', () => { |
| 73 | + const { toJSON } = renderWithProvider( |
| 74 | + <ArticleRow article={mockArticle} onPress={jest.fn()} isLastItem />, |
| 75 | + ); |
| 76 | + const json = JSON.stringify(toJSON()); |
| 77 | + expect(json).not.toContain('"borderBottomWidth":1'); |
| 78 | + }); |
| 79 | +}); |
0 commit comments