|
| 1 | +import { vi } from 'vitest'; |
1 | 2 |
|
2 |
| -import { vi } from "vitest"; |
| 3 | +import { createRgbaImage } from '../../../test/testUtils.js'; |
| 4 | +import { encode } from '../encode.js'; |
| 5 | +import { encodeBase64 } from '../encodeBase64.js'; |
3 | 6 |
|
4 |
| -import { createRgbaImage } from "../../../test/testUtils.js"; |
5 |
| -import { encode } from "../encode.js"; |
6 |
| -import { encodeBase64 } from "../encodeBase64.js"; |
7 |
| - |
8 |
| -test("basic image (png)",()=>{ |
| 7 | +test('basic image (png)', () => { |
9 | 8 | const image = testUtils.createGreyImage([
|
10 | 9 | [0, 0, 0, 0, 0],
|
11 | 10 | [0, 255, 255, 255, 0],
|
12 | 11 | [0, 255, 0, 255, 0],
|
13 | 12 | [0, 255, 255, 255, 0],
|
14 | 13 | [255, 0, 255, 0, 255],
|
15 | 14 | ]);
|
16 |
| - const base64Url = encodeBase64(image,'png'); |
| 15 | + const base64Url = encodeBase64(image, 'png'); |
17 | 16 |
|
18 |
| - expect(base64Url).toBe("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg=="); |
| 17 | + expect(base64Url).toBe( |
| 18 | + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==', |
| 19 | + ); |
19 | 20 | });
|
20 | 21 |
|
21 |
| -test("basic image 2 (jpeg)",()=>{ |
| 22 | +test('basic image 2 (jpeg)', () => { |
22 | 23 | const image = testUtils.createGreyImage([
|
23 | 24 | [255, 255, 255, 255, 255],
|
24 | 25 | [255, 0, 0, 0, 255],
|
25 | 26 | [255, 0, 0, 0, 255],
|
26 | 27 | [255, 0, 0, 0, 255],
|
27 | 28 | [255, 255, 255, 255, 255],
|
28 | 29 | ]);
|
29 |
| - const format = 'jpeg' |
30 |
| - const base64 = encodeBase64(image,format); |
31 |
| - const base64Data = Buffer.from(encode(image,{format})) |
32 |
| - .toString('base64'); |
| 30 | + const format = 'jpeg'; |
| 31 | + const base64 = encodeBase64(image, format); |
| 32 | + const base64Data = Buffer.from(encode(image, { format })).toString('base64'); |
33 | 33 | expect(typeof base64).toBe('string');
|
34 |
| - expect(base64Data).toBe(base64.slice( base64.indexOf(',') + 1)); |
| 34 | + expect(base64Data).toBe(base64.slice(base64.indexOf(',') + 1)); |
35 | 35 | });
|
36 | 36 |
|
37 |
| -test("legacy image-js test",()=>{ |
38 |
| - const image = createRgbaImage([[ |
39 |
| - 255, |
40 |
| - 0, |
41 |
| - 0, |
42 |
| - 255, // red |
43 |
| - 0, |
44 |
| - 255, |
45 |
| - 0, |
46 |
| - 255,// green |
47 |
| - 0, |
48 |
| - 0, |
49 |
| - 255, |
50 |
| - 255 // blue |
51 |
| - ],[255, |
| 37 | +test('legacy image-js test', () => { |
| 38 | + const image = createRgbaImage([ |
| 39 | + [ |
| 40 | + 255, |
| 41 | + 0, |
| 42 | + 0, |
| 43 | + 255, // red |
| 44 | + 0, |
| 45 | + 255, |
| 46 | + 0, |
| 47 | + 255, // green |
| 48 | + 0, |
| 49 | + 0, |
| 50 | + 255, |
| 51 | + 255, // blue |
| 52 | + ], |
| 53 | + [ |
52 | 54 | 255,
|
53 |
| - 0, |
54 |
| - 255,// yellow |
55 | 55 | 255,
|
56 | 56 | 0,
|
57 |
| - 255, |
58 |
| - 255,// magenta |
| 57 | + 255, // yellow |
| 58 | + 255, |
59 | 59 | 0,
|
60 | 60 | 255,
|
61 |
| - 255, |
62 |
| - 255,// cyan |
63 |
| - ],[ 0, |
64 |
| - 0, |
65 |
| - 0, |
66 |
| - 255,// black |
67 |
| - 255, |
68 |
| - 255, |
69 |
| - 255, |
70 |
| - 255, // white |
71 |
| - 127, |
72 |
| - 127, |
73 |
| - 127, |
74 |
| - 255,//grey |
75 |
| - ]]); |
76 |
| - const format = 'jpeg'; |
77 |
| - const url = encodeBase64(image,format); |
78 |
| - const base64Data = Buffer.from(encode(image,{format})) |
79 |
| - .toString('base64') |
80 |
| - expect(typeof url).toBe('string'); |
81 |
| - expect(base64Data).toBe(url.slice(url.indexOf(',') + 1)); |
82 |
| -}) |
83 |
| -test("browser testing",()=>{ |
| 61 | + 255, // magenta |
| 62 | + 0, |
| 63 | + 255, |
| 64 | + 255, |
| 65 | + 255, // cyan |
| 66 | + ], |
| 67 | + [ |
| 68 | + 0, |
| 69 | + 0, |
| 70 | + 0, |
| 71 | + 255, // black |
| 72 | + 255, |
| 73 | + 255, |
| 74 | + 255, |
| 75 | + 255, // white |
| 76 | + 127, |
| 77 | + 127, |
| 78 | + 127, |
| 79 | + 255, //grey |
| 80 | + ], |
| 81 | + ]); |
| 82 | + const format = 'jpeg'; |
| 83 | + const url = encodeBase64(image, format); |
| 84 | + const base64Data = Buffer.from(encode(image, { format })).toString('base64'); |
| 85 | + expect(typeof url).toBe('string'); |
| 86 | + expect(base64Data).toBe(url.slice(url.indexOf(',') + 1)); |
| 87 | +}); |
| 88 | +test('browser testing', () => { |
84 | 89 | const image = testUtils.createGreyImage([
|
85 | 90 | [255, 255, 255, 255, 255],
|
86 | 91 | [255, 0, 0, 0, 255],
|
87 | 92 | [255, 0, 0, 0, 255],
|
88 | 93 | [255, 0, 0, 0, 255],
|
89 | 94 | [255, 255, 255, 255, 255],
|
90 | 95 | ]);
|
91 |
| - const base64Node = encodeBase64(image,'jpg'); |
92 |
| - vi.stubGlobal('window',()=>{ |
93 |
| - const base64Browser = encodeBase64(image,'jpg'); |
94 |
| - expect(base64Browser).not.toBe(base64Node); |
95 |
| - }) |
96 |
| - vi.stubGlobal('document',()=>{ |
97 |
| - const base64Browser = encodeBase64(image,'jpg'); |
98 |
| - expect(base64Browser).not.toBe(base64Node); |
99 |
| - }) |
100 |
| -}) |
| 96 | + const base64Node = encodeBase64(image, 'jpg'); |
| 97 | + vi.stubGlobal('window', () => { |
| 98 | + const base64Browser = encodeBase64(image, 'jpg'); |
| 99 | + expect(base64Browser).not.toBe(base64Node); |
| 100 | + }); |
| 101 | + vi.stubGlobal('document', () => { |
| 102 | + const base64Browser = encodeBase64(image, 'jpg'); |
| 103 | + expect(base64Browser).not.toBe(base64Node); |
| 104 | + }); |
| 105 | +}); |
0 commit comments