|
| 1 | +import { describe, test, render, screen, expect } from 'react-native-harness'; |
| 2 | +import { View, Text } from 'react-native'; |
| 3 | + |
| 4 | +describe('Screenshot', () => { |
| 5 | + test('should match image snapshot with multiple snapshots', async () => { |
| 6 | + await render( |
| 7 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 8 | + <View |
| 9 | + style={{ |
| 10 | + width: 100, |
| 11 | + height: 100, |
| 12 | + backgroundColor: 'blue', |
| 13 | + justifyContent: 'center', |
| 14 | + alignItems: 'center', |
| 15 | + }} |
| 16 | + > |
| 17 | + <Text style={{ color: 'white' }}>Hello, world!</Text> |
| 18 | + </View> |
| 19 | + </View> |
| 20 | + ); |
| 21 | + const screenshot1 = await screen.screenshot(); |
| 22 | + await expect(screenshot1).toMatchImageSnapshot({ name: 'blue-square' }); |
| 23 | + |
| 24 | + // Change the background color and take another snapshot |
| 25 | + await render( |
| 26 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 27 | + <View |
| 28 | + style={{ |
| 29 | + width: 100, |
| 30 | + height: 100, |
| 31 | + backgroundColor: 'red', |
| 32 | + justifyContent: 'center', |
| 33 | + alignItems: 'center', |
| 34 | + }} |
| 35 | + > |
| 36 | + <Text style={{ color: 'white' }}>Hello, world!</Text> |
| 37 | + </View> |
| 38 | + </View> |
| 39 | + ); |
| 40 | + const screenshot2 = await screen.screenshot(); |
| 41 | + await expect(screenshot2).toMatchImageSnapshot({ name: 'red-square' }); |
| 42 | + |
| 43 | + // Take a third snapshot with different content |
| 44 | + await render( |
| 45 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 46 | + <View |
| 47 | + style={{ |
| 48 | + width: 100, |
| 49 | + height: 100, |
| 50 | + backgroundColor: 'green', |
| 51 | + justifyContent: 'center', |
| 52 | + alignItems: 'center', |
| 53 | + }} |
| 54 | + > |
| 55 | + <Text style={{ color: 'white' }}>Goodbye, world!</Text> |
| 56 | + </View> |
| 57 | + </View> |
| 58 | + ); |
| 59 | + const screenshot3 = await screen.screenshot(); |
| 60 | + await expect(screenshot3).toMatchImageSnapshot({ name: 'green-square' }); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should match image snapshot with custom options', async () => { |
| 64 | + await render( |
| 65 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 66 | + <View |
| 67 | + style={{ |
| 68 | + width: 100, |
| 69 | + height: 100, |
| 70 | + backgroundColor: 'yellow', |
| 71 | + justifyContent: 'center', |
| 72 | + alignItems: 'center', |
| 73 | + }} |
| 74 | + > |
| 75 | + <Text style={{ color: 'black' }}>Custom options test</Text> |
| 76 | + </View> |
| 77 | + </View> |
| 78 | + ); |
| 79 | + const screenshot = await screen.screenshot(); |
| 80 | + await expect(screenshot).toMatchImageSnapshot({ |
| 81 | + name: 'yellow-square-custom-options', |
| 82 | + threshold: 0.05, // More sensitive threshold |
| 83 | + diffColor: [0, 255, 0], // Green diff color |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should create diff image when test fails', async () => { |
| 88 | + await render( |
| 89 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 90 | + <View |
| 91 | + style={{ |
| 92 | + width: 100, |
| 93 | + height: 100, |
| 94 | + backgroundColor: 'purple', // Different color to cause mismatch |
| 95 | + justifyContent: 'center', |
| 96 | + alignItems: 'center', |
| 97 | + }} |
| 98 | + > |
| 99 | + <Text style={{ color: 'white' }}>This will fail</Text> |
| 100 | + </View> |
| 101 | + </View> |
| 102 | + ); |
| 103 | + const screenshot = await screen.screenshot(); |
| 104 | + // This should fail and create a diff image |
| 105 | + await expect(screenshot).toMatchImageSnapshot({ |
| 106 | + name: 'purple-square-will-fail', |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments