Skip to content

Commit

Permalink
chore: add wip test setup to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
nikgraf committed Jun 13, 2023
1 parent 5420edb commit 9d8b780
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/maestro-flow-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ appId: com.opaqueexample
---
- launchApp
- assertVisible:
text: 'Run full flow'
text: 'Tests passed'
enabled: true
2 changes: 1 addition & 1 deletion e2e-tests/maestro-flow-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ appId: org.reactjs.native.example.OpaqueExample
---
- launchApp
- assertVisible:
text: 'Run Demo'
text: 'Tests passed'
enabled: true
2 changes: 2 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -161,6 +162,7 @@ function App() {
}}
/>
</View>
<Tests />
</View>
);
}
Expand Down
114 changes: 114 additions & 0 deletions example/src/Tests.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<View>
<Text>{allTestsPassed ? 'Tests passed' : 'Tests failed'}</Text>
{tests.map((result) => {
return (
<View key={performance.now()}>
<Text>
{result.description}: {result.failed ? '❌' : '✅'}
</Text>
</View>
);
})}
</View>
);
};

0 comments on commit 9d8b780

Please sign in to comment.