Skip to content

Commit

Permalink
Added react test action
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Sep 9, 2024
1 parent 992e9e7 commit 2e7455a
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 261 deletions.
15 changes: 15 additions & 0 deletions .github/actions/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Prepare PowerAuth JS environment

runs:
using: composite
steps:
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22
- name: Install dependencies
run: npm i
- name: Set .env file
env:
ENV_TEST_FILE: ${{ secrets.ENV_TEST_FILE }}
run: echo -e "$ENV_TEST_FILE" > testapp/.env
28 changes: 14 additions & 14 deletions .github/workflows/test-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22
- name: Install dependencies
run: npm i
- name: Install Cordova
run: npm install -g cordova
- name: Set .env file
env:
ENV_TEST_FILE: ${{ secrets.ENV_TEST_FILE }}
run: echo -e "$ENV_TEST_FILE" > testapp/.env
- name: Run iOS Tests
run: npm run buildAndRunCordovaIosTests
- name: Prepare environment
uses: ./.github/actions/prepare
- name: Run Cordova iOS Tests
run: npm run buildAndRunCordovaIosTests
test-react-native:
name: Test React Native iOS
runs-on: macos-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Prepare environment
uses: ./.github/actions/prepare
- name: Run React-Native iOS Tests
run: npm run buildAndRunReactIosTests
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"source": "src/index",
"scripts": {
"build": "node node_modules/gulp/bin/gulp.js",
"buildAndRunCordovaIosTests": "npm run build && pushd testapp-cordova && npm run reinstallPlugin && npm run build && cordova run ios && popd && node test-listener.js"
"buildAndRunCordovaIosTests": "npm run build && pushd testapp-cordova && npm run reinstallPlugin && npm run build && npm run ios && popd && node test-listener.js",
"buildAndRunReactIosTests": "npm run build && pushd testapp && npm run reinstallPlugin && npm run ios && popd && node test-listener.js"
},
"files": [
"README.md",
Expand Down
2 changes: 1 addition & 1 deletion testapp-cordova/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const envConfigStr = `const EnvConfig = ${JSON.stringify(envConfig)};`

const copyTestFiles = () =>
gulp
.src([`${rnTestAppDir}/src/testbed/**/**.ts`, `${rnTestAppDir}/src/Config.ts`, `${rnTestAppDir}/_tests/**/**.ts`], { base: rnTestAppDir })
.src([`${rnTestAppDir}/src/testbed/**/**.ts`, `${rnTestAppDir}/src/Config.ts`, `${rnTestAppDir}/src/TestExecutor.ts`, `${rnTestAppDir}/_tests/**/**.ts`], { base: rnTestAppDir })
.pipe(replace(/import {[a-zA-Z }\n,]+from "react-native-powerauth-mobile-sdk";/g, ''))
.pipe(replace('import { Platform } from "react-native";', platformClass))
.pipe(replace('import { Config as EnvConfig } from "react-native-config";', envConfigStr))
Expand Down
6 changes: 3 additions & 3 deletions testapp-cordova/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"description": "A sample Cordova application that runs PowerAuth tests.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"installPlugin": "cordova plugin add ../build/cdv",
"reinstallPlugin": "(cordova plugin remove cordova-powerauth-mobile-sdk || true) && npm run installPlugin",
"ios": "npx cordova run ios",
"installPlugin": "npx cordova plugin add ../build/cdv",
"reinstallPlugin": "(npx cordova plugin remove cordova-powerauth-mobile-sdk || true) && npm run installPlugin",
"build": "node node_modules/gulp/bin/gulp.js"
},
"keywords": [
Expand Down
145 changes: 3 additions & 142 deletions testapp-cordova/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,143 +14,9 @@
// limitations under the License.
//

import { getInteractiveLibraryTests, getLibraryTests, getTestbedTests } from '../_tests/AllTests'
import { getTestConfig } from './Config'
import { TestContext, UserPromptDuration, UserInteraction, TestCounter, TestProgressObserver } from './testbed'
import { TestLog } from './testbed/TestLog'
import { TestMonitorGroup } from './testbed/TestMonitor'
import { TestRunner } from './testbed/TestRunner'

// TODO: this is copied from the react native app. improve - extract instead of copy
class TestExecutor implements UserInteraction {
// @ts-nocheck

private isRunning = false
private readonly onShowPrompt: (context: TestContext, message: string, duration: number) => Promise<void>
private readonly onProgress: TestProgressObserver
private readonly onCompletion: (inProgress: boolean) => void
private testRunner?: TestRunner


constructor(
onShowPrompt: (context: TestContext, message: string, duration: number) => Promise<void>,
onProgress: TestProgressObserver,
onCompletion: (inProgress: boolean)=>void) {
this.onShowPrompt = onShowPrompt
this.onProgress = onProgress
this.onCompletion = onCompletion
this.runTests(false)
}

async runTests(interactive: boolean) {
if (this.isRunning) {
console.warn('Tests are still in progress...');
return
}
this.onCompletion(true)
this.isRunning = true

const cfg = await getTestConfig()
const logger = new TestLog()
const monitor = new TestMonitorGroup([ logger ])
const runner = this.testRunner = new TestRunner('Automatic tests', cfg, monitor, this)
runner.allTestsCounter.addObserver(this.onProgress)
const tests = interactive ? getInteractiveLibraryTests() : getLibraryTests().concat(getTestbedTests())
try {
await runner.runTests(tests)
} catch (e) {
console.log("Run Tests failed");
console.error(e);
}
this.isRunning = false
this.testRunner = undefined
this.onCompletion(false)
}

cancelTests() {
this.testRunner?.cancelRunningTests()
}

stillRunnint(): boolean {
return this.isRunning
}

async showPrompt(context: TestContext, message: string, duration: UserPromptDuration): Promise<void> {
let sleepDuration: number
if (duration === UserPromptDuration.QUICK) {
sleepDuration = 500
} else if (duration === UserPromptDuration.SHORT) {
sleepDuration = 2000
} else {
sleepDuration = 5000
}
return await this.onShowPrompt(context, message, sleepDuration)
}

async sleepWithProgress(context: TestContext, durationMs: number): Promise<void> {
let remaining = durationMs
while (remaining > 0) {
if (remaining >= 1000) {
const timeInSeconds = Math.round(remaining * 0.001)
if (timeInSeconds > 1) {
await this.onShowPrompt(context, `Sleeping for ${timeInSeconds} seconds...`, 1000)
} else {
await this.onShowPrompt(context, `Finishing sleep...`, 1000)
}
remaining -= 1000
} else {
// Otherwise just sleep for the remaining time
await new Promise<void>(resolve => setTimeout(resolve, remaining))
remaining = 0
}
}
}
}

declare var cordova: any;

class TestServer {

log(...data) {
this.call("log", data)
}

reportStatus(data) {
this.call("reportStatus", data)
}

private call(method, object) {
fetch("http://localhost:8083/" + method, { method: "POST", body: JSON.stringify(object) }).catch((e) => {
// do we need to react?
})
}
}

const server = new TestServer();

const logF = console.log;
const warnF = console.warn;
const errorF = console.error;
const infoF = console.info;

console.log = (...params) => {
server.log(params)
logF(...params);
}

console.warn = (...params) => {
server.log(params)
warnF(...params);
}

console.info = (...params) => {
server.log(params)
infoF(...params);
}

console.error = (...params) => {
server.log(params)
errorF(...params);
}
import { TestExecutor } from './TestExecutor'

document.addEventListener('deviceready', onDeviceReady, false);

Expand All @@ -163,17 +29,12 @@ function onDeviceReady() {

console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
document.getElementById('deviceready').classList.add('ready');
document.getElementById('deviceready').addEventListener('click', async function (e) {

})

const executor = new TestExecutor(async (_context, message, duration) => {
console.log(message)
await new Promise<void>(resolve => setTimeout(resolve, duration))
}, (progress) => {
const text = `${progress.succeeded} succeeded<br>${progress.failed} failed<br>${progress.skipped} skipped<br>out of total ${progress.total}`;
server.reportStatus(progress)
progressEl.innerHTML = text;
progressEl.innerHTML = `${progress.succeeded} succeeded<br>${progress.failed} failed<br>${progress.skipped} skipped<br>out of total ${progress.total}`;;
}, (finished) => {
statusEl.innerHTML = finished ? "Tests running" : "Tests finished";
})
Expand Down
19 changes: 10 additions & 9 deletions testapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions testapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"android": "npx react-native run-android",
"ios": "npx react-native run-ios",
"start": "npx react-native start",
"pods": "pushd ios; pod install; popd",
"test": "echo Just run this app; exit 1",
"lint": "eslint .",
"reinstallPlugin": "npm r react-native-powerauth-mobile-sdk && npm i ../build/react-native/react-native-powerauth-mobile-sdk-2.5.0.tgz"
"reinstallPlugin": "(npm r react-native-powerauth-mobile-sdk || true) && npm i ../build/react-native/react-native-powerauth-mobile-sdk-2.5.0.tgz"
},
"dependencies": {
"chalk": "^4.1.0",
"powerauth-js-test-client": "^0.2.9",
"powerauth-js-test-client": "^0.2.12",
"react": "18.2.0",
"react-native": "0.71.6",
"react-native-config": "^1.5.0",
Expand Down
Loading

0 comments on commit 2e7455a

Please sign in to comment.