Skip to content

Commit

Permalink
Make auth hooks tests CI friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
takameyer committed Jun 5, 2023
1 parent 93ba265 commit 10eb99f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 63 deletions.
37 changes: 30 additions & 7 deletions .github/workflows/package-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,49 @@ jobs:
strategy:
fail-fast: false
matrix:
workspace:
- realm
- '@realm/bindgen'
- '@realm/network-transport'
- '@realm/babel-plugin'
- '@realm/react'
name: ${{ matrix.workspace }} unit tests
variant:
- {workspace: realm}
- {workspace: '@realm/bindgen'}
- {workspace: '@realm/network-transport'}
- {workspace: '@realm/babel-plugin'}
- {workspace: '@realm/react', use-baas: true}
name: ${{ matrix.variant.workspace }} unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: "recursive"

- uses: actions/setup-node@v3
with:
node-version: 18

# ninja-build is used by default if available and results in faster build times
- name: Install ninja
run: sudo apt-get install ninja-build

- name: ccache
uses: hendrikmuhs/ccache-action@v1

- name: Generate server configuration
if: ${{matrix.variant.use-baas}}
id: baas-config
run:
suffix=$(node -p 'Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)');
subdomain="realm-js-test-server-${{ github.run_id }}-${{ github.run_attempt }}-${suffix}";
echo "subdomain=${subdomain}" >> $GITHUB_OUTPUT;
echo "url=https://${subdomain}.ngrok.io" >> $GITHUB_OUTPUT;

- name: Trigger the test server workflow to start the server
if: ${{matrix.variant.use-baas}}
run: gh workflow run test-server.yml -f ngrok_subdomain=${{ steps.baas-config.outputs.subdomain }} -f run_id=${{ github.run_id }}
env:
GH_TOKEN: ${{ github.token }}

- name: Set baas env
if: ${{matrix.variant.use-baas}}
run: echo "realmBaseUrl=${{ steps.baas-config.outputs.url }}" >> $GITHUB_ENV

# Install the root package to get dev-dependencies
# (--ignore-scripts to avoid downloading or building the native module)
- run: npm ci --ignore-scripts
Expand Down
52 changes: 52 additions & 0 deletions packages/realm-react/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { AppConfig, AppImporter, Credentials } from "@realm/app-importer";

const { realmBaseUrl = "http://localhost:9090" } = process.env;

export const baseUrl = realmBaseUrl;

function getCredentials(): Credentials {
const { publicKey, privateKey, username = "unique_user@domain.com", password = "password" } = process.env;
if (typeof publicKey === "string" && typeof privateKey === "string") {
return {
kind: "api-key",
publicKey,
privateKey,
};
} else {
return {
kind: "username-password",
username,
password,
};
}
}

const credentials = getCredentials();

const importer = new AppImporter({
baseUrl: realmBaseUrl,
credentials,
});

export async function importApp(config: AppConfig): Promise<{ appId: string }> {
const { appId } = await importer.importApp(config);
return { appId };
}
41 changes: 16 additions & 25 deletions packages/realm-react/src/__tests__/useAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,13 @@ import React from "react";
import { AppProvider } from "../AppProvider";
import { waitFor, renderHook, act } from "@testing-library/react-native";

import { AppConfigBuilder, AppImporter, Credentials } from "@realm/app-importer";
import { ImportedApp } from "@realm/app-importer/src/AppImporter";
import { AppConfigBuilder } from "@realm/app-importer";
import { useAuth } from "../useAuth";
import { baseUrl, importApp } from "./helpers";

const credentials: Credentials = {
kind: "username-password",
username: "unique_user@domain.com",
password: "password",
};

const baseUrl = "http://localhost:9090";
const appImporter = new AppImporter({ baseUrl, credentials });

function renderAuth(importedApp: ImportedApp) {
function renderAuth(appId: string, baseUrl: string) {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<AppProvider id={importedApp.appId} baseUrl={baseUrl}>
<AppProvider id={appId} baseUrl={baseUrl}>
{children}
</AppProvider>
);
Expand All @@ -45,13 +36,13 @@ function renderAuth(importedApp: ImportedApp) {
// The tests for the authentication methods themselves should be written elsewhere
describe("useAuth", () => {
describe("all methods are callable and report a state", () => {
let importedApp: ImportedApp;
let appId: string;
beforeAll(async () => {
const config = new AppConfigBuilder("test-app");
importedApp = await appImporter.importApp(config.config);
({ appId } = await importApp(config.config));
});
it("logIn", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logIn({ email: "test@test.com", password: "password" });
await waitFor(() => {
Expand All @@ -63,7 +54,7 @@ describe("useAuth", () => {
});
});
it("logInWithAnonymous", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithAnonymous();
await waitFor(() => {
Expand All @@ -75,7 +66,7 @@ describe("useAuth", () => {
});
});
it("logInWithApiKey", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithApiKey("12345");
await waitFor(() => {
Expand All @@ -87,7 +78,7 @@ describe("useAuth", () => {
});
});
it("logInWithEmailPassword", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithEmailPassword({ email: "test@test.com", password: "password" });
await waitFor(() => {
Expand All @@ -99,7 +90,7 @@ describe("useAuth", () => {
});
});
it("logInWithJWT", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithJWT("token");
await waitFor(() => {
Expand All @@ -111,7 +102,7 @@ describe("useAuth", () => {
});
});
it("logInWithGoogle", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithGoogle({ idToken: "1234" });
await waitFor(() => {
Expand All @@ -123,7 +114,7 @@ describe("useAuth", () => {
});
});
it("logInWithApple", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithApple("token");
await waitFor(() => {
Expand All @@ -135,7 +126,7 @@ describe("useAuth", () => {
});
});
it("logInWithFacebook", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithFacebook("token");
await waitFor(() => {
Expand All @@ -147,7 +138,7 @@ describe("useAuth", () => {
});
});
it("logInWithFunction", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logInWithFunction({ foo: "bar" });
await waitFor(() => {
Expand All @@ -159,7 +150,7 @@ describe("useAuth", () => {
});
});
it("logOut", async () => {
const { result } = renderAuth(importedApp);
const { result } = renderAuth(appId, baseUrl);
await act(async () => {
result.current.logOut();
await waitFor(() => {
Expand Down
Loading

0 comments on commit 10eb99f

Please sign in to comment.