|
| 1 | +import { encode } from '../encode.js'; |
| 2 | +import { encodeDataURL } from '../encodeDataURL.js'; |
| 3 | + |
| 4 | +test('basic image (png)', () => { |
| 5 | + const image = testUtils.createGreyImage([ |
| 6 | + [0, 0, 0, 0, 0], |
| 7 | + [0, 255, 255, 255, 0], |
| 8 | + [0, 255, 0, 255, 0], |
| 9 | + [0, 255, 255, 255, 0], |
| 10 | + [255, 0, 255, 0, 255], |
| 11 | + ]); |
| 12 | + const base64Url = encodeDataURL(image); |
| 13 | + |
| 14 | + expect(base64Url).toBe( |
| 15 | + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==', |
| 16 | + ); |
| 17 | +}); |
| 18 | + |
| 19 | +test('basic image 2 (jpeg)', () => { |
| 20 | + const image = testUtils.createGreyImage([ |
| 21 | + [255, 255, 255, 255, 255], |
| 22 | + [255, 0, 0, 0, 255], |
| 23 | + [255, 0, 0, 0, 255], |
| 24 | + [255, 0, 0, 0, 255], |
| 25 | + [255, 255, 255, 255, 255], |
| 26 | + ]); |
| 27 | + const format = 'jpeg'; |
| 28 | + const base64 = encodeDataURL(image, { format }); |
| 29 | + const base64Data = Buffer.from(encode(image, { format })).toString('base64'); |
| 30 | + expect(typeof base64).toBe('string'); |
| 31 | + expect(base64Data).toMatchSnapshot(); |
| 32 | +}); |
| 33 | + |
| 34 | +test('legacy image-js test', () => { |
| 35 | + const image = testUtils.createRgbaImage( |
| 36 | + ` |
| 37 | + 255 0 0 / 255 | 0 255 0 / 255 | 0 0 255 / 255 |
| 38 | + 255 255 0 / 255 | 255 0 255 / 255 | 0 255 255 / 255 |
| 39 | + 0 0 0 / 255 | 255 255 255 / 255 | 127 127 127 / 255 |
| 40 | + `, |
| 41 | + ); |
| 42 | + const format = 'jpeg'; |
| 43 | + const url = encodeDataURL(image, { format }); |
| 44 | + const base64Data = Buffer.from(encode(image, { format })).toString('base64'); |
| 45 | + expect(typeof url).toBe('string'); |
| 46 | + expect(base64Data).toBe(url.slice(url.indexOf(',') + 1)); |
| 47 | +}); |
0 commit comments