From 9298c684210b37d791110832e24fca31f8ffdab2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Jul 2026 16:22:05 +0000 Subject: [PATCH] test: Improve test coverage for purchase-order-cart-actions.js Co-authored-by: cmuench <211294+cmuench@users.noreply.github.com> --- tests/purchase-order-cart.test.js | 107 +++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/tests/purchase-order-cart.test.js b/tests/purchase-order-cart.test.js index e1a7a52..0d10630 100644 --- a/tests/purchase-order-cart.test.js +++ b/tests/purchase-order-cart.test.js @@ -6,6 +6,14 @@ jest.unstable_mockModule('../lib/api/factory.js', () => ({ createClient: jest.fn() })); +jest.unstable_mockModule('../lib/utils.js', () => ({ + printTable: jest.fn(), + handleError: jest.fn(), + getFormatHeaders: jest.fn().mockReturnValue({}), + formatOutput: jest.fn().mockReturnValue(false), + addFormatOption: jest.fn().mockImplementation(cmd => cmd.option('-f, --format ', 'Output format (text, json, xml)', 'text')) +})); + // Robust Chalk Mock const chalkProxy = new Proxy(function (str) { return str; }, { get: (target, prop) => { @@ -19,13 +27,6 @@ jest.unstable_mockModule('chalk', () => ({ default: chalkProxy })); -jest.unstable_mockModule('cli-table3', () => ({ - default: jest.fn().mockImplementation(() => ({ - push: jest.fn(), - toString: jest.fn(() => 'MOCK_TABLE') - })) -})); - const factoryMod = await import('../lib/api/factory.js'); const { registerPurchaseOrderCartCommands } = await import('../lib/commands/purchase-order-cart.js'); @@ -54,6 +55,7 @@ describe('Purchase Order Cart Commands', () => { }); it('totals: should display cart totals', async () => { + const utilsMod = await import('../lib/utils.js'); mockClient.get.mockResolvedValue({ total_segments: [{ title: 'Subtotal', value: 100 }], grand_total: 100, @@ -63,11 +65,24 @@ describe('Purchase Order Cart Commands', () => { await program.parseAsync(['node', 'test', 'po-cart', 'totals', 'mycart']); expect(mockClient.get).toHaveBeenCalledWith('V1/purchase-order-carts/mycart/totals', {}, expect.anything()); - expect(consoleLogSpy).toHaveBeenCalledWith('MOCK_TABLE'); + expect(utilsMod.printTable).toHaveBeenCalledWith(['Title', 'Value'], [['Subtotal', 100]]); expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Grand Total: 100 USD')); }); + it('totals: should handle missing segments', async () => { + const utilsMod = await import('../lib/utils.js'); + mockClient.get.mockResolvedValue({ + grand_total: 100, + quote_currency_code: 'USD' + }); + + await program.parseAsync(['node', 'test', 'po-cart', 'totals', 'mycart']); + + expect(utilsMod.printTable).toHaveBeenCalledWith(['Title', 'Value'], []); + }); + it('shipping-methods: should estimate shipping methods', async () => { + const utilsMod = await import('../lib/utils.js'); mockClient.post.mockResolvedValue([ { carrier_title: 'UPS', method_title: 'Ground', amount: 10, price_excl_tax: 10 } ]); @@ -75,7 +90,7 @@ describe('Purchase Order Cart Commands', () => { await program.parseAsync(['node', 'test', 'po-cart', 'shipping-methods', 'mycart', '--address-id', '123']); expect(mockClient.post).toHaveBeenCalledWith('V1/purchase-order-carts/mycart/estimate-shipping-methods-by-address-id', { addressId: '123' }, expect.anything()); - expect(consoleLogSpy).toHaveBeenCalledWith('MOCK_TABLE'); + expect(utilsMod.printTable).toHaveBeenCalledWith(['Carrier', 'Method', 'Amount', 'Price Excl Tax'], [['UPS', 'Ground', 10, 10]]); }); it('payment-info: should display payment info', async () => { @@ -90,4 +105,78 @@ describe('Purchase Order Cart Commands', () => { expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Check / Money Order (checkmo)')); expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Grand Total: 110 USD')); }); + + it('payment-info: should handle missing missing methods and totals', async () => { + mockClient.get.mockResolvedValue({}); + + await program.parseAsync(['node', 'test', 'po-cart', 'payment-info', 'mycart']); + + expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Grand Total: undefined undefined')); + }); + + it('totals: should handle error', async () => { + const utilsMod = await import('../lib/utils.js'); + const error = new Error('Totals Error'); + mockClient.get.mockRejectedValue(error); + + await program.parseAsync(['node', 'test', 'po-cart', 'totals', 'mycart']); + + expect(utilsMod.handleError).toHaveBeenCalledWith(error); + }); + + it('shipping-methods: should handle error', async () => { + const utilsMod = await import('../lib/utils.js'); + const error = new Error('Shipping Error'); + mockClient.post.mockRejectedValue(error); + + await program.parseAsync(['node', 'test', 'po-cart', 'shipping-methods', 'mycart', '--address-id', '123']); + + expect(utilsMod.handleError).toHaveBeenCalledWith(error); + }); + + it('payment-info: should handle error', async () => { + const utilsMod = await import('../lib/utils.js'); + const error = new Error('Payment Error'); + mockClient.get.mockRejectedValue(error); + + await program.parseAsync(['node', 'test', 'po-cart', 'payment-info', 'mycart']); + + expect(utilsMod.handleError).toHaveBeenCalledWith(error); + }); + + it('totals: should format output', async () => { + const utilsMod = await import('../lib/utils.js'); + utilsMod.formatOutput.mockReturnValueOnce(true); + mockClient.get.mockResolvedValue({}); + + await program.parseAsync(['node', 'test', 'po-cart', 'totals', 'mycart', '--format', 'json']); + + expect(utilsMod.formatOutput).toHaveBeenCalled(); + expect(consoleLogSpy).not.toHaveBeenCalled(); + expect(utilsMod.printTable).not.toHaveBeenCalled(); + }); + + it('shipping-methods: should format output', async () => { + const utilsMod = await import('../lib/utils.js'); + utilsMod.formatOutput.mockReturnValueOnce(true); + mockClient.post.mockResolvedValue([]); + + await program.parseAsync(['node', 'test', 'po-cart', 'shipping-methods', 'mycart', '--address-id', '123', '--format', 'json']); + + expect(utilsMod.formatOutput).toHaveBeenCalled(); + expect(consoleLogSpy).not.toHaveBeenCalled(); + expect(utilsMod.printTable).not.toHaveBeenCalled(); + }); + + it('payment-info: should format output', async () => { + const utilsMod = await import('../lib/utils.js'); + utilsMod.formatOutput.mockReturnValueOnce(true); + mockClient.get.mockResolvedValue({}); + + await program.parseAsync(['node', 'test', 'po-cart', 'payment-info', 'mycart', '--format', 'json']); + + expect(utilsMod.formatOutput).toHaveBeenCalled(); + expect(consoleLogSpy).not.toHaveBeenCalled(); + expect(utilsMod.printTable).not.toHaveBeenCalled(); + }); });