import { unifiedMethods } from '../src/methods/unified';
import { veeqoMethods } from '../src/methods/veeqo';
import { easypostMethods } from '../src/methods/easypost';
import { ValidationError } from '../src/utils/errors';
// Mock the dependent methods
jest.mock('../src/methods/veeqo');
jest.mock('../src/methods/easypost');
describe('Unified Methods', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('createOrderWithLabel', () => {
it('should create order and label successfully', async () => {
const mockVeeqoOrder = {
id: 'ord_123',
number: 'V123456',
status: 'pending',
channel_order_code: 'CH123',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-01T00:00:00Z'
};
const mockEasypostShipment = {
id: 'shp_123',
to_address: { id: 'adr_to' },
from_address: { id: 'adr_from' },
parcel: { length: 10, width: 8, height: 4, weight: 2 },
status: 'unknown'
};
const mockLabel = {
id: 'label_1',
tracking_code: '9400111899221111111111',
shipment_id: 'shp_123',
rate: {
service: 'Ground',
carrier: 'USPS',
rate: '5.50',
currency: 'USD'
},
label_url: 'https://example.com/label.pdf'
};
const mockUpdatedOrder = {
id: 'ord_123',
number: 'V123456',
status: 'shipped',
channel_order_code: 'CH123',
updated_at: '2023-01-01T01:00:00Z'
};
(veeqoMethods.createOrder as jest.Mock).mockResolvedValue(mockVeeqoOrder);
(easypostMethods.createShipment as jest.Mock).mockResolvedValue(mockEasypostShipment);
(easypostMethods.buyLabel as jest.Mock).mockResolvedValue(mockLabel);
(veeqoMethods.updateOrder as jest.Mock).mockResolvedValue(mockUpdatedOrder);
const params = {
order: {
deliver_to: {
first_name: 'John',
last_name: 'Doe',
address1: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '12345',
country: 'US'
},
line_items_attributes: [
{ product_id: 'prod_1', quantity: 1 }
]
},
from_address: {
name: 'Jane Smith',
street1: '456 Oak Ave',
city: 'Another City',
state: 'NY',
zip: '67890',
country: 'US'
},
rate_id: 'rate_1'
};
const result = await unifiedMethods.createOrderWithLabel(params);
expect(result).toEqual({
order: mockUpdatedOrder,
shipment: {
id: 'shp_123',
tracking_code: '9400111899221111111111',
label_url: 'https://example.com/label.pdf'
}
});
expect(veeqoMethods.createOrder).toHaveBeenCalled();
expect(easypostMethods.createShipment).toHaveBeenCalled();
expect(easypostMethods.buyLabel).toHaveBeenCalled();
expect(veeqoMethods.updateOrder).toHaveBeenCalled();
});
it('should throw validation error if required params are missing', async () => {
await expect(unifiedMethods.createOrderWithLabel({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('syncTracking', () => {
it('should sync tracking successfully', async () => {
const mockRates = [
{
id: 'rate_1',
service: 'Ground',
carrier: 'USPS',
rate: '5.50',
currency: 'USD',
tracking_code: '9400111899221111111111'
}
];
const mockTracking = {
id: 'trk_1',
tracking_code: '9400111899221111111111',
status: 'delivered',
carrier: 'USPS',
signed_by: 'John Doe',
est_delivery_date: '2023-01-05'
};
const mockUpdatedOrder = {
id: 'ord_123',
number: 'V123456',
status: 'shipped',
channel_order_code: 'CH123',
updated_at: '2023-01-01T01:00:00Z'
};
(easypostMethods.getRates as jest.Mock).mockResolvedValue(mockRates);
(easypostMethods.trackShipment as jest.Mock).mockResolvedValue(mockTracking);
(veeqoMethods.updateOrder as jest.Mock).mockResolvedValue(mockUpdatedOrder);
const result = await unifiedMethods.syncTracking({
order_id: 'ord_123',
shipment_id: 'shp_123'
});
expect(result).toEqual({
order: mockUpdatedOrder,
tracking: {
tracking_code: '9400111899221111111111',
status: 'delivered',
carrier: 'USPS',
signed_by: 'John Doe',
est_delivery_date: '2023-01-05'
}
});
});
it('should throw validation error if required params are missing', async () => {
await expect(unifiedMethods.syncTracking({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('fulfillOrder', () => {
it('should fulfill order successfully', async () => {
const mockLabel = {
id: 'label_1',
tracking_code: '9400111899221111111111',
shipment_id: 'shp_123',
rate: {
service: 'Ground',
carrier: 'USPS',
rate: '5.50',
currency: 'USD'
},
label_url: 'https://example.com/label.pdf'
};
const mockUpdatedOrder = {
id: 'ord_123',
number: 'V123456',
status: 'shipped',
channel_order_code: 'CH123',
updated_at: '2023-01-01T01:00:00Z'
};
(easypostMethods.buyLabel as jest.Mock).mockResolvedValue(mockLabel);
(veeqoMethods.updateOrder as jest.Mock).mockResolvedValue(mockUpdatedOrder);
const result = await unifiedMethods.fulfillOrder({
order_id: 'ord_123',
shipment_id: 'shp_123',
rate_id: 'rate_1'
});
expect(result).toEqual({
order: mockUpdatedOrder,
label: {
tracking_code: '9400111899221111111111',
label_url: 'https://example.com/label.pdf',
carrier: 'USPS',
service: 'Ground'
}
});
});
it('should throw validation error if required params are missing', async () => {
await expect(unifiedMethods.fulfillOrder({} as any))
.rejects
.toThrow(ValidationError);
});
});
});