From a15f2b37b3d361089102519d4c09b89d45509774 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 17 May 2024 09:33:34 +0530 Subject: [PATCH] test: add tests --- .../product-collections.spec.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts b/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts index 8fba59fbd6..557ace762c 100644 --- a/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts @@ -365,6 +365,73 @@ moduleIntegrationTestRunner({ "ProductCollection with id: does-not-exist was not found" ) }) + + it("should dissociate existing products when new products are synced", async () => { + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productOne.id, productTwo.id], + }, + ]) + + /** + * Another upsert should remove the first productOne + */ + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productTwo.id], + }, + ]) + + const productCollection = await service.retrieveCollection( + collectionId, + { + select: ["products.id"], + relations: ["products"], + } + ) + + expect(productCollection.products).toHaveLength(1) + expect(productCollection).toEqual( + expect.objectContaining({ + products: expect.arrayContaining([ + expect.objectContaining({ + id: productTwo.id, + }), + ]), + }) + ) + }) + + it("should dissociate all existing products", async () => { + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productOne.id, productTwo.id], + }, + ]) + + /** + * Another upsert should remove the first productOne + */ + await service.upsertCollections([ + { + id: collectionId, + product_ids: [], + }, + ]) + + const productCollection = await service.retrieveCollection( + collectionId, + { + select: ["products.id"], + relations: ["products"], + } + ) + + expect(productCollection.products).toHaveLength(0) + }) }) describe("createCollections", () => {