import { render, screen, waitFor, within } from '@testing-library/react' import React from 'react' import apiFetch from '@wordpress/api-fetch' import WhatsNew from '../WhatsNew' import { ChangelogEntry, formatChangelogDate } from '../../model/changelog' import { AutoUpdates } from '../../model/autoUpdates' jest.mock('@wordpress/api-fetch') jest.mock( '@woocommerce/settings', () => ({ getAdminLink: (path: string) => `/wp-admin/${path}` }), { virtual: true } ) const mockApiFetch = apiFetch as jest.MockedFunction const CHANGELOG = '/wp-admin/admin.php?page=wc-admin&path=/posten-bring-checkout/changelog' const entry = ( slug: string, over: Partial = {} ): ChangelogEntry => ({ slug, title: `Change ${slug}`, date: '2026-10-20T00:00:00+00:00', type: 'new', version: '1.1.70', installed: true, contentHtml: '

What changed.

', ...over, }) const respond = ( entries: Array, feedOk = true, autoUpdates: Promise = Promise.resolve({ enabled: false, canChange: true, }) ) => mockApiFetch.mockImplementation((({ path }: { path: string }) => path === '/posten-bring-checkout/auto-updates' ? autoUpdates : Promise.resolve({ feedOk, currentVersion: '1.1.70', entries, })) as never) describe('WhatsNew', () => { beforeEach(() => { jest.clearAllMocks() }) it('shows the three latest changes', async () => { respond([entry('a'), entry('b'), entry('c'), entry('d')]) render() expect(await screen.findByText('Change a')).toBeInTheDocument() expect(screen.getByText('Change b')).toBeInTheDocument() expect(screen.getByText('Change c')).toBeInTheDocument() expect(screen.queryByText('Change d')).not.toBeInTheDocument() }) it('links each change to its entry in the changelog', async () => { respond([entry('automatic-label-printing')]) render() expect( await screen.findByRole('link', { name: 'Change automatic-label-printing', }) ).toHaveAttribute('href', `${CHANGELOG}#automatic-label-printing`) }) it('shows the date and type of each change', async () => { respond([entry('a', { type: 'fix' })]) render() expect( await screen.findByText(formatChangelogDate('2026-10-20T00:00:00+00:00')) ).toBeInTheDocument() expect(screen.getByText('Fix')).toBeInTheDocument() }) it('links to all updates', async () => { respond([entry('a')]) render() expect( await screen.findByRole('link', { name: 'See all updates' }) ).toHaveAttribute('href', CHANGELOG) }) it('shows nothing when there are no changes', async () => { respond([], false) const { container } = render() await waitFor(() => expect(container).toBeEmptyDOMElement()) }) it('shows nothing when the changelog cannot be read', async () => { mockApiFetch.mockRejectedValue(new Error('offline')) const { container } = render() await waitFor(() => expect(container).toBeEmptyDOMElement()) }) describe('changes that need a newer version', () => { const PLUGINS = '/wp-admin/plugins.php' it('still shows the latest changes when they are not installed yet', async () => { respond([entry('a', { installed: false }), entry('b')]) render() expect(await screen.findByText('Change a')).toBeInTheDocument() expect(screen.getByText('Change b')).toBeInTheDocument() }) it('marks each change that needs an update', async () => { respond([entry('a', { installed: false }), entry('b')]) render() const notInstalled = (await screen.findByText('Change a')).closest( 'div' )!.parentElement! expect( within(notInstalled).getByText('Requires update') ).toBeInTheDocument() expect(screen.getAllByText('Requires update')).toHaveLength(1) }) it('lets a user who may update the plugin go and do so', async () => { respond([entry('a', { installed: false })]) render() expect( await screen.findByText('A newer version of the plugin is available.') ).toBeInTheDocument() expect( screen.getByRole('link', { name: 'Update the plugin' }) ).toHaveAttribute('href', PLUGINS) }) it('asks other users to have an administrator update the plugin', async () => { respond([entry('a', { installed: false })]) render() expect( await screen.findByText( 'A newer version of the plugin is available. Ask your administrator to update it.' ) ).toBeInTheDocument() expect( screen.queryByRole('link', { name: 'Update the plugin' }) ).not.toBeInTheDocument() }) it('leaves updating to WordPress when automatic updates are on', async () => { respond( [entry('a', { installed: false })], true, Promise.resolve({ enabled: true, canChange: true }) ) render() await screen.findByText('Change a') await waitFor(() => expect(mockApiFetch).toHaveBeenCalledTimes(2)) expect(screen.queryByText(/newer version/)).not.toBeInTheDocument() // The change itself is still marked as not installed yet expect(screen.getByText('Requires update')).toBeInTheDocument() }) it('says nothing about updating until it knows whether automatic updates are on', async () => { respond([entry('a', { installed: false })], true, new Promise(() => {})) render() await screen.findByText('Change a') expect(screen.queryByText(/newer version/)).not.toBeInTheDocument() }) it('says nothing about updating when every change is installed', async () => { respond([entry('a'), entry('b')]) render() await screen.findByText('Change a') expect(screen.queryByText(/newer version/)).not.toBeInTheDocument() expect(screen.queryByText('Requires update')).not.toBeInTheDocument() }) }) })