Skip to content

Commit 71ecce0

Browse files
authored
feat: add support for my business unit by key (#332)
1 parent 7948f69 commit 71ecce0

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

.changeset/early-geese-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": minor
3+
---
4+
5+
Add support for get my business unit by key

src/services/my-business-unit.test.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import type { BusinessUnitDraft } from "@commercetools/platform-sdk";
22
import supertest from "supertest";
3-
import { describe, expect, test } from "vitest";
3+
import { afterEach, describe, expect, test } from "vitest";
44
import { CommercetoolsMock } from "../index";
55

66
const ctMock = new CommercetoolsMock();
77

88
describe("MyBusinessUnit", () => {
9+
afterEach(() => {
10+
ctMock.clear();
11+
});
12+
913
test("Get my business units", async () => {
1014
// First create a business unit
1115
const draft: BusinessUnitDraft = {
@@ -51,6 +55,28 @@ describe("MyBusinessUnit", () => {
5155
expect(response.body).toEqual(createResponse.body);
5256
});
5357

58+
test("Get my business unit by key", async () => {
59+
// First create a business unit
60+
const draft: BusinessUnitDraft = {
61+
key: "my-business-unit",
62+
unitType: "Company",
63+
name: "My Business Unit",
64+
contactEmail: "[email protected]",
65+
};
66+
const createResponse = await supertest(ctMock.app)
67+
.post("/dummy/business-units")
68+
.send(draft);
69+
70+
expect(createResponse.status).toBe(201);
71+
72+
const response = await supertest(ctMock.app).get(
73+
`/dummy/me/business-units/key=${createResponse.body.key}`,
74+
);
75+
76+
expect(response.status).toBe(200);
77+
expect(response.body).toEqual(createResponse.body);
78+
});
79+
5480
test("Delete my business unit", async () => {
5581
// First create a business unit
5682
const draft: BusinessUnitDraft = {
@@ -80,6 +106,35 @@ describe("MyBusinessUnit", () => {
80106
expect(newResponse.status).toBe(404);
81107
});
82108

109+
test("Delete my business unit by key", async () => {
110+
// First create a business unit
111+
const draft: BusinessUnitDraft = {
112+
key: "my-business-unit",
113+
unitType: "Company",
114+
name: "My Business Unit",
115+
contactEmail: "[email protected]",
116+
};
117+
const createResponse = await supertest(ctMock.app)
118+
.post("/dummy/business-units")
119+
.send(draft);
120+
121+
expect(createResponse.status).toBe(201);
122+
123+
// Now delete the business unit
124+
const deleteResponse = await supertest(ctMock.app).delete(
125+
`/dummy/me/business-units/key=${createResponse.body.key}`,
126+
);
127+
128+
expect(deleteResponse.status).toBe(200);
129+
expect(deleteResponse.body).toEqual(createResponse.body);
130+
131+
// Verify that the business unit is deleted
132+
const newResponse = await supertest(ctMock.app).get(
133+
`/dummy/me/business-units/key=${createResponse.body.key}`,
134+
);
135+
expect(newResponse.status).toBe(404);
136+
});
137+
83138
test("Update my business unit", async () => {
84139
// First create a business unit
85140
const draft: BusinessUnitDraft = {
@@ -110,4 +165,35 @@ describe("MyBusinessUnit", () => {
110165
expect(updateResponse.status).toBe(200);
111166
expect(updateResponse.body.name).toBe("Updated Business Unit Name");
112167
});
168+
169+
test("Update my business unit by key", async () => {
170+
// First create a business unit
171+
const draft: BusinessUnitDraft = {
172+
key: "my-business-unit",
173+
unitType: "Company",
174+
name: "My Business Unit",
175+
contactEmail: "[email protected]",
176+
};
177+
const createResponse = await supertest(ctMock.app)
178+
.post("/dummy/business-units")
179+
.send(draft);
180+
181+
expect(createResponse.status).toBe(201);
182+
183+
const updateResponse = await supertest(ctMock.app)
184+
.post(`/dummy/me/business-units/key=${createResponse.body.key}`)
185+
.send({
186+
id: createResponse.body.id,
187+
version: createResponse.body.version,
188+
actions: [
189+
{
190+
action: "changeName",
191+
name: "Updated Business Unit Name",
192+
},
193+
],
194+
});
195+
196+
expect(updateResponse.status).toBe(200);
197+
expect(updateResponse.body.name).toBe("Updated Business Unit Name");
198+
});
113199
});

src/services/my-business-unit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ export class MyBusinessUnitService extends AbstractService {
2222
this.extraRoutes(router);
2323

2424
router.get("/business-units/", this.get.bind(this));
25+
router.get("/business-units/key=:key", this.getWithKey.bind(this));
2526
router.get("/business-units/:id", this.getWithId.bind(this));
2627

28+
router.delete("/business-units/key=:key", this.deleteWithKey.bind(this));
2729
router.delete("/business-units/:id", this.deleteWithId.bind(this));
2830

2931
router.post("/business-units/", this.post.bind(this));
32+
router.post("/business-units/key=:key", this.postWithKey.bind(this));
3033
router.post("/business-units/:id", this.postWithId.bind(this));
3134

3235
parent.use(`/${basePath}`, router);

0 commit comments

Comments
 (0)