fix: adds tests
This commit is contained in:
+172
@@ -0,0 +1,172 @@
|
||||
import { MockManager, MockRepository } from "medusa-test-utils"
|
||||
import RestockNotificationService from "../restock-notification"
|
||||
|
||||
describe("RestockNotificationService", () => {
|
||||
const RestockNotificationModel = MockRepository({
|
||||
findOne: (q) => {
|
||||
if (q.where.variant_id === "variant_1234") {
|
||||
return Promise.resolve({
|
||||
variant_id: "variant_1234",
|
||||
emails: ["test@tesmail.com"],
|
||||
})
|
||||
}
|
||||
if (q.where.variant_id === "variant_outofstock") {
|
||||
return Promise.resolve({
|
||||
variant_id: "variant_outofstock",
|
||||
emails: ["test@tesmail.com"],
|
||||
})
|
||||
}
|
||||
return Promise.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
const ProductVariantService = {
|
||||
retrieve: (id) => {
|
||||
if (id === "variant_instock") {
|
||||
return {
|
||||
id,
|
||||
inventory_quantity: 10,
|
||||
}
|
||||
}
|
||||
if (id === "variant_1234") {
|
||||
return {
|
||||
id,
|
||||
inventory_quantity: 10,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
inventory_quantity: 0,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const EventBusService = {
|
||||
emit: jest.fn(),
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
}
|
||||
|
||||
describe("retrieve", () => {
|
||||
const restockNotiService = new RestockNotificationService({
|
||||
manager: MockManager,
|
||||
productVariantService: ProductVariantService,
|
||||
restockNotificationModel: RestockNotificationModel,
|
||||
eventBusService: EventBusService,
|
||||
})
|
||||
|
||||
it("successfully retrieves", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const result = await restockNotiService.retrieve("variant_1234")
|
||||
|
||||
expect(result).toEqual({
|
||||
variant_id: "variant_1234",
|
||||
emails: ["test@tesmail.com"],
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully retrieves with empty response", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const result = await restockNotiService.retrieve("variant_non")
|
||||
|
||||
expect(result).toEqual(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addEmail", () => {
|
||||
const restockNotiService = new RestockNotificationService({
|
||||
manager: MockManager,
|
||||
productVariantService: ProductVariantService,
|
||||
restockNotificationModel: RestockNotificationModel,
|
||||
eventBusService: EventBusService,
|
||||
})
|
||||
|
||||
it("successfully adds email to non-existing noti", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const result = await restockNotiService.addEmail(
|
||||
"variant_test",
|
||||
"seb@med-test.com"
|
||||
)
|
||||
|
||||
expect(RestockNotificationModel.create).toHaveBeenCalledTimes(1)
|
||||
expect(RestockNotificationModel.create).toHaveBeenCalledWith({
|
||||
variant_id: "variant_test",
|
||||
emails: ["seb@med-test.com"],
|
||||
})
|
||||
|
||||
expect(RestockNotificationModel.save).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("successfully adds email to existing noti", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const result = await restockNotiService.addEmail(
|
||||
"variant_1234",
|
||||
"seb@med-test.com"
|
||||
)
|
||||
|
||||
expect(RestockNotificationModel.save).toHaveBeenCalledTimes(1)
|
||||
expect(RestockNotificationModel.save).toHaveBeenCalledWith({
|
||||
variant_id: "variant_1234",
|
||||
emails: ["test@tesmail.com", "seb@med-test.com"],
|
||||
})
|
||||
})
|
||||
|
||||
it("fails to add if in stock", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
await expect(
|
||||
restockNotiService.addEmail("variant_instock", "seb@med-test.com")
|
||||
).rejects.toThrow(
|
||||
"You cannot sign up for restock notifications on a product that is not sold out"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("triggerRestock", () => {
|
||||
const restockNotiService = new RestockNotificationService({
|
||||
manager: MockManager,
|
||||
productVariantService: ProductVariantService,
|
||||
restockNotificationModel: RestockNotificationModel,
|
||||
eventBusService: EventBusService,
|
||||
})
|
||||
|
||||
it("non-existing noti does nothing", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
await expect(restockNotiService.triggerRestock("variant_test")).resolves
|
||||
})
|
||||
|
||||
it("existing noti but out of stock does nothing", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
await expect(restockNotiService.triggerRestock("variant_outofstock"))
|
||||
.resolves
|
||||
})
|
||||
|
||||
it("existing noti emits and deletes", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
await restockNotiService.triggerRestock("variant_1234")
|
||||
|
||||
expect(EventBusService.emit).toHaveBeenCalledTimes(1)
|
||||
expect(EventBusService.emit).toHaveBeenCalledWith(
|
||||
"restock-notification.restocked",
|
||||
{
|
||||
variant_id: "variant_1234",
|
||||
emails: ["test@tesmail.com"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(RestockNotificationModel.delete).toHaveBeenCalledTimes(1)
|
||||
expect(RestockNotificationModel.delete).toHaveBeenCalledWith(
|
||||
"variant_1234"
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,16 @@
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
import { RestockNotification } from "../models/restock-notification"
|
||||
|
||||
class RestockNotificationService extends BaseService {
|
||||
constructor({ manager, eventBusService, productVariantService }, options) {
|
||||
constructor(
|
||||
{
|
||||
manager,
|
||||
eventBusService,
|
||||
productVariantService,
|
||||
restockNotificationModel,
|
||||
},
|
||||
options
|
||||
) {
|
||||
super()
|
||||
|
||||
this.manager_ = manager
|
||||
@@ -13,6 +19,8 @@ class RestockNotificationService extends BaseService {
|
||||
|
||||
this.productVariantService_ = productVariantService
|
||||
|
||||
this.restockNotificationModel_ = restockNotificationModel
|
||||
|
||||
this.eventBus_ = eventBusService
|
||||
}
|
||||
|
||||
@@ -34,13 +42,15 @@ class RestockNotificationService extends BaseService {
|
||||
}
|
||||
|
||||
async retrieve(variantId) {
|
||||
const restockRepo = this.manager_.getRepository(RestockNotification)
|
||||
const restockRepo = this.manager_.getRepository(
|
||||
this.restockNotificationModel_
|
||||
)
|
||||
return await restockRepo.findOne({ where: { variant_id: variantId } })
|
||||
}
|
||||
|
||||
async addEmail(variantId, email) {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const restockRepo = manager.getRepository(RestockNotification)
|
||||
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
||||
const existing = await this.retrieve(variantId)
|
||||
|
||||
if (existing) {
|
||||
@@ -73,7 +83,7 @@ class RestockNotificationService extends BaseService {
|
||||
|
||||
async triggerRestock(variantId) {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const restockRepo = manager.getRepository(RestockNotification)
|
||||
const restockRepo = manager.getRepository(this.restockNotificationModel_)
|
||||
|
||||
const existing = await this.retrieve(variantId)
|
||||
if (!existing) {
|
||||
@@ -84,7 +94,7 @@ class RestockNotificationService extends BaseService {
|
||||
if (variant.inventory_quantity > 0) {
|
||||
await this.eventBus_
|
||||
.withTransaction(manager)
|
||||
.emit("restock_notification.restocked", {
|
||||
.emit("restock-notification.restocked", {
|
||||
variant_id: variantId,
|
||||
emails: existing.emails,
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,8 @@
|
||||
export default {
|
||||
getRepository: function (repo) {
|
||||
return repo;
|
||||
},
|
||||
|
||||
getCustomRepository: function (repo) {
|
||||
return repo;
|
||||
},
|
||||
|
||||
@@ -10,10 +10,12 @@ class MockRepo {
|
||||
findOneOrFail,
|
||||
save,
|
||||
findAndCount,
|
||||
del,
|
||||
}) {
|
||||
this.create_ = create;
|
||||
this.update_ = update;
|
||||
this.remove_ = remove;
|
||||
this.delete_ = del;
|
||||
this.softRemove_ = softRemove;
|
||||
this.find_ = find;
|
||||
this.findOne_ = findOne;
|
||||
@@ -93,6 +95,12 @@ class MockRepo {
|
||||
}
|
||||
return {};
|
||||
});
|
||||
delete = jest.fn().mockImplementation((...args) => {
|
||||
if (this.delete_) {
|
||||
return this.delete_(...args);
|
||||
}
|
||||
return {};
|
||||
});
|
||||
}
|
||||
|
||||
export default (methods = {}) => {
|
||||
|
||||
@@ -351,7 +351,7 @@ function registerModels(pluginDetails, container) {
|
||||
if (typeof val === "function" || val instanceof EntitySchema) {
|
||||
const name = formatRegistrationName(fn)
|
||||
container.register({
|
||||
[name]: asClass(val),
|
||||
[name]: asValue(val),
|
||||
})
|
||||
|
||||
container.registerAdd("db_entities", asValue(val))
|
||||
|
||||
Reference in New Issue
Block a user