import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import apiFetch from '@wordpress/api-fetch'; import ShipmentTracking, { formatStage } from '../features/shipments/shipment-tracking'; import { captureException } from '../shared/sentry'; import type { ShipmentTracking as Tracking } from '../types'; jest.mock( '@wordpress/api-fetch', () => jest.fn() ); jest.mock( '../shared/sentry', () => ( { captureException: jest.fn() } ) ); const mockedFetch = apiFetch as unknown as jest.Mock; ( globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; let container: HTMLDivElement; let root: Root; beforeEach( () => { container = document.createElement( 'div' ); document.body.appendChild( container ); root = createRoot( container ); } ); afterEach( () => { act( () => root.unmount() ); container.remove(); document.body.innerHTML = ''; jest.clearAllMocks(); } ); const tracking = ( overrides: Partial< Tracking > = {} ): Tracking => ( { currentStage: 'InTransit', parcels: [ { number: 'PN1', currentStage: 'InTransit', events: [ { description: 'Parcel collected', timestamp: '2026-11-20T10:00:00', stage: 'Collected' }, { description: 'Arrived at hub', timestamp: '2026-11-21T08:30:00', stage: 'InTransit' }, ], images: [ { key: 'proof', value: 'QUJD' } ], }, { number: 'PN2', currentStage: 'Collected', events: [ { description: 'Parcel collected', timestamp: '2026-11-20T10:00:00', stage: 'Collected' } ], images: [], }, ], ...overrides, } ); const button = () => container.querySelector( 'button' ) as HTMLButtonElement; const modal = () => document.querySelector( '.components-modal__frame' ) as HTMLElement | null; async function mount() { await act( async () => { root.render( ); } ); } async function openTracking() { await act( async () => { button().click(); } ); } describe( 'formatStage', () => { it( 'labels the known stages', () => { expect( formatStage( 'InTransit' ) ).toBe( 'In transit' ); expect( formatStage( 'DeliveryScheduled' ) ).toBe( 'Delivery scheduled' ); } ); it( 'splits unknown stages and leaves empty ones empty', () => { expect( formatStage( 'AwaitingShopPickup' ) ).toBe( 'Awaiting shop pickup' ); expect( formatStage( '' ) ).toBe( '' ); } ); } ); describe( 'ShipmentTracking', () => { it( 'loads tracking when it shows and says it is checking meanwhile', async () => { mockedFetch.mockReturnValue( new Promise( () => {} ) ); await mount(); expect( mockedFetch ).toHaveBeenCalledWith( { path: '/parcel2go-shipping/v1/shipments/42/tracking' } ); expect( container.textContent ).toContain( 'Tracking' ); expect( button().textContent ).toBe( 'Checking…' ); expect( modal() ).toBeNull(); } ); it( 'shows a spinner in the pop-up while loading', async () => { mockedFetch.mockReturnValue( new Promise( () => {} ) ); await mount(); await openTracking(); expect( modal()?.querySelector( '.components-spinner' ) ).not.toBeNull(); } ); it( 'shows the stage, then lists each parcel with its images and events without fetching again', async () => { mockedFetch.mockResolvedValue( tracking() ); await mount(); expect( button().textContent ).toBe( 'In transit' ); await openTracking(); const text = modal()?.textContent ?? ''; expect( text ).toContain( 'PN1' ); expect( text ).toContain( 'PN2' ); expect( text ).toContain( 'Arrived at hub' ); expect( modal()?.querySelectorAll( 'li' ) ).toHaveLength( 3 ); expect( modal()?.querySelector( 'img' )?.getAttribute( 'src' ) ).toBe( 'data:image/jpeg;base64,QUJD' ); expect( mockedFetch ).toHaveBeenCalledTimes( 1 ); } ); it( 'says it is awaiting the courier when there are no events', async () => { mockedFetch.mockResolvedValue( { currentStage: '', parcels: [ { number: 'PN1', currentStage: '', events: [], images: [] } ], } ); await mount(); expect( button().textContent ).toBe( 'Awaiting courier' ); await openTracking(); expect( modal()?.textContent ).toContain( 'Awaiting tracking data from courier' ); // Nothing from the courier yet, so opening asks again. expect( mockedFetch ).toHaveBeenCalledTimes( 2 ); } ); it( 'loads tracking again on open once it is over 5 minutes old', async () => { const now = jest.spyOn( Date, 'now' ).mockReturnValue( 1_000_000 ); mockedFetch.mockResolvedValueOnce( tracking() ).mockResolvedValueOnce( tracking( { currentStage: 'Delivered' } ) ); await mount(); expect( button().textContent ).toBe( 'In transit' ); now.mockReturnValue( 1_000_000 + 5 * 60 * 1000 + 1 ); await openTracking(); expect( mockedFetch ).toHaveBeenCalledTimes( 2 ); expect( button().textContent ).toBe( 'Delivered' ); expect( captureException ).not.toHaveBeenCalled(); now.mockRestore(); } ); it( 'keeps a failed preload quiet, then retries, shows the error and reports only the error on open', async () => { const error = Object.assign( new Error( 'Could not load tracking from Parcel2Go. Please try again.' ), { code: 'p2g_tracking_failed', } ); mockedFetch.mockRejectedValue( error ); await mount(); expect( button().textContent ).toBe( 'View tracking' ); expect( captureException ).not.toHaveBeenCalled(); await openTracking(); expect( mockedFetch ).toHaveBeenCalledTimes( 2 ); expect( modal()?.querySelector( '.components-notice.is-error' )?.textContent ).toContain( 'Could not load tracking from Parcel2Go.' ); expect( captureException ).toHaveBeenCalledTimes( 1 ); expect( ( captureException as jest.Mock ).mock.calls[ 0 ] ).toEqual( [ error ] ); } ); } );