RotNot / src /lib /native /__tests__ /native.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
6.25 kB
/**
* Tests for the RotNot native bridge (src/lib/native/index.ts)
*
* The bridge wraps Capacitor plugins behind feature-detection.
* All tests run in a plain Node environment β€” Capacitor plugins are NOT
* available, so all exported functions must be graceful no-ops.
*
* isNative() returns false in Node (no window.Capacitor), so we test only
* the no-op / web-fallback behaviour of all exported APIs.
*/
import { describe, it, expect } from "vitest";
import { isNative, nativeHaptics, nativePush, nativeNotifications, nativeStatusBar } from "../index";
// ─── isNative ─────────────────────────────────────────────────────────────
describe("isNative", () => {
it("returns false in a Node (non-browser) environment", () => {
// window is undefined in Node β€” isNative() guards against this
expect(isNative()).toBe(false);
});
it("returns a boolean", () => {
expect(typeof isNative()).toBe("boolean");
});
});
// ─── nativeHaptics ────────────────────────────────────────────────────────
describe("nativeHaptics", () => {
it("tap() resolves without error in non-native context", async () => {
await expect(nativeHaptics.tap()).resolves.toBeUndefined();
});
it("xpGain() resolves without error in non-native context", async () => {
await expect(nativeHaptics.xpGain()).resolves.toBeUndefined();
});
it("levelUp() resolves without error in non-native context", async () => {
await expect(nativeHaptics.levelUp()).resolves.toBeUndefined();
});
it("correct() resolves without error in non-native context", async () => {
await expect(nativeHaptics.correct()).resolves.toBeUndefined();
});
it("error() resolves without error in non-native context", async () => {
await expect(nativeHaptics.error()).resolves.toBeUndefined();
});
it("all haptic functions are safe to call multiple times", async () => {
for (let i = 0; i < 5; i++) {
await nativeHaptics.xpGain();
}
// no throw = pass
expect(true).toBe(true);
});
it("all haptic methods exist on the exported object", () => {
expect(typeof nativeHaptics.tap).toBe("function");
expect(typeof nativeHaptics.xpGain).toBe("function");
expect(typeof nativeHaptics.levelUp).toBe("function");
expect(typeof nativeHaptics.correct).toBe("function");
expect(typeof nativeHaptics.error).toBe("function");
});
});
// ─── nativePush ───────────────────────────────────────────────────────────
describe("nativePush", () => {
it("requestPermission() returns { granted: false } in non-native context", async () => {
const result = await nativePush.requestPermission();
expect(result).toEqual({ granted: false });
});
it("requestPermission() never throws", async () => {
await expect(nativePush.requestPermission()).resolves.not.toThrow();
});
it("requestPermission() returns a PushPermissionResult shape", async () => {
const result = await nativePush.requestPermission();
expect(typeof result.granted).toBe("boolean");
// token is optional β€” undefined is fine
expect("token" in result || result.token === undefined).toBe(true);
});
it("token is undefined when not granted", async () => {
const result = await nativePush.requestPermission();
expect(result.token).toBeUndefined();
});
});
// ─── nativeNotifications ──────────────────────────────────────────────────
describe("nativeNotifications", () => {
it("scheduleStreakReminder() resolves without error (non-native)", async () => {
await expect(
nativeNotifications.scheduleStreakReminder({ streakDays: 5 })
).resolves.toBeUndefined();
});
it("scheduleStreakReminder() accepts zero streak days", async () => {
await expect(
nativeNotifications.scheduleStreakReminder({ streakDays: 0 })
).resolves.toBeUndefined();
});
it("scheduleStreakReminder() accepts delayMinutes override", async () => {
await expect(
nativeNotifications.scheduleStreakReminder({ streakDays: 3, delayMinutes: 30 })
).resolves.toBeUndefined();
});
it("scheduleReviewReminder() resolves without error (non-native)", async () => {
const dueAt = new Date(Date.now() + 86_400_000);
await expect(
nativeNotifications.scheduleReviewReminder(3, dueAt)
).resolves.toBeUndefined();
});
it("scheduleReviewReminder() handles singular count", async () => {
const dueAt = new Date(Date.now() + 3_600_000);
await expect(
nativeNotifications.scheduleReviewReminder(1, dueAt)
).resolves.toBeUndefined();
});
it("scheduleReviewReminder() handles zero count", async () => {
const dueAt = new Date(Date.now() + 60_000);
await expect(
nativeNotifications.scheduleReviewReminder(0, dueAt)
).resolves.toBeUndefined();
});
it("cancelAll() resolves without error (non-native)", async () => {
await expect(nativeNotifications.cancelAll()).resolves.toBeUndefined();
});
it("all notification functions are exported", () => {
expect(typeof nativeNotifications.scheduleStreakReminder).toBe("function");
expect(typeof nativeNotifications.scheduleReviewReminder).toBe("function");
expect(typeof nativeNotifications.cancelAll).toBe("function");
});
});
// ─── nativeStatusBar ──────────────────────────────────────────────────────
describe("nativeStatusBar", () => {
it("setDark() resolves without error (non-native)", async () => {
await expect(nativeStatusBar.setDark()).resolves.toBeUndefined();
});
it("setDark() can be called multiple times safely", async () => {
await nativeStatusBar.setDark();
await nativeStatusBar.setDark();
expect(true).toBe(true);
});
});