Improve unit test coverage for src/display/display_utils.js
				
					
				
			The `DOMCanvasFactory` class is now fully covered. Moreover, missing cases for the `getFilenameFromUrl` function have been included. Finally, `var` usage has been removed.
This commit is contained in:
		
							parent
							
								
									87a70f3359
								
							
						
					
					
						commit
						b244622f7e
					
				| @ -12,13 +12,111 @@ | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| /* eslint no-var: error */ | ||||
| 
 | ||||
| import { | ||||
|   DOMSVGFactory, getFilenameFromUrl, isValidFetchUrl | ||||
|   DOMCanvasFactory, DOMSVGFactory, getFilenameFromUrl, isValidFetchUrl | ||||
| } from '../../src/display/display_utils'; | ||||
| import isNodeJS from '../../src/shared/is_node'; | ||||
| 
 | ||||
| describe('display_utils', function() { | ||||
|   describe('DOMCanvasFactory', function() { | ||||
|     let canvasFactory; | ||||
| 
 | ||||
|     beforeAll(function(done) { | ||||
|       canvasFactory = new DOMCanvasFactory(); | ||||
|       done(); | ||||
|     }); | ||||
| 
 | ||||
|     afterAll(function() { | ||||
|       canvasFactory = null; | ||||
|     }); | ||||
| 
 | ||||
|     it('`create` should throw an error if the dimensions are invalid', | ||||
|         function() { | ||||
|       // Invalid width.
 | ||||
|       expect(function() { | ||||
|         return canvasFactory.create(-1, 1); | ||||
|       }).toThrow(new Error('Invalid canvas size')); | ||||
| 
 | ||||
|       // Invalid height.
 | ||||
|       expect(function() { | ||||
|         return canvasFactory.create(1, -1); | ||||
|       }).toThrow(new Error('Invalid canvas size')); | ||||
|     }); | ||||
| 
 | ||||
|     it('`create` should return a canvas if the dimensions are valid', | ||||
|         function() { | ||||
|       if (isNodeJS()) { | ||||
|         pending('Document is not supported in Node.js.'); | ||||
|       } | ||||
| 
 | ||||
|       const { canvas, context, } = canvasFactory.create(20, 40); | ||||
|       expect(canvas instanceof HTMLCanvasElement).toBe(true); | ||||
|       expect(context instanceof CanvasRenderingContext2D).toBe(true); | ||||
|       expect(canvas.width).toBe(20); | ||||
|       expect(canvas.height).toBe(40); | ||||
|     }); | ||||
| 
 | ||||
|     it('`reset` should throw an error if no canvas is provided', function() { | ||||
|       const canvasAndContext = { canvas: null, context: null, }; | ||||
| 
 | ||||
|       expect(function() { | ||||
|         return canvasFactory.reset(canvasAndContext, 20, 40); | ||||
|       }).toThrow(new Error('Canvas is not specified')); | ||||
|     }); | ||||
| 
 | ||||
|     it('`reset` should throw an error if the dimensions are invalid', | ||||
|         function() { | ||||
|       const canvasAndContext = { canvas: 'foo', context: 'bar', }; | ||||
| 
 | ||||
|       // Invalid width.
 | ||||
|       expect(function() { | ||||
|         return canvasFactory.reset(canvasAndContext, -1, 1); | ||||
|       }).toThrow(new Error('Invalid canvas size')); | ||||
| 
 | ||||
|       // Invalid height.
 | ||||
|       expect(function() { | ||||
|         return canvasFactory.reset(canvasAndContext, 1, -1); | ||||
|       }).toThrow(new Error('Invalid canvas size')); | ||||
|     }); | ||||
| 
 | ||||
|     it('`reset` should alter the canvas/context if the dimensions are valid', | ||||
|         function() { | ||||
|       if (isNodeJS()) { | ||||
|         pending('Document is not supported in Node.js.'); | ||||
|       } | ||||
| 
 | ||||
|       const canvasAndContext = canvasFactory.create(20, 40); | ||||
|       canvasFactory.reset(canvasAndContext, 60, 80); | ||||
| 
 | ||||
|       const { canvas, context, } = canvasAndContext; | ||||
|       expect(canvas instanceof HTMLCanvasElement).toBe(true); | ||||
|       expect(context instanceof CanvasRenderingContext2D).toBe(true); | ||||
|       expect(canvas.width).toBe(60); | ||||
|       expect(canvas.height).toBe(80); | ||||
|     }); | ||||
| 
 | ||||
|     it('`destroy` should throw an error if no canvas is provided', function() { | ||||
|       expect(function() { | ||||
|         return canvasFactory.destroy({}); | ||||
|       }).toThrow(new Error('Canvas is not specified')); | ||||
|     }); | ||||
| 
 | ||||
|     it('`destroy` should clear the canvas/context', function() { | ||||
|       if (isNodeJS()) { | ||||
|         pending('Document is not supported in Node.js.'); | ||||
|       } | ||||
| 
 | ||||
|       const canvasAndContext = canvasFactory.create(20, 40); | ||||
|       canvasFactory.destroy(canvasAndContext); | ||||
| 
 | ||||
|       const { canvas, context, } = canvasAndContext; | ||||
|       expect(canvas).toBe(null); | ||||
|       expect(context).toBe(null); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('DOMSVGFactory', function() { | ||||
|     let svgFactory; | ||||
| 
 | ||||
| @ -50,8 +148,7 @@ describe('display_utils', function() { | ||||
|         pending('Document is not supported in Node.js.'); | ||||
|       } | ||||
| 
 | ||||
|       let svg = svgFactory.create(20, 40); | ||||
| 
 | ||||
|       const svg = svgFactory.create(20, 40); | ||||
|       expect(svg instanceof SVGSVGElement).toBe(true); | ||||
|       expect(svg.getAttribute('version')).toBe('1.1'); | ||||
|       expect(svg.getAttribute('width')).toBe('20px'); | ||||
| @ -73,25 +170,30 @@ describe('display_utils', function() { | ||||
|         pending('Document is not supported in Node.js.'); | ||||
|       } | ||||
| 
 | ||||
|       let svg = svgFactory.createElement('svg:rect'); | ||||
| 
 | ||||
|       const svg = svgFactory.createElement('svg:rect'); | ||||
|       expect(svg instanceof SVGRectElement).toBe(true); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('getFilenameFromUrl', function() { | ||||
|     it('should get the filename from an absolute URL', function() { | ||||
|       var url = 'http://server.org/filename.pdf'; | ||||
|       var result = getFilenameFromUrl(url); | ||||
|       var expected = 'filename.pdf'; | ||||
|       expect(result).toEqual(expected); | ||||
|       const url = 'https://server.org/filename.pdf'; | ||||
|       expect(getFilenameFromUrl(url)).toEqual('filename.pdf'); | ||||
|     }); | ||||
| 
 | ||||
|     it('should get the filename from a relative URL', function() { | ||||
|       var url = '../../filename.pdf'; | ||||
|       var result = getFilenameFromUrl(url); | ||||
|       var expected = 'filename.pdf'; | ||||
|       expect(result).toEqual(expected); | ||||
|       const url = '../../filename.pdf'; | ||||
|       expect(getFilenameFromUrl(url)).toEqual('filename.pdf'); | ||||
|     }); | ||||
| 
 | ||||
|     it('should get the filename from a URL with an anchor', function() { | ||||
|       const url = 'https://server.org/filename.pdf#foo'; | ||||
|       expect(getFilenameFromUrl(url)).toEqual('filename.pdf'); | ||||
|     }); | ||||
| 
 | ||||
|     it('should get the filename from a URL with query parameters', function() { | ||||
|       const url = 'https://server.org/filename.pdf?foo=bar'; | ||||
|       expect(getFilenameFromUrl(url)).toEqual('filename.pdf'); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user