import { veeqoMethods } from '../src/methods/veeqo';
import { VeeqoClient } from '../src/clients/veeqoClient';
import { ValidationError } from '../src/utils/errors';
// Mock the Veeqo client
jest.mock('../src/clients/veeqoClient');
describe('Veeqo Methods', () => {
const mockClient = VeeqoClient as jest.MockedClass<typeof VeeqoClient>;
beforeEach(() => {
mockClient.mockClear();
});
describe('createOrder', () => {
it('should create an order successfully', async () => {
const mockOrder = {
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'
};
mockClient.prototype.createOrder = jest.fn().mockResolvedValue(mockOrder);
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 }
]
}
};
const result = await veeqoMethods.createOrder(params);
expect(result).toEqual({
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'
});
});
it('should throw validation error if order is missing', async () => {
await expect(veeqoMethods.createOrder({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('getOrder', () => {
it('should get an order successfully', async () => {
const mockOrder = {
id: 'ord_123',
number: 'V123456',
status: 'pending',
channel_order_code: 'CH123',
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 }
],
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-01T00:00:00Z'
};
mockClient.prototype.getOrder = jest.fn().mockResolvedValue(mockOrder);
const result = await veeqoMethods.getOrder({ order_id: 'ord_123' });
expect(result).toEqual({
id: 'ord_123',
number: 'V123456',
status: 'pending',
channel_order_code: 'CH123',
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 }
],
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-01T00:00:00Z'
});
});
it('should throw validation error if order_id is missing', async () => {
await expect(veeqoMethods.getOrder({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('listOrders', () => {
it('should list orders successfully', async () => {
const mockResponse = {
orders: [
{
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'
}
],
page: 1,
per_page: 20,
total_pages: 1,
total_count: 1
};
mockClient.prototype.listOrders = jest.fn().mockResolvedValue(mockResponse);
const result = await veeqoMethods.listOrders({});
expect(result).toEqual({
orders: [
{
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'
}
],
page: 1,
per_page: 20,
total_pages: 1,
total_count: 1
});
});
});
describe('updateOrder', () => {
it('should update an order successfully', async () => {
const mockOrder = {
id: 'ord_123',
number: 'V123456',
status: 'shipped',
channel_order_code: 'CH123',
updated_at: '2023-01-01T01:00:00Z'
};
mockClient.prototype.updateOrder = jest.fn().mockResolvedValue(mockOrder);
const result = await veeqoMethods.updateOrder({
order_id: 'ord_123',
order: { status: 'shipped' }
});
expect(result).toEqual({
id: 'ord_123',
number: 'V123456',
status: 'shipped',
channel_order_code: 'CH123',
updated_at: '2023-01-01T01:00:00Z'
});
});
it('should throw validation error if order_id is missing', async () => {
await expect(veeqoMethods.updateOrder({ order: {} } as any))
.rejects
.toThrow(ValidationError);
});
});
describe('deleteOrder', () => {
it('should delete an order successfully', async () => {
mockClient.prototype.deleteOrder = jest.fn().mockResolvedValue(true);
const result = await veeqoMethods.deleteOrder({ order_id: 'ord_123' });
expect(result).toEqual({ success: true });
});
it('should throw validation error if order_id is missing', async () => {
await expect(veeqoMethods.deleteOrder({} as any))
.rejects
.toThrow(ValidationError);
});
});
describe('syncInventory', () => {
it('should sync inventory successfully', async () => {
const mockItems = [
{
id: 'item_1',
product_id: 'prod_1',
sellable_id: 'sell_1',
quantity: 10,
allocated: 2,
available: 8
}
];
mockClient.prototype.syncInventory = jest.fn().mockResolvedValue(mockItems);
const result = await veeqoMethods.syncInventory({});
expect(result).toEqual([
{
id: 'item_1',
product_id: 'prod_1',
sellable_id: 'sell_1',
quantity: 10,
allocated: 2,
available: 8
}
]);
});
});
});