merge develop

This commit is contained in:
Sebastian Rindom
2021-10-15 20:09:31 +02:00
parent 10c87e8d5a
commit 4fd361fddd
207 changed files with 8699 additions and 4124 deletions
@@ -1,9 +0,0 @@
{
"plugins": ["prettier"],
"extends": ["prettier"],
"rules": {
"prettier/prettier": "error",
"semi": "error",
"no-unused-expressions": "true"
}
}
@@ -1,7 +0,0 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}
@@ -135,6 +135,57 @@ describe("RestockNotificationService", () => {
})
describe("triggerRestock", () => {
afterEach(() => {
jest.useRealTimers()
})
it("trigger delay default to 0", async () => {
const restockNotiService = new RestockNotificationService({
manager: MockManager,
productVariantService: ProductVariantService,
restockNotificationModel: RestockNotificationModel,
eventBusService: EventBusService,
})
restockNotiService.restockExecute_ = jest.fn()
jest.clearAllMocks()
jest.useFakeTimers()
restockNotiService.triggerRestock("variant_test")
jest.runAllTimers()
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 0)
})
it("trigger delay 10", async () => {
const restockNotiService = new RestockNotificationService(
{
manager: MockManager,
productVariantService: ProductVariantService,
restockNotificationModel: RestockNotificationModel,
eventBusService: EventBusService,
},
{ trigger_delay: 10 }
)
restockNotiService.restockExecute_ = jest.fn()
jest.clearAllMocks()
jest.useFakeTimers()
restockNotiService.triggerRestock("variant_test")
jest.runAllTimers()
expect(setTimeout).toHaveBeenCalledTimes(1)
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 10)
})
})
describe("restockExecute_", () => {
const restockNotiService = new RestockNotificationService({
manager: MockManager,
productVariantService: ProductVariantService,
@@ -145,20 +196,20 @@ describe("RestockNotificationService", () => {
it("non-existing noti does nothing", async () => {
jest.clearAllMocks()
await expect(restockNotiService.triggerRestock("variant_test")).resolves
await expect(restockNotiService.restockExecute_("variant_test")).resolves
})
it("existing noti but out of stock does nothing", async () => {
jest.clearAllMocks()
await expect(restockNotiService.triggerRestock("variant_outofstock"))
await expect(restockNotiService.restockExecute_("variant_outofstock"))
.resolves
})
it("existing noti emits and deletes", async () => {
jest.clearAllMocks()
await restockNotiService.triggerRestock("variant_1234")
await restockNotiService.restockExecute_("variant_1234")
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
expect(EventBusService.emit).toHaveBeenCalledWith(
@@ -187,7 +238,7 @@ describe("RestockNotificationService", () => {
{ inventory_required: 5 }
)
await service.triggerRestock("variant_1234")
await service.restockExecute_("variant_1234")
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
expect(EventBusService.emit).toHaveBeenCalledWith(
@@ -214,7 +265,7 @@ describe("RestockNotificationService", () => {
{ inventory_required: 5 }
)
await service.triggerRestock("variant_low_inventory")
await service.restockExecute_("variant_low_inventory")
expect(EventBusService.emit).toHaveBeenCalledTimes(0)
expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(0)
@@ -108,10 +108,15 @@ class RestockNotificationService extends BaseService {
* and emits a restocked event to the event bus. After successful emission the
* restock notification is deleted.
* @param {string} variantId - the variant id to trigger restock for
* @return {Promise<RestockNotification>} The resulting restock notification
* @return The resulting restock notification
*/
async triggerRestock(variantId) {
return this.atomicPhase_(async (manager) => {
triggerRestock(variantId) {
const delay = this.options_?.trigger_delay ?? 0
setTimeout(() => this.restockExecute_(variantId), delay)
}
async restockExecute_(variantId) {
return await this.atomicPhase_(async (manager) => {
const restockRepo = manager.getRepository(this.restockNotificationModel_)
const existing = await this.retrieve(variantId)