Created
October 22, 2021 23:46
-
-
Save Pyrolistical/6cdbc114a6ee6c4ed90a0530364fbbc9 to your computer and use it in GitHub Desktop.
ecommerce-escalation v2 test
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 Service, {InsufficientStock} from './service'; | |
describe('checkout', () => { | |
test('fails when insufficient stock for desired product', async () => { | |
const session = { | |
findInventoryByProuctSkus: jest.fn() | |
.mockResolvedValueOnce([ | |
{ | |
productSku: 'some product sku', | |
stock: 2 | |
} | |
]), | |
updateInventoryStock: jest.fn(), | |
createOrder: jest.fn() | |
}; | |
const repository = { | |
async checkoutTransaction(closure) { | |
return closure(session); | |
} | |
}; | |
const service = Service(repository); | |
await expect(service.checkout([ | |
{ | |
productSku: 'some product sku', | |
quantity: 10 | |
} | |
], 'some shipping address', 'some payment details')).rejects.toThrow(InsufficientStock); | |
expect(session.findInventoryByProuctSkus).toHaveBeenNthCalledWith(1, ['some product sku']); | |
expect(session.updateInventoryStock).not.toHaveBeenCalled(); | |
expect(session.createOrder).not.toHaveBeenCalled(); | |
}); | |
test('creates order when all desired items are in stock', async () => { | |
const session = { | |
findInventoryByProuctSkus: jest.fn() | |
.mockResolvedValueOnce([ | |
{ | |
productSku: 'some product sku', | |
stock: 30 | |
} | |
]), | |
updateInventoryStock: jest.fn() | |
.mockResolvedValueOnce(), | |
createOrder: jest.fn() | |
.mockResolvedValueOnce('some order id') | |
}; | |
const repository = { | |
async checkoutTransaction(closure) { | |
return closure(session); | |
} | |
}; | |
const service = Service(repository); | |
const result = await service.checkout([ | |
{ | |
productSku: 'some product sku', | |
quantity: 10 | |
} | |
], 'some shipping address', 'some payment details'); | |
expect(result).toBe('some order id'); | |
expect(session.findInventoryByProuctSkus).toHaveBeenNthCalledWith(1, ['some product sku']); | |
expect(session.updateInventoryStock).toHaveBeenNthCalledWith(1, [ | |
{ | |
productSku: 'some product sku', | |
stock: 20 | |
} | |
]); | |
expect(session.createOrder).toHaveBeenNthCalledWith(1, { | |
lineItems: [ | |
{ | |
productSku: 'some product sku', | |
quantity: 10 | |
}, | |
], | |
shippingAddress: 'some shipping address', | |
paymentDetails: 'some payment details' | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment