From 9d8b78065627fd35864c748217a831f6aff336d3 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Tue, 13 Jun 2023 09:01:53 +0200 Subject: [PATCH] chore: add wip test setup to example app --- e2e-tests/maestro-flow-android.yml | 2 +- e2e-tests/maestro-flow-ios.yml | 2 +- example/src/App.tsx | 2 + example/src/Tests.tsx | 114 +++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 example/src/Tests.tsx diff --git a/e2e-tests/maestro-flow-android.yml b/e2e-tests/maestro-flow-android.yml index 16506c2..4b762df 100644 --- a/e2e-tests/maestro-flow-android.yml +++ b/e2e-tests/maestro-flow-android.yml @@ -2,5 +2,5 @@ appId: com.opaqueexample --- - launchApp - assertVisible: - text: 'Run full flow' + text: 'Tests passed' enabled: true diff --git a/e2e-tests/maestro-flow-ios.yml b/e2e-tests/maestro-flow-ios.yml index 6535de2..d157f0b 100644 --- a/e2e-tests/maestro-flow-ios.yml +++ b/e2e-tests/maestro-flow-ios.yml @@ -2,5 +2,5 @@ appId: org.reactjs.native.example.OpaqueExample --- - launchApp - assertVisible: - text: 'Run Demo' + text: 'Tests passed' enabled: true diff --git a/example/src/App.tsx b/example/src/App.tsx index 4fd5a83..831d7de 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -2,6 +2,7 @@ import * as React from 'react'; import { Alert, Button, StyleSheet, Text, TextInput, View } from 'react-native'; import * as opaque from 'react-native-opaque'; +import { Tests } from './Tests'; async function request(method: string, url: string, body: any = undefined) { console.log(`${method} ${url}`, body); @@ -161,6 +162,7 @@ function App() { }} /> + ); } diff --git a/example/src/Tests.tsx b/example/src/Tests.tsx new file mode 100644 index 0000000..8b77dbf --- /dev/null +++ b/example/src/Tests.tsx @@ -0,0 +1,114 @@ +import React from 'react'; +import { Text, View } from 'react-native'; + +type ExpectResult = + | { + type: 'toBe'; + error: boolean; + actualValue: unknown; + comparisonValue: unknown; + } + | { + type: 'toBeUndefined'; + error: boolean; + actualValue: unknown; + }; + +type TestEntry = { + description: string; + failed: boolean; + expectResults: ExpectResult[]; +}; + +let tests: TestEntry[] = []; + +async function test(description: string, callback: () => void) { + const testEntry: TestEntry = { + description, + failed: true, + expectResults: [], + }; + tests.push(testEntry); + try { + callback(); + if (testEntry.expectResults.length === 0) { + testEntry.failed = true; + } else { + testEntry.failed = testEntry.expectResults.some((result) => result.error); + } + } catch (error) { + testEntry.failed = true; + } +} + +function expect(actualValue: unknown) { + const testEntry = tests[tests.length - 1]; + if (!testEntry) { + throw new Error('No test entry found'); + } + + return { + toBe(comparisonValue: unknown) { + if (actualValue !== comparisonValue) { + testEntry.expectResults.push({ + type: 'toBe', + error: true, + actualValue, + comparisonValue, + }); + } else { + testEntry.expectResults.push({ + type: 'toBe', + error: false, + actualValue, + comparisonValue, + }); + } + }, + toBeUndefined() { + if (actualValue === undefined) { + testEntry.expectResults.push({ + type: 'toBeUndefined', + error: false, + actualValue, + }); + } else { + testEntry.expectResults.push({ + type: 'toBeUndefined', + error: true, + actualValue, + }); + } + }, + }; +} + +export const Tests: React.FC = () => { + tests = []; + + test('1 === 1', async () => { + expect(1).toBe(1); + // expect(1).toBe(2); + }); + + test('something to be undefined', async () => { + expect(undefined).toBeUndefined(); + }); + + const allTestsPassed = tests.every((testEntry) => !testEntry.failed); + + return ( + + {allTestsPassed ? 'Tests passed' : 'Tests failed'} + {tests.map((result) => { + return ( + + + {result.description}: {result.failed ? '❌' : '✅'} + + + ); + })} + + ); +};