Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/early-geese-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Add support for get my business unit by key
88 changes: 87 additions & 1 deletion src/services/my-business-unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { BusinessUnitDraft } from "@commercetools/platform-sdk";
import supertest from "supertest";
import { describe, expect, test } from "vitest";
import { afterEach, describe, expect, test } from "vitest";
import { CommercetoolsMock } from "../index";

const ctMock = new CommercetoolsMock();

describe("MyBusinessUnit", () => {
afterEach(() => {
ctMock.clear();
});

test("Get my business units", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
Expand Down Expand Up @@ -51,6 +55,28 @@ describe("MyBusinessUnit", () => {
expect(response.body).toEqual(createResponse.body);
});

test("Get my business unit by key", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
key: "my-business-unit",
unitType: "Company",
name: "My Business Unit",
contactEmail: "contact@example.com",
};
const createResponse = await supertest(ctMock.app)
.post("/dummy/business-units")
.send(draft);

expect(createResponse.status).toBe(201);

const response = await supertest(ctMock.app).get(
`/dummy/me/business-units/key=${createResponse.body.key}`,
);

expect(response.status).toBe(200);
expect(response.body).toEqual(createResponse.body);
});

test("Delete my business unit", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
Expand Down Expand Up @@ -80,6 +106,35 @@ describe("MyBusinessUnit", () => {
expect(newResponse.status).toBe(404);
});

test("Delete my business unit by key", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
key: "my-business-unit",
unitType: "Company",
name: "My Business Unit",
contactEmail: "contact@example.com",
};
const createResponse = await supertest(ctMock.app)
.post("/dummy/business-units")
.send(draft);

expect(createResponse.status).toBe(201);

// Now delete the business unit
const deleteResponse = await supertest(ctMock.app).delete(
`/dummy/me/business-units/key=${createResponse.body.key}`,
);

expect(deleteResponse.status).toBe(200);
expect(deleteResponse.body).toEqual(createResponse.body);

// Verify that the business unit is deleted
const newResponse = await supertest(ctMock.app).get(
`/dummy/me/business-units/key=${createResponse.body.key}`,
);
expect(newResponse.status).toBe(404);
});

test("Update my business unit", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
Expand Down Expand Up @@ -110,4 +165,35 @@ describe("MyBusinessUnit", () => {
expect(updateResponse.status).toBe(200);
expect(updateResponse.body.name).toBe("Updated Business Unit Name");
});

test("Update my business unit by key", async () => {
// First create a business unit
const draft: BusinessUnitDraft = {
key: "my-business-unit",
unitType: "Company",
name: "My Business Unit",
contactEmail: "contact@example.com",
};
const createResponse = await supertest(ctMock.app)
.post("/dummy/business-units")
.send(draft);

expect(createResponse.status).toBe(201);

const updateResponse = await supertest(ctMock.app)
.post(`/dummy/me/business-units/key=${createResponse.body.key}`)
.send({
id: createResponse.body.id,
version: createResponse.body.version,
actions: [
{
action: "changeName",
name: "Updated Business Unit Name",
},
],
});

expect(updateResponse.status).toBe(200);
expect(updateResponse.body.name).toBe("Updated Business Unit Name");
});
});
3 changes: 3 additions & 0 deletions src/services/my-business-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ export class MyBusinessUnitService extends AbstractService {
this.extraRoutes(router);

router.get("/business-units/", this.get.bind(this));
router.get("/business-units/key=:key", this.getWithKey.bind(this));
Comment thread
jsm1t marked this conversation as resolved.
router.get("/business-units/:id", this.getWithId.bind(this));

router.delete("/business-units/key=:key", this.deleteWithKey.bind(this));
Comment thread
jsm1t marked this conversation as resolved.
router.delete("/business-units/:id", this.deleteWithId.bind(this));

router.post("/business-units/", this.post.bind(this));
router.post("/business-units/key=:key", this.postWithKey.bind(this));
Comment thread
jsm1t marked this conversation as resolved.
router.post("/business-units/:id", this.postWithId.bind(this));

parent.use(`/${basePath}`, router);
Expand Down
Loading