import { easypostMethods } from '../src/methods/easypost';
import { EasyPostClient } from '../src/clients/easypostClient';
import { ValidationError } from '../src/utils/errors';
// Mock the EasyPost client
jest.mock('../src/clients/easypostClient');
describe('EasyPost Methods', () => {
const mockClient = EasyPostClient as jest.MockedClass<typeof EasyPostClient>;
beforeEach(() => {
mockClient.mockClear();
});
describe('createShipment', () => {
it('should create a shipment successfully', async () => {
const mockShipment = {
id: 'shp_123',
to_address: { id: 'adr_to' },
from_address: { id: 'adr_from' },
parcel: { length: 10, width: 8, height: 4, weight: 2 },
status: 'unknown'
};
mockClient.prototype.createShipment = jest.fn().mockResolvedValue(mockShipment);
const params = {
to_address: { name: 'John Doe', street1: '123 Main St', city: 'Anytown', state: 'CA', zip: '12345', country: 'US' },
from_address: { name: 'Jane Smith', street1: '456 Oak Ave', city: 'Another City', state: 'NY', zip: '67890', country: 'US' }
};
const result = await easypostMethods.createShipment(params);
expect(result).toEqual({
id: 'shp_123',
to_address: { id: 'adr_to' },
from_address: { id: 'adr_from' },
parcel: { length: 10, width: 8, height: 4, weight: 2 },
status: 'unknown'
});
});
it('should throw validation error if addresses are missing', async () => {
await expect(easypostMethods.createShipment({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('getRates', () => {
it('should get rates successfully', async () => {
const mockRates = [
{
id: 'rate_1',
service: 'Ground',
carrier: 'USPS',
rate: '5.50',
currency: 'USD',
delivery_days: 3
}
];
mockClient.prototype.getRates = jest.fn().mockResolvedValue(mockRates);
const result = await easypostMethods.getRates({ shipment_id: 'shp_123' });
expect(result).toEqual([
{
id: 'rate_1',
service: 'Ground',
carrier: 'USPS',
rate: '5.50',
currency: 'USD',
delivery_days: 3,
delivery_date: undefined
}
]);
});
it('should throw validation error if shipment_id is missing', async () => {
await expect(easypostMethods.getRates({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('buyLabel', () => {
it('should buy a label 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'
};
mockClient.prototype.buyLabel = jest.fn().mockResolvedValue(mockLabel);
const result = await easypostMethods.buyLabel({
shipment_id: 'shp_123',
rate_id: 'rate_1'
});
expect(result).toEqual({
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'
});
});
it('should throw validation error if shipment_id or rate_id is missing', async () => {
await expect(easypostMethods.buyLabel({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('trackShipment', () => {
it('should track a shipment successfully', async () => {
const mockTracker = {
id: 'trk_1',
tracking_code: '9400111899221111111111',
status: 'delivered',
carrier: 'USPS',
signed_by: 'John Doe',
weight: 2.5,
est_delivery_date: '2023-01-05',
shipment_id: 'shp_123',
tracking_details: [],
mode: 'test'
};
mockClient.prototype.trackShipment = jest.fn().mockResolvedValue(mockTracker);
const result = await easypostMethods.trackShipment({ tracking_code: '9400111899221111111111' });
expect(result).toEqual({
id: 'trk_1',
tracking_code: '9400111899221111111111',
status: 'delivered',
carrier: 'USPS',
signed_by: 'John Doe',
weight: 2.5,
est_delivery_date: '2023-01-05',
tracking_details: []
});
});
it('should throw validation error if tracking_code is missing', async () => {
await expect(easypostMethods.trackShipment({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('refundLabel', () => {
it('should refund a label successfully', async () => {
mockClient.prototype.refundLabel = jest.fn().mockResolvedValue(true);
const result = await easypostMethods.refundLabel({ shipment_id: 'shp_123' });
expect(result).toEqual({ success: true });
});
it('should throw validation error if shipment_id is missing', async () => {
await expect(easypostMethods.refundLabel({} as any))
.rejects
.toThrow(ValidationError);
});
});
});