Skip to content

Commit 236f394

Browse files
committed
Add more relevant tests
1 parent dae1ea1 commit 236f394

File tree

2 files changed

+115
-8
lines changed

2 files changed

+115
-8
lines changed

server/__tests__/content-types.test.js

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('Tests content types', () => {
9898
expect(count).toEqual(2)
9999
})
100100

101-
test('Test fetching entries of content type', async () => {
101+
test('Test fetching entries of a content type with default parameters', async () => {
102102
const contentTypeServices = createContentTypeService({
103103
strapi: fakeStrapi,
104104
})
@@ -107,19 +107,128 @@ describe('Tests content types', () => {
107107
contentType: 'api::restaurant.restaurant',
108108
})
109109

110+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledWith(
111+
'api::restaurant.restaurant',
112+
{
113+
fields: '*',
114+
start: 0,
115+
limit: 500,
116+
filters: {},
117+
sort: {},
118+
populate: '*',
119+
publicationState: 'live',
120+
}
121+
)
122+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(1)
110123
expect(count).toEqual([{ id: 1 }])
111124
})
112125

113-
test('Test fetching entries on non existing content type', async () => {
126+
test('Test fetching entries of a content type with custom parameters', async () => {
114127
const contentTypeServices = createContentTypeService({
115128
strapi: fakeStrapi,
116129
})
117130

118131
const count = await contentTypeServices.getEntries({
132+
contentType: 'api::restaurant.restaurant',
133+
fields: 'title',
134+
start: 1,
135+
limit: 2,
136+
filters: { where: { title: 'hello' } },
137+
sort: 'id',
138+
populate: {},
139+
publicationState: 'preview',
140+
})
141+
142+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledWith(
143+
'api::restaurant.restaurant',
144+
{
145+
fields: 'title',
146+
start: 1,
147+
limit: 2,
148+
filters: { where: { title: 'hello' } },
149+
sort: 'id',
150+
populate: {},
151+
publicationState: 'preview',
152+
}
153+
)
154+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(1)
155+
expect(count).toEqual([{ id: 1 }])
156+
})
157+
158+
test('Test fetching entries on non existing content type', async () => {
159+
const contentTypeServices = createContentTypeService({
160+
strapi: fakeStrapi,
161+
})
162+
163+
const entry = await contentTypeServices.getEntries({
164+
contentType: 'api::test.test',
165+
})
166+
167+
expect(fakeStrapi.entityService.findMany).toHaveBeenCalledTimes(0)
168+
expect(entry).toEqual([])
169+
})
170+
171+
test('Test fetching an entry of a content type with default parameters', async () => {
172+
const contentTypeServices = createContentTypeService({
173+
strapi: fakeStrapi,
174+
})
175+
176+
const entry = await contentTypeServices.getEntry({
177+
contentType: 'api::restaurant.restaurant',
178+
id: 200,
179+
})
180+
181+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledWith(
182+
'api::restaurant.restaurant',
183+
200,
184+
{
185+
fields: '*',
186+
populate: '*',
187+
}
188+
)
189+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(1)
190+
expect(entry).toEqual([{ id: 1 }])
191+
})
192+
193+
test('Test fetching an entry of a content type with custom parameters', async () => {
194+
const contentTypeServices = createContentTypeService({
195+
strapi: fakeStrapi,
196+
})
197+
198+
const entry = await contentTypeServices.getEntry({
199+
contentType: 'api::restaurant.restaurant',
200+
id: 200,
201+
fields: ['title'],
202+
populate: {
203+
subClass: true,
204+
},
205+
})
206+
207+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledWith(
208+
'api::restaurant.restaurant',
209+
200,
210+
{
211+
fields: ['title'],
212+
populate: {
213+
subClass: true,
214+
},
215+
}
216+
)
217+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(1)
218+
expect(entry).toEqual([{ id: 1 }])
219+
})
220+
221+
test('Test fetching an entry on a non existing content type', async () => {
222+
const contentTypeServices = createContentTypeService({
223+
strapi: fakeStrapi,
224+
})
225+
226+
const count = await contentTypeServices.getEntry({
119227
contentType: 'api::test.test',
120228
})
121229

122-
expect(count).toEqual([])
230+
expect(fakeStrapi.entityService.findOne).toHaveBeenCalledTimes(0)
231+
expect(count).toEqual({})
123232
})
124233

125234
test('Test operation in batches on entries', async () => {
@@ -137,8 +246,6 @@ describe('Tests content types', () => {
137246
})),
138247
})
139248

140-
console.log(entries)
141-
142249
expect(entries[0].id).toEqual(2)
143250
expect(entries[0].contentType).toEqual(contentType)
144251
})

server/services/content-types/content-types.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ module.exports = ({ strapi }) => ({
125125
* More information: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/crud.html#findone
126126
*
127127
* @param {object} options
128-
* @param {string} [options.id] - Id of the entry.
128+
* @param {string | number} [options.id] - Id of the entry.
129129
* @param {string | string[]} [options.fields] - Fields present in the returned entry.
130130
* @param {object} [options.populate] - Relations, components and dynamic zones to populate.
131131
* @param {string} [options.contentType] - Content type.
@@ -172,7 +172,7 @@ module.exports = ({ strapi }) => ({
172172
filters = {},
173173
sort = {},
174174
populate = '*',
175-
publicationState,
175+
publicationState = 'live',
176176
}) {
177177
const contentTypeUid = this.getContentTypeUid({ contentType })
178178
if (contentTypeUid === undefined) return []
@@ -184,7 +184,7 @@ module.exports = ({ strapi }) => ({
184184
filters,
185185
sort,
186186
populate,
187-
publicationState: publicationState || 'live',
187+
publicationState: publicationState,
188188
})
189189
// Safe guard in case the content-type is a single type.
190190
// In which case it is wrapped in an array for consistency.

0 commit comments

Comments
 (0)