xref: /expo/apps/test-suite/tests/Clipboard.js (revision dfd15ebd)
1import * as Clipboard from 'expo-clipboard';
2import { Platform } from 'react-native';
3
4export const name = 'Clipboard';
5
6export function test({ describe, expect, it, afterEach, ...t }) {
7  describe('Clipboard', () => {
8    const throws = async (run) => {
9      let error = null;
10      try {
11        await run();
12      } catch (e) {
13        error = e;
14      }
15      expect(error).toBeTruthy();
16    };
17
18    afterEach(async () => {
19      await Clipboard.setStringAsync('');
20    });
21
22    describe('Strings', () => {
23      it('sets and gets a string', async () => {
24        await Clipboard.setStringAsync('test string');
25        const result = await Clipboard.getStringAsync();
26        expect(result).toEqual('test string');
27      });
28
29      it('checks if clipboard has string content', async () => {
30        await Clipboard.setStringAsync('test string');
31        const result = await Clipboard.hasStringAsync();
32        expect(result).toBe(true);
33      });
34
35      it('gets and sets HTML string', async () => {
36        await Clipboard.setStringAsync('<p>test</p>', {
37          inputFormat: Clipboard.StringFormat.HTML,
38        });
39        const result = await Clipboard.getStringAsync({
40          preferredFormat: Clipboard.StringFormat.HTML,
41        });
42        // The OS can add some atributes or inner tags to the HTML string, so we can't just
43        // check for equality.
44        expect(/<p(\s.*)?>(<.*>)?test(<\/.*>)?<\/p>/gi.test(result)).toBe(true);
45      });
46
47      it('gets plain text from copied HTML', async () => {
48        await Clipboard.setStringAsync('<p>test</p>', {
49          inputFormat: Clipboard.StringFormat.HTML,
50        });
51        const result = await Clipboard.getStringAsync({
52          preferredFormat: Clipboard.StringFormat.PLAIN_TEXT,
53        });
54        expect(result.trim()).toEqual('test');
55      });
56
57      it('falls back to plain text if no HTML is copied', async () => {
58        await Clipboard.setStringAsync('test', { inputFormat: Clipboard.StringFormat.PLAIN_TEXT });
59        const result = await Clipboard.getStringAsync({
60          preferredFormat: Clipboard.StringFormat.HTML,
61        });
62        expect(result).toEqual('test');
63      });
64    });
65
66    if (Platform.OS === 'ios') {
67      describe('URLs', () => {
68        it('sets and gets an url', async () => {
69          const exampleUrl = 'https://example.com';
70          let hasUrl = await Clipboard.hasUrlAsync();
71          expect(hasUrl).toEqual(false);
72          await Clipboard.setUrlAsync(exampleUrl);
73          hasUrl = await Clipboard.hasUrlAsync();
74          expect(hasUrl).toEqual(true);
75          const result = await Clipboard.getUrlAsync();
76          expect(result).toEqual(exampleUrl);
77        });
78
79        it('rejects a malformed url', async () => {
80          const malformedUrl = 'malformed url';
81          await throws(() => Clipboard.setUrlAsync(malformedUrl));
82          const hasUrl = await Clipboard.hasUrlAsync();
83          expect(hasUrl).toEqual(false);
84        });
85      });
86    }
87
88    describe('Images', () => {
89      it('sets and gets a png image', async () => {
90        const imageBase64 =
91          'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
92        const expectedResultRegex = 'data:image/png;base64,[A-Za-z0-9+/=]*';
93        let hasImage = await Clipboard.hasImageAsync();
94        expect(hasImage).toEqual(false);
95        await Clipboard.setImageAsync(imageBase64);
96        hasImage = await Clipboard.hasImageAsync();
97        expect(hasImage).toEqual(true);
98        const result = await Clipboard.getImageAsync({ format: 'png' });
99        expect(result.data).toMatch(expectedResultRegex);
100      });
101
102      if (Platform.OS !== 'web') {
103        it('sets and gets a jpg image', async () => {
104          const imageBase64 =
105            'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
106          const expectedResultRegex = 'data:image/jpeg;base64,[A-Za-z0-9+/=]*';
107          let hasImage = await Clipboard.hasImageAsync();
108          expect(hasImage).toEqual(false);
109          await Clipboard.setImageAsync(imageBase64);
110          hasImage = await Clipboard.hasImageAsync();
111          expect(hasImage).toEqual(true);
112          const result = await Clipboard.getImageAsync({ format: 'jpeg' });
113          expect(result.data).toMatch(expectedResultRegex);
114        });
115
116        it('rejects invalid base64', async () => {
117          const imageBase64 = 'invalid';
118          await throws(() => Clipboard.setImageAsync(imageBase64));
119          const hasImage = await Clipboard.hasImageAsync();
120          expect(hasImage).toEqual(false);
121        });
122      }
123    });
124  });
125}
126