|
| 1 | +// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.WASM. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +import { ImageMagick } from '../../src/image-magick'; |
| 5 | +import { MagickReadSettings } from '../../src/settings/magick-read-settings'; |
| 6 | +import { TestFiles } from '../test-files'; |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as util from 'util'; |
| 9 | +import { MagickFormat } from '../../src/magick-format'; |
| 10 | + |
| 11 | +beforeAll(() => { ImageMagick._api = (global as any).native; }); |
| 12 | + |
| 13 | +describe('ImageMagick#readCollection', () => { |
| 14 | + it('should read built-in image async', async () => { |
| 15 | + await ImageMagick.readCollection('logo:', async (images) => { |
| 16 | + expect(images.length).toBe(1); |
| 17 | + expect(images[0].width).toBe(640); |
| 18 | + expect(images[0].height).toBe(480); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should read built-in image', () => { |
| 23 | + ImageMagick.readCollection('wizard:', (images) => { |
| 24 | + expect(images.length).toBe(1); |
| 25 | + expect(images[0].width).toBe(480); |
| 26 | + expect(images[0].height).toBe(640); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should read image from array async', async () => { |
| 31 | + const readFile = util.promisify(fs.readFile); |
| 32 | + const data = await readFile(TestFiles.roseSparkleGif); |
| 33 | + await ImageMagick.readCollection(data, (images) => { |
| 34 | + expect(images.length).toBe(3); |
| 35 | + images.forEach(image => { |
| 36 | + expect(image.width).toBe(70); |
| 37 | + expect(image.height).toBe(46); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should read image from array', () => { |
| 43 | + const data = fs.readFileSync(TestFiles.roseSparkleGif); |
| 44 | + ImageMagick.readCollection(data, (images) => { |
| 45 | + expect(images.length).toBe(3); |
| 46 | + images.forEach(image => { |
| 47 | + expect(image.width).toBe(70); |
| 48 | + expect(image.height).toBe(46); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should read image from array with settings async', async () => { |
| 54 | + const settings = new MagickReadSettings( |
| 55 | + { |
| 56 | + format: MagickFormat.Png |
| 57 | + }); |
| 58 | + |
| 59 | + const readFile = util.promisify(fs.readFile); |
| 60 | + const data = await readFile(TestFiles.roseSparkleGif); |
| 61 | + await expect(async () => { |
| 62 | + await ImageMagick.readCollection(data, settings, () => { |
| 63 | + // will never be reached |
| 64 | + }); |
| 65 | + }).rejects.toThrowError('ImproperImageHeader'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should read image from array with settings', () => { |
| 69 | + const settings = new MagickReadSettings( |
| 70 | + { |
| 71 | + format: MagickFormat.Png |
| 72 | + }); |
| 73 | + |
| 74 | + const data = fs.readFileSync(TestFiles.roseSparkleGif); |
| 75 | + expect(() => { |
| 76 | + ImageMagick.readCollection(data, settings, () => { |
| 77 | + // will never be reached |
| 78 | + }); |
| 79 | + }).toThrowError('ImproperImageHeader'); |
| 80 | + }); |
| 81 | +}); |
0 commit comments