1import { Asset, FileSystem, ImageManipulator } from 'expo';
2
3export const name = 'ImageManipulator';
4
5export async function test(t) {
6  t.describe('ImageManipulator', async () => {
7    let image;
8
9    t.beforeAll(async () => {
10      image = Asset.fromModule(require('../assets/example_image_1.jpg'));
11      await image.downloadAsync();
12    });
13
14    t.describe('manipulateAsync()', async () => {
15      t.it('returns valid image', async () => {
16        const result = await ImageManipulator.manipulateAsync(image.localUri, [
17          { resize: { width: 100, height: 100 } },
18        ]);
19        t.expect(result).toBeDefined();
20        t.expect(typeof result.uri).toBe('string');
21        t.expect(typeof result.width).toBe('number');
22        t.expect(typeof result.height).toBe('number');
23      });
24
25      t.it('saves with default format', async () => {
26        const result = await ImageManipulator.manipulateAsync(image.localUri, [
27          { resize: { width: 100, height: 100 } },
28        ]);
29        t.expect(result.uri.endsWith('.jpg')).toBe(true);
30      });
31
32      t.it('saves as JPEG', async () => {
33        const result = await ImageManipulator.manipulateAsync(
34          image.localUri,
35          [{ resize: { width: 100, height: 100 } }],
36          {
37            format: ImageManipulator.SaveFormat.JPEG,
38          }
39        );
40
41        t.expect(result.uri.endsWith('.jpg')).toBe(true);
42      });
43
44      t.it('saves as PNG', async () => {
45        const result = await ImageManipulator.manipulateAsync(
46          image.localUri,
47          [{ resize: { width: 100, height: 100 } }],
48          {
49            format: ImageManipulator.SaveFormat.PNG,
50          }
51        );
52
53        t.expect(result.uri.endsWith('.png')).toBe(true);
54      });
55
56      t.it('provides Base64', async () => {
57        const result = await ImageManipulator.manipulateAsync(
58          image.localUri,
59          [{ resize: { width: 100, height: 100 } }],
60          {
61            base64: true,
62          }
63        );
64
65        t.expect(typeof result.base64).toBe('string');
66      });
67
68      t.it('performs compression', async () => {
69        const result = await ImageManipulator.manipulateAsync(
70          image.localUri,
71          [{ flip: ImageManipulator.FlipType.Vertical }],
72          {
73            compress: 0.0,
74          }
75        );
76
77        const imageInfo = await FileSystem.getInfoAsync(image.localUri);
78        const resultInfo = await FileSystem.getInfoAsync(result.uri);
79
80        t.expect(imageInfo.size).toBeGreaterThan(resultInfo.size);
81      });
82
83      t.it('rotates images', async () => {
84        const result = await ImageManipulator.manipulateAsync(image.localUri, [{ rotate: 45 }]);
85        t.expect(result.width).toBeGreaterThan(image.width);
86      });
87
88      t.it('flips horizontally', async () => {
89        const result = await ImageManipulator.manipulateAsync(image.localUri, [
90          { flip: ImageManipulator.FlipType.Horizontal },
91        ]);
92        t.expect(result.width).toBe(image.width);
93        t.expect(result.height).toBe(image.height);
94      });
95
96      t.it('flips vertically', async () => {
97        const result = await ImageManipulator.manipulateAsync(image.localUri, [
98          { flip: ImageManipulator.FlipType.Vertical },
99        ]);
100        t.expect(result.width).toBe(image.width);
101        t.expect(result.height).toBe(image.height);
102      });
103
104      t.it('resizes image', async () => {
105        const result = await ImageManipulator.manipulateAsync(image.localUri, [
106          { resize: { width: 100, height: 100 } },
107        ]);
108        t.expect(result.height).toBe(100);
109        t.expect(result.width).toBe(100);
110      });
111
112      t.it('cropes image', async () => {
113        const result = await ImageManipulator.manipulateAsync(image.localUri, [
114          { crop: { originX: 20, originY: 20, width: 100, height: 100 } },
115        ]);
116        t.expect(result.height).toBe(100);
117        t.expect(result.width).toBe(100);
118      });
119
120      t.it('performs multiple transformations', async () => {
121        const result = await ImageManipulator.manipulateAsync(image.localUri, [
122          { resize: { width: 200, height: 200 } },
123          { flip: ImageManipulator.FlipType.Vertical },
124          { rotate: 45 },
125          { crop: { originX: 20, originY: 20, width: 100, height: 100 } },
126        ]);
127        t.expect(result.height).toBe(100);
128        t.expect(result.width).toBe(100);
129      });
130    });
131  });
132}
133