Skip to content

Commit e4be69a

Browse files
committed
fix(content-type-handlers.ts): import toCamelCase function from utils/to-camel-case.js to fix duplicate function declaration
feat(content-type-handlers.ts): add support for converting content type names to camel case using the toCamelCase function from utils/to-camel-case.js feat(to-camel-case.ts): create a new utility function toCamelCase that converts a string to camel case test(content-type-handler.test.ts): update test cases to use the toCamelCase function from utils/to-camel-case.js to match the changes in content-type-handlers.ts fix(msw-setup.ts): remove console.log statements and fix formatting feat(msw-setup.ts): add support for creating content types with specific IDs in the create content type handler feat(msw-setup.ts): add support for updating content types with specific IDs in the update content type handler
1 parent 6aa771c commit e4be69a

File tree

4 files changed

+302
-292
lines changed

4 files changed

+302
-292
lines changed

src/handlers/content-type-handlers.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { getContentfulClient } from "../config/client.js"
33
import { ContentTypeProps, CreateContentTypeProps } from "contentful-management"
4+
import { toCamelCase } from "../utils/to-camel-case.js"
45

56
export const contentTypeHandlers = {
67
listContentTypes: async (args: { spaceId: string; environmentId: string }) => {
@@ -50,16 +51,6 @@ export const contentTypeHandlers = {
5051
const spaceId = process.env.SPACE_ID || args.spaceId
5152
const environmentId = process.env.ENVIRONMENT_ID || args.environmentId
5253

53-
const toCamelCase = (str: string): string =>
54-
str
55-
.split(/\s+/)
56-
.map((word: string, index: number) =>
57-
index === 0
58-
? word.toLowerCase()
59-
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(),
60-
)
61-
.join("")
62-
6354
const params = {
6455
contentTypeId: toCamelCase(args.name),
6556
spaceId,

src/utils/to-camel-case.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const toCamelCase = (str: string): string =>
2+
str
3+
.split(/\s+/)
4+
.map((word: string, index: number) =>
5+
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(),
6+
)
7+
.join("")
Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
1-
import { expect } from "vitest";
2-
import { contentTypeHandlers } from "../../src/handlers/content-type-handlers.js";
3-
import { server } from "../msw-setup.js";
1+
import { expect } from "vitest"
2+
import { contentTypeHandlers } from "../../src/handlers/content-type-handlers.js"
3+
import { server } from "../msw-setup.js"
4+
import { toCamelCase } from "../../src/utils/to-camel-case.js"
45

56
describe("Content Type Handlers Integration Tests", () => {
67
// Start MSW Server before tests
7-
beforeAll(() => server.listen());
8-
afterEach(() => server.resetHandlers());
9-
afterAll(() => server.close());
8+
beforeAll(() => server.listen())
9+
afterEach(() => server.resetHandlers())
10+
afterAll(() => server.close())
1011

11-
const testSpaceId = "test-space-id";
12-
const testContentTypeId = "test-content-type-id";
12+
const testSpaceId = "test-space-id"
13+
const testContentTypeId = "test-content-type-id"
1314

1415
describe("listContentTypes", () => {
1516
it("should list all content types", async () => {
1617
const result = await contentTypeHandlers.listContentTypes({
1718
spaceId: testSpaceId,
18-
});
19-
20-
expect(result).to.have.property("content").that.is.an("array");
21-
expect(result.content).to.have.lengthOf(1);
22-
23-
const contentTypes = JSON.parse(result.content[0].text);
24-
expect(contentTypes.items).to.be.an("array");
25-
expect(contentTypes.items[0]).to.have.nested.property(
26-
"sys.id",
27-
"test-content-type-id",
28-
);
29-
expect(contentTypes.items[0]).to.have.property(
30-
"name",
31-
"Test Content Type",
32-
);
33-
});
34-
});
19+
})
20+
21+
expect(result).to.have.property("content").that.is.an("array")
22+
expect(result.content).to.have.lengthOf(1)
23+
24+
const contentTypes = JSON.parse(result.content[0].text)
25+
expect(contentTypes.items).to.be.an("array")
26+
expect(contentTypes.items[0]).to.have.nested.property("sys.id", "test-content-type-id")
27+
expect(contentTypes.items[0]).to.have.property("name", "Test Content Type")
28+
})
29+
})
3530

3631
describe("getContentType", () => {
3732
it("should get details of a specific content type", async () => {
3833
const result = await contentTypeHandlers.getContentType({
3934
spaceId: testSpaceId,
4035
contentTypeId: testContentTypeId,
41-
});
36+
})
4237

43-
expect(result).to.have.property("content");
44-
const contentType = JSON.parse(result.content[0].text);
45-
expect(contentType).to.have.nested.property("sys.id", testContentTypeId);
46-
expect(contentType).to.have.property("name", "Test Content Type");
47-
expect(contentType.fields).to.be.an("array");
48-
});
38+
expect(result).to.have.property("content")
39+
const contentType = JSON.parse(result.content[0].text)
40+
expect(contentType).to.have.nested.property("sys.id", toCamelCase(testContentTypeId))
41+
expect(contentType).to.have.property("name", "Test Content Type")
42+
expect(contentType.fields).to.be.an("array")
43+
})
4944

5045
it("should throw error for invalid content type ID", async () => {
5146
try {
5247
await contentTypeHandlers.getContentType({
5348
spaceId: testSpaceId,
5449
contentTypeId: "invalid-id",
55-
});
56-
expect.fail("Should have thrown an error");
50+
})
51+
expect.fail("Should have thrown an error")
5752
} catch (error) {
58-
expect(error).to.exist;
53+
expect(error).to.exist
5954
}
60-
});
61-
});
55+
})
56+
})
6257

6358
describe("createContentType", () => {
6459
it("should create a new content type", async () => {
@@ -73,21 +68,17 @@ describe("Content Type Handlers Integration Tests", () => {
7368
required: true,
7469
},
7570
],
76-
};
77-
78-
const result =
79-
await contentTypeHandlers.createContentType(contentTypeData);
80-
81-
expect(result).to.have.property("content");
82-
const contentType = JSON.parse(result.content[0].text);
83-
expect(contentType).to.have.nested.property(
84-
"sys.id",
85-
"new-content-type-id",
86-
);
87-
expect(contentType).to.have.property("name", "New Content Type");
88-
expect(contentType.fields).to.be.an("array");
89-
});
90-
});
71+
}
72+
73+
const result = await contentTypeHandlers.createContentType(contentTypeData)
74+
75+
expect(result).to.have.property("content")
76+
const contentType = JSON.parse(result.content[0].text)
77+
expect(contentType).to.have.nested.property("sys.id", "newContentType")
78+
expect(contentType).to.have.property("name", "New Content Type")
79+
expect(contentType.fields).to.be.an("array")
80+
})
81+
})
9182

9283
describe("updateContentType", () => {
9384
it("should update an existing content type", async () => {
@@ -103,62 +94,62 @@ describe("Content Type Handlers Integration Tests", () => {
10394
required: true,
10495
},
10596
],
106-
};
97+
}
10798

108-
const result = await contentTypeHandlers.updateContentType(updateData);
99+
const result = await contentTypeHandlers.updateContentType(updateData)
109100

110-
expect(result).to.have.property("content");
111-
const contentType = JSON.parse(result.content[0].text);
112-
expect(contentType).to.have.nested.property("sys.id", testContentTypeId);
113-
expect(contentType).to.have.property("name", "Updated Content Type");
114-
});
115-
});
101+
expect(result).to.have.property("content")
102+
const contentType = JSON.parse(result.content[0].text)
103+
expect(contentType).to.have.nested.property("sys.id", testContentTypeId)
104+
expect(contentType).to.have.property("name", "Updated Content Type")
105+
})
106+
})
116107

117108
describe("deleteContentType", () => {
118109
it("should delete a content type", async () => {
119110
const result = await contentTypeHandlers.deleteContentType({
120111
spaceId: testSpaceId,
121112
contentTypeId: testContentTypeId,
122-
});
113+
})
123114

124-
expect(result).to.have.property("content");
125-
expect(result.content[0].text).to.include("deleted successfully");
126-
});
115+
expect(result).to.have.property("content")
116+
expect(result.content[0].text).to.include("deleted successfully")
117+
})
127118

128119
it("should throw error when deleting non-existent content type", async () => {
129120
try {
130121
await contentTypeHandlers.deleteContentType({
131122
spaceId: testSpaceId,
132123
contentTypeId: "non-existent-id",
133-
});
134-
expect.fail("Should have thrown an error");
124+
})
125+
expect.fail("Should have thrown an error")
135126
} catch (error) {
136-
expect(error).to.exist;
127+
expect(error).to.exist
137128
}
138-
});
139-
});
129+
})
130+
})
140131

141132
describe("publishContentType", () => {
142133
it("should publish a content type", async () => {
143134
const result = await contentTypeHandlers.publishContentType({
144135
spaceId: testSpaceId,
145136
contentTypeId: testContentTypeId,
146-
});
137+
})
147138

148-
expect(result).to.have.property("content");
149-
expect(result.content[0].text).to.include("published successfully");
150-
});
139+
expect(result).to.have.property("content")
140+
expect(result.content[0].text).to.include("published successfully")
141+
})
151142

152143
it("should throw error when publishing non-existent content type", async () => {
153144
try {
154145
await contentTypeHandlers.publishContentType({
155146
spaceId: testSpaceId,
156147
contentTypeId: "non-existent-id",
157-
});
158-
expect.fail("Should have thrown an error");
148+
})
149+
expect.fail("Should have thrown an error")
159150
} catch (error) {
160-
expect(error).to.exist;
151+
expect(error).to.exist
161152
}
162-
});
163-
});
164-
});
153+
})
154+
})
155+
})

0 commit comments

Comments
 (0)