mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 09:56:44 +00:00 
			
		
		
		
	test upload image
This commit is contained in:
		
							parent
							
								
									f0b67b9ec5
								
							
						
					
					
						commit
						09fdd984c1
					
				| @ -1,6 +1,6 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| const uploadService = require('../upload')({}); | ||||
| const uploadService = require('../../upload')({}); | ||||
| 
 | ||||
| describe('Upload service', () => { | ||||
|   beforeAll(() => { | ||||
							
								
								
									
										
											BIN
										
									
								
								packages/core/upload/server/services/__tests__/upload/image.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								packages/core/upload/server/services/__tests__/upload/image.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 6.5 KiB | 
| @ -0,0 +1,88 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| const path = require('path'); | ||||
| const fs = require('fs'); | ||||
| const fse = require('fs-extra'); | ||||
| const _ = require('lodash'); | ||||
| const uploadService = require('../../upload')({}); | ||||
| 
 | ||||
| const testFilePath = path.join(__dirname, './image.png'); | ||||
| const tmpWorkingDirectory = path.join(__dirname, './tmp'); | ||||
| 
 | ||||
| function mockUploadProvider(uploadFunc, props) { | ||||
|   const { responsiveDimensions = false } = props || {}; | ||||
| 
 | ||||
|   const default_config = { | ||||
|     plugin: { | ||||
|       upload: { | ||||
|         breakpoints: { | ||||
|           large: 1000, | ||||
|           medium: 750, | ||||
|         }, | ||||
|       }, | ||||
|     }, | ||||
|   }; | ||||
| 
 | ||||
|   global.strapi = { | ||||
|     config: { | ||||
|       get: (path, defaultValue) => _.get(default_config, path, defaultValue), | ||||
|     }, | ||||
|     plugins: { | ||||
|       upload: { | ||||
|         services: { | ||||
|           provider: { | ||||
|             upload: uploadFunc, | ||||
|           }, | ||||
|           upload: { | ||||
|             getSettings: () => ({ responsiveDimensions }), | ||||
|           }, | ||||
|           'image-manipulation': require('../../image-manipulation')(), | ||||
|         }, | ||||
|       }, | ||||
|     }, | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| const getFileData = () => ({ | ||||
|   alternativeText: 'image.png', | ||||
|   caption: 'image.png', | ||||
|   ext: '.png', | ||||
|   folder: null, | ||||
|   folderPath: '/', | ||||
|   getStream: () => fs.createReadStream(testFilePath), | ||||
|   hash: 'image_d9b4f84424', | ||||
|   height: 1000, | ||||
|   size: 4, | ||||
|   width: 1500, | ||||
|   tmpWorkingDirectory, | ||||
| }); | ||||
| 
 | ||||
| describe('Upload image', () => { | ||||
|   beforeAll(async () => { | ||||
|     // Create tmp directory if it does not exist
 | ||||
|     await fse.mkdir(tmpWorkingDirectory); | ||||
|   }); | ||||
| 
 | ||||
|   afterAll(async () => { | ||||
|     // Remove tmp directory
 | ||||
|     await fse.remove(tmpWorkingDirectory); | ||||
|   }); | ||||
|   test('Upload with thubmnail', async () => { | ||||
|     let fileData = getFileData(); | ||||
|     const upload = jest.fn(); | ||||
|     mockUploadProvider(upload); | ||||
| 
 | ||||
|     await uploadService.uploadImage(fileData); | ||||
|     expect(upload).toHaveBeenCalledTimes(2); | ||||
|   }); | ||||
| 
 | ||||
|   test('Upload with responsive formats', async () => { | ||||
|     let fileData = getFileData(); | ||||
|     const upload = jest.fn(); | ||||
|     mockUploadProvider(upload, { responsiveDimensions: true }); | ||||
| 
 | ||||
|     await uploadService.uploadImage(fileData); | ||||
|     // 1 for the original image, 1 for thubnhail, 2 for the responsive formats
 | ||||
|     expect(upload).toHaveBeenCalledTimes(4); | ||||
|   }); | ||||
| }); | ||||
| @ -176,6 +176,8 @@ module.exports = ({ strapi }) => ({ | ||||
|     // Store width and height of the original image
 | ||||
|     const { width, height } = await getDimensions(fileData); | ||||
| 
 | ||||
|     // Make sure this is assigned before calling upload
 | ||||
|     // That way it can mutate the width and height
 | ||||
|     _.assign(fileData, { | ||||
|       width, | ||||
|       height, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Marc-Roig
						Marc-Roig