| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | let rq; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const ct = { | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'withdate', | 
					
						
							|  |  |  |   singularName: 'withdate', | 
					
						
							|  |  |  |   pluralName: 'withdates', | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   attributes: { | 
					
						
							|  |  |  |     field: { | 
					
						
							|  |  |  |       type: 'date', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  | describe('Test type date', () => { | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await builder.addContentType(ct).build(); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('Create entry with valid value JSON', async () => { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |     const res = await rq.post('/content-manager/collection-types/api::withdate.withdate', { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       body: { | 
					
						
							|  |  |  |         field: '2019-08-08', | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(res.body).toMatchObject({ | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |       field: '2019-08-08', | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |   test.each([ | 
					
						
							|  |  |  |     '2019-08-08', | 
					
						
							|  |  |  |     '2019-08-08 12:11:12', | 
					
						
							|  |  |  |     '2019-08-08T00:00:00', | 
					
						
							|  |  |  |     '2019-08-08T00:00:00Z', | 
					
						
							|  |  |  |     '2019-08-08 00:00:00.123', | 
					
						
							|  |  |  |     '2019-08-08 00:00:00.123Z', | 
					
						
							|  |  |  |     '2019-08-08T00:00:00.123', | 
					
						
							|  |  |  |     '2019-08-08T00:00:00.123Z', | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |   ])('Date can be sent in any iso format and the date part will be kept, (%s)', async (input) => { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |     const res = await rq.post('/content-manager/collection-types/api::withdate.withdate', { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       body: { | 
					
						
							|  |  |  |         field: input, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |       field: '2019-08-08', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test.each([1234567891012, '1234567891012', '2019/12/11', '12:11:11'])( | 
					
						
							|  |  |  |     'Throws on invalid date (%s)', | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     async (value) => { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       const res = await rq.post('/content-manager/collection-types/api::withdate.withdate', { | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           field: value, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('Reading entry, returns correct value', async () => { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |     const res = await rq.get('/content-manager/collection-types/api::withdate.withdate'); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  |     expect(res.body.pagination).toBeDefined(); | 
					
						
							|  |  |  |     expect(Array.isArray(res.body.results)).toBe(true); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     res.body.results.forEach((entry) => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       expect(entry.field).toMatch(/^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Updating entry sets the right value and format JSON', async () => { | 
					
						
							| 
									
										
										
										
											2022-06-08 11:09:53 +02:00
										 |  |  |     const now = '2018-08-05'; | 
					
						
							| 
									
										
										
										
											2022-04-12 16:32:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |     const res = await rq.post('/content-manager/collection-types/api::withdate.withdate', { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       body: { | 
					
						
							| 
									
										
										
										
											2022-04-12 16:32:05 +02:00
										 |  |  |         field: now, | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 11:09:53 +02:00
										 |  |  |     const newDate = '2017-11-23'; | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |     const updateRes = await rq.put( | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       `/content-manager/collection-types/api::withdate.withdate/${res.body.id}`, | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |       { | 
					
						
							|  |  |  |         body: { | 
					
						
							| 
									
										
										
										
											2022-04-12 16:32:05 +02:00
										 |  |  |           field: newDate, | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(updateRes.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(updateRes.body).toMatchObject({ | 
					
						
							|  |  |  |       id: res.body.id, | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |       field: '2017-11-23', | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |