diff --git a/changelog/dev-add-unit-tests-for-diputed-order-notice b/changelog/dev-add-unit-tests-for-diputed-order-notice new file mode 100644 index 00000000000..8d88338d894 --- /dev/null +++ b/changelog/dev-add-unit-tests-for-diputed-order-notice @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Add Jest tests for the disputed order notices diff --git a/client/components/disputed-order-notice/test/__snapshots__/index.test.js.snap b/client/components/disputed-order-notice/test/__snapshots__/index.test.js.snap new file mode 100644 index 00000000000..4c3f5a3611a --- /dev/null +++ b/client/components/disputed-order-notice/test/__snapshots__/index.test.js.snap @@ -0,0 +1,91 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DisputedOrderNoticeHandler renders regular dispute notice 1`] = ` +
+
+
+
+
+ + This order has a payment dispute for $10.00 for the reason 'Transaction unauthorized'. + + + Please respond before Oct 28, 2023. +
+ +
+
+
+
+
+
+
+`; + +exports[`DisputedOrderNoticeHandler renders urgent dispute notice 1`] = ` +
+
+
+
+
+ + Please resolve the dispute on this order of $10.00 labeled 'Transaction unauthorized' by Oct 28, 2023. + + + (Last day today) +
+ +
+
+
+
+
+
+
+`; diff --git a/client/components/disputed-order-notice/test/index.test.js b/client/components/disputed-order-notice/test/index.test.js new file mode 100644 index 00000000000..7e44da132e0 --- /dev/null +++ b/client/components/disputed-order-notice/test/index.test.js @@ -0,0 +1,111 @@ +/** + * External dependencies + */ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +/** + * Internal dependencies + */ +import DisputedOrderNoticeHandler from '../index'; +import { useCharge } from 'wcpay/data'; + +jest.mock( 'wcpay/data', () => ( { + useCharge: jest.fn(), +} ) ); + +jest.mock( 'tracks', () => ( { + recordEvent: jest.fn(), +} ) ); + +describe( 'DisputedOrderNoticeHandler', () => { + const mockCharge = { + dispute: { + status: 'needs_response', + reason: 'fraudulent', + amount: 1000, + currency: 'USD', + evidence_details: { + due_by: 1698500219, + }, + }, + }; + + beforeEach( () => { + window.wcpaySettings = { + zeroDecimalCurrencies: [], + connect: { + country: 'US', + }, + }; + useCharge.mockReturnValue( { data: mockCharge } ); + } ); + + afterEach( () => { + jest.useRealTimers(); + jest.clearAllMocks(); + } ); + + test( 'renders urgent dispute notice', () => { + const fixedDate = new Date( '2023-10-28T00:00:00Z' ); + jest.useFakeTimers(); + jest.setSystemTime( fixedDate ); + + const { container } = render( + + ); + const disputeMessages = screen.getAllByText( + /Please resolve the dispute on this order of/ + ); + expect( disputeMessages[ 0 ] ).toBeInTheDocument(); + expect( screen.getByRole( 'button' ) ).toHaveTextContent( + 'Respond today' + ); + expect( container ).toMatchSnapshot(); + } ); + + test( 'renders regular dispute notice', () => { + const fixedDate = new Date( '2023-10-20T00:00:00Z' ); + jest.useFakeTimers(); + jest.setSystemTime( fixedDate ); + + const { container } = render( + + ); + const disputeMessages = screen.getAllByText( /Please respond before/ ); + expect( disputeMessages[ 0 ] ).toBeInTheDocument(); + expect( screen.getByRole( 'button' ) ).toHaveTextContent( + 'Respond now' + ); + expect( container ).toMatchSnapshot(); + } ); + + test( 'does not render notice if no dispute', () => { + useCharge.mockReturnValue( { data: {} } ); + const { container } = render( + + ); + expect( container ).toBeEmptyDOMElement(); + } ); + + test( 'does not render notice if dispute is not awaiting response', () => { + mockCharge.dispute.status = 'won'; + render( + + ); + expect( + screen.queryByText( /Please resolve the dispute on this order of/ ) + ).not.toBeInTheDocument(); + } ); +} );