Created
February 18, 2025 03:40
DAPR C# to Typescript workflow unit test conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { jest } from '@jest/globals'; | |
import { OrderProcessingWorkflow } from '../src/workflows/OrderProcessingWorkflow'; | |
import { ReserveInventoryActivity } from '../src/activities/ReserveInventoryActivity'; | |
import { ProcessPaymentActivity } from '../src/activities/ProcessPaymentActivity'; | |
import { NotifyActivity } from '../src/activities/NotifyActivity'; | |
import { WorkflowContext } from 'dapr-workflow'; | |
interface OrderPayload { | |
name: string; | |
totalCost: number; | |
quantity: number; | |
} | |
interface PaymentRequest { | |
orderId: string; | |
name: string; | |
quantity: number; | |
totalCost: number; | |
} | |
interface InventoryRequest { | |
orderId: string; | |
name: string; | |
quantity: number; | |
} | |
interface InventoryResult { | |
success: boolean; | |
item: { name: string; price: number; quantity: number } | null; | |
} | |
describe('OrderProcessingTests', () => { | |
test('TestSuccessfulOrder', async () => { | |
const order: OrderPayload = { name: 'Paperclips', totalCost: 99.95, quantity: 10 }; | |
const expectedPaymentRequest: PaymentRequest = { orderId: expect.any(String), ...order }; | |
const expectedInventoryRequest: InventoryRequest = { orderId: expect.any(String), name: order.name, quantity: order.quantity }; | |
const inventoryResult: InventoryResult = { success: true, item: { name: order.name, price: 9.99, quantity: order.quantity } }; | |
const mockContext: jest.Mocked<WorkflowContext> = { | |
callActivityAsync: jest.fn().mockImplementation((activityName, input) => { | |
if (activityName === 'ReserveInventoryActivity') return Promise.resolve(inventoryResult); | |
return Promise.resolve(); | |
}), | |
} as unknown as jest.Mocked<WorkflowContext>; | |
const workflow = new OrderProcessingWorkflow(); | |
const result = await workflow.run(mockContext, order); | |
expect(result).not.toBeNull(); | |
expect(result.processed).toBe(true); | |
expect(mockContext.callActivityAsync).toHaveBeenCalledWith('ReserveInventoryActivity', expectedInventoryRequest, expect.any(Object)); | |
expect(mockContext.callActivityAsync).toHaveBeenCalledWith('ProcessPaymentActivity', expectedPaymentRequest, expect.any(Object)); | |
expect(mockContext.callActivityAsync).toHaveBeenCalledTimes(4); // 2x NotifyActivity calls | |
}); | |
test('TestInsufficientInventory', async () => { | |
const order: OrderPayload = { name: 'Paperclips', totalCost: 99.95, quantity: Number.MAX_SAFE_INTEGER }; | |
const expectedInventoryRequest: InventoryRequest = { orderId: expect.any(String), name: order.name, quantity: order.quantity }; | |
const inventoryResult: InventoryResult = { success: false, item: null }; | |
const mockContext: jest.Mocked<WorkflowContext> = { | |
callActivityAsync: jest.fn().mockImplementation((activityName, input) => { | |
if (activityName === 'ReserveInventoryActivity') return Promise.resolve(inventoryResult); | |
return Promise.resolve(); | |
}), | |
} as unknown as jest.Mocked<WorkflowContext>; | |
const workflow = new OrderProcessingWorkflow(); | |
await workflow.run(mockContext, order); | |
expect(mockContext.callActivityAsync).toHaveBeenCalledWith('ReserveInventoryActivity', expectedInventoryRequest, expect.any(Object)); | |
expect(mockContext.callActivityAsync).not.toHaveBeenCalledWith('ProcessPaymentActivity', expect.anything(), expect.any(Object)); | |
expect(mockContext.callActivityAsync).toHaveBeenCalledTimes(3); // 2x NotifyActivity calls | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment