|
| 1 | +import { toMatchImageSnapshot } from 'jest-image-snapshot'; |
| 2 | +import { ImageCache } from '../imagecache'; |
| 3 | + |
| 4 | +expect.extend({ toMatchImageSnapshot }); |
| 5 | + |
| 6 | +// TODO: Fix these tests. |
| 7 | +test('applies scale and crop with exact dimensions', async () => { |
| 8 | + const imagecache = new ImageCache([ |
| 9 | + { |
| 10 | + presetName: 'scale_and_crop', |
| 11 | + actions: [ |
| 12 | + { |
| 13 | + action: 'scale_and_crop', |
| 14 | + config: { |
| 15 | + width: 200, |
| 16 | + height: 100, |
| 17 | + }, |
| 18 | + }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + ]); |
| 22 | + |
| 23 | + const image = await imagecache.render('./examples/in.png', 'scale_and_crop'); |
| 24 | + const imageBuffer = await image.toBuffer(); |
| 25 | + expect(imageBuffer).toMatchImageSnapshot({ |
| 26 | + failureThresholdType: 'percent', |
| 27 | + failureThreshold: 10, |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +test('applies scale and crop with relative dimensions', async () => { |
| 32 | + const imagecache = new ImageCache([ |
| 33 | + { |
| 34 | + presetName: 'scale_and_crop', |
| 35 | + actions: [ |
| 36 | + { |
| 37 | + action: 'scale_and_crop', |
| 38 | + config: { |
| 39 | + width: '50%', |
| 40 | + height: '50%', |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + ]); |
| 46 | + |
| 47 | + const image = await imagecache.render('./examples/in.png', 'scale_and_crop'); |
| 48 | + const imageBuffer = await image.toBuffer(); |
| 49 | + expect(imageBuffer).toMatchImageSnapshot({ |
| 50 | + failureThresholdType: 'percent', |
| 51 | + failureThreshold: 10, |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('applies scale and crop with a mix of exact and relative dimensions', async () => { |
| 56 | + const imagecache = new ImageCache([ |
| 57 | + { |
| 58 | + presetName: 'scale_and_crop', |
| 59 | + actions: [ |
| 60 | + { |
| 61 | + action: 'scale_and_crop', |
| 62 | + config: { |
| 63 | + width: '50%', |
| 64 | + height: 300, |
| 65 | + }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + ]); |
| 70 | + |
| 71 | + const image = await imagecache.render('./examples/in.png', 'scale_and_crop'); |
| 72 | + const imageBuffer = await image.toBuffer(); |
| 73 | + expect(imageBuffer).toMatchImageSnapshot({ |
| 74 | + failureThresholdType: 'percent', |
| 75 | + failureThreshold: 10, |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +// test('does not apply scale when dimensions are missing', async () => { |
| 80 | +// const imagecache = new ImageCache([ |
| 81 | +// { |
| 82 | +// presetName: 'scale', |
| 83 | +// actions: [ |
| 84 | +// { |
| 85 | +// action: 'scale', |
| 86 | +// config: {}, |
| 87 | +// }, |
| 88 | +// ], |
| 89 | +// }, |
| 90 | +// ]); |
| 91 | +// |
| 92 | +// const image = await imagecache.render('./examples/in.png', 'scale'); |
| 93 | +// const imageBuffer = await image.toBuffer(); |
| 94 | +// expect(imageBuffer).toMatchImageSnapshot({ |
| 95 | +// failureThresholdType: 'percent', |
| 96 | +// failureThreshold: 1, |
| 97 | +// }); |
| 98 | +// }); |
| 99 | +// |
| 100 | +// test('does not scale when upscale is false and final dimensions are bigger', async () => { |
| 101 | +// const imagecache = new ImageCache([ |
| 102 | +// { |
| 103 | +// presetName: 'scale', |
| 104 | +// actions: [ |
| 105 | +// { |
| 106 | +// action: 'scale', |
| 107 | +// config: { |
| 108 | +// width: 20000, |
| 109 | +// height: 10000, |
| 110 | +// upscale: false, |
| 111 | +// }, |
| 112 | +// }, |
| 113 | +// ], |
| 114 | +// }, |
| 115 | +// ]); |
| 116 | +// |
| 117 | +// const image = await imagecache.render('./examples/in.png', 'scale'); |
| 118 | +// const imageBuffer = await image.toBuffer(); |
| 119 | +// expect(imageBuffer).toMatchImageSnapshot({ |
| 120 | +// failureThresholdType: 'percent', |
| 121 | +// failureThreshold: 1, |
| 122 | +// }); |
| 123 | +// }); |
| 124 | +// |
| 125 | +// test('applies scale when width is missing', async () => { |
| 126 | +// const imagecache = new ImageCache([ |
| 127 | +// { |
| 128 | +// presetName: 'scale', |
| 129 | +// actions: [ |
| 130 | +// { |
| 131 | +// action: 'scale', |
| 132 | +// config: { |
| 133 | +// height: 100, |
| 134 | +// }, |
| 135 | +// }, |
| 136 | +// ], |
| 137 | +// }, |
| 138 | +// ]); |
| 139 | +// |
| 140 | +// const image = await imagecache.render('./examples/in.png', 'scale'); |
| 141 | +// const imageBuffer = await image.toBuffer(); |
| 142 | +// expect(imageBuffer).toMatchImageSnapshot({ |
| 143 | +// failureThresholdType: 'percent', |
| 144 | +// failureThreshold: 10, |
| 145 | +// }); |
| 146 | +// }); |
| 147 | +// |
| 148 | +// test('applies scale when height is missing', async () => { |
| 149 | +// const imagecache = new ImageCache([ |
| 150 | +// { |
| 151 | +// presetName: 'scale', |
| 152 | +// actions: [ |
| 153 | +// { |
| 154 | +// action: 'scale', |
| 155 | +// config: { |
| 156 | +// width: 200, |
| 157 | +// }, |
| 158 | +// }, |
| 159 | +// ], |
| 160 | +// }, |
| 161 | +// ]); |
| 162 | +// |
| 163 | +// const image = await imagecache.render('./examples/in.png', 'scale'); |
| 164 | +// const imageBuffer = await image.toBuffer(); |
| 165 | +// expect(imageBuffer).toMatchImageSnapshot({ |
| 166 | +// failureThresholdType: 'percent', |
| 167 | +// failureThreshold: 10, |
| 168 | +// }); |
| 169 | +// }); |
0 commit comments