var createTestPackage = require('../../helpers/test-package'); var Dgeni = require('dgeni'); describe('addImageDimensions post-processor', () => { let processor, getImageDimensionsSpy, addImageDimensions, log; beforeEach(() => { const testPackage = createTestPackage('angular-base-package') .factory('getImageDimensions', mockGetImageDimensions); const dgeni = new Dgeni([testPackage]); const injector = dgeni.configureInjector(); log = injector.get('log'); addImageDimensions = injector.get('addImageDimensions'); addImageDimensions.basePath = 'base/path'; getImageDimensionsSpy = injector.get('getImageDimensions'); processor = injector.get('postProcessHtml'); processor.docTypes = ['a']; processor.plugins = [addImageDimensions]; }); it('should add the image dimensions into tags', () => { const docs = [{ docType: 'a', renderedContent: `
xxx
yyy
zzz
` }]; processor.$process(docs); expect(getImageDimensionsSpy).toHaveBeenCalledWith('base/path', 'a/b.jpg'); expect(getImageDimensionsSpy).toHaveBeenCalledWith('base/path', 'c/d.png'); expect(docs).toEqual([jasmine.objectContaining({ docType: 'a', renderedContent: `xxx
yyy
zzz
` })]); }); it('should log a warning for images with no src attribute', () => { const docs = [{ docType: 'a', renderedContent: '' }]; processor.$process(docs); expect(getImageDimensionsSpy).not.toHaveBeenCalled(); expect(docs).toEqual([jasmine.objectContaining({ docType: 'a', renderedContent: '' })]); expect(log.warn).toHaveBeenCalledWith('Missing src in image tag `` - doc (a) '); }); it('should fail for images whose source cannot be loaded', () => { getImageDimensionsSpy.and.callFake(() => { const error = new Error('no such file or directory'); error.code = 'ENOENT'; throw error; }); const docs = [{ docType: 'a', renderedContent: '' }]; expect(() => processor.$process(docs)).toThrowError('Unable to load src in image tag `` - doc (a) '); expect(getImageDimensionsSpy).toHaveBeenCalledWith('base/path', 'missing'); }); it('should ignore images with width or height attributes', () => { const docs = [{ docType: 'a', renderedContent: ` ` }]; processor.$process(docs); expect(docs).toEqual([jasmine.objectContaining({ docType: 'a', renderedContent: ` ` })]); }); function mockGetImageDimensions() { const imageInfo = { 'a/b.jpg': { width: 10, height: 20 }, 'c/d.png': { width: 30, height: 40 }, }; // eslint-disable-next-line jasmine/no-unsafe-spy return jasmine.createSpy('getImageDimensions') .and.callFake((base, url) => imageInfo[url]); } });