Tests: bun test for the web stub, LEVELPLAY_DEMO and exports, compile-only public API checks; verify runs them
This commit is contained in:
parent
cd4147f719
commit
74a6310617
7 changed files with 219 additions and 1 deletions
|
|
@ -54,7 +54,8 @@
|
||||||
"clean": "rm -rf dist",
|
"clean": "rm -rf dist",
|
||||||
"docgen": "docgen --api LevelPlayPlugin --output-readme README.md --output-json dist/docs.json",
|
"docgen": "docgen --api LevelPlayPlugin --output-readme README.md --output-json dist/docs.json",
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"verify": "bun run typecheck && bun run build && bun run verify:ios && bun run verify:android",
|
"test": "bun test ./test && tsc -p test",
|
||||||
|
"verify": "bun run typecheck && bun run test && bun run build && bun run verify:ios && bun run verify:android",
|
||||||
"verify:ios": "xcodebuild build -scheme CapacitorLevelplay -destination 'generic/platform=iOS Simulator'",
|
"verify:ios": "xcodebuild build -scheme CapacitorLevelplay -destination 'generic/platform=iOS Simulator'",
|
||||||
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
||||||
"verify:web": "bun run build",
|
"verify:web": "bun run build",
|
||||||
|
|
|
||||||
8
test/bun-test.d.ts
vendored
Normal file
8
test/bun-test.d.ts
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
// Minimal typings for the parts of `bun:test` used here (keeps the repo free of @types/bun).
|
||||||
|
declare module 'bun:test' {
|
||||||
|
type Fn = () => void | Promise<void>;
|
||||||
|
export function describe(name: string, fn: () => void): void;
|
||||||
|
export function test(name: string, fn: Fn): void;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export function expect(value: unknown): any;
|
||||||
|
}
|
||||||
18
test/demo.test.ts
Normal file
18
test/demo.test.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { describe, expect, test } from 'bun:test';
|
||||||
|
|
||||||
|
import { LEVELPLAY_DEMO } from '../src/demo';
|
||||||
|
|
||||||
|
describe('LEVELPLAY_DEMO', () => {
|
||||||
|
for (const platform of ['android', 'ios'] as const) {
|
||||||
|
test(platform, () => {
|
||||||
|
const ids = LEVELPLAY_DEMO[platform];
|
||||||
|
expect(Object.keys(ids).sort()).toEqual(['appKey', 'banner', 'interstitial', 'rewarded']);
|
||||||
|
for (const v of Object.values(ids)) expect(v).toMatch(/^[a-z0-9]{8,16}$/);
|
||||||
|
expect(new Set(Object.values(ids)).size).toBe(4);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('platforms differ', () => {
|
||||||
|
expect(LEVELPLAY_DEMO.android.appKey).not.toBe(LEVELPLAY_DEMO.ios.appKey);
|
||||||
|
});
|
||||||
|
});
|
||||||
13
test/index.test.ts
Normal file
13
test/index.test.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { expect, test } from 'bun:test';
|
||||||
|
|
||||||
|
import * as api from '../src/index';
|
||||||
|
|
||||||
|
test('public exports', () => {
|
||||||
|
expect(typeof api.LevelPlay).toBe('object');
|
||||||
|
expect(api.LEVELPLAY_DEMO.android.appKey).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('web implementation is used outside native platforms', async () => {
|
||||||
|
expect(await api.LevelPlay.getSdkVersion()).toEqual({ sdkVersion: 'web' });
|
||||||
|
expect(await api.LevelPlay.getCapabilities()).toMatchObject({ banner: false });
|
||||||
|
});
|
||||||
5
test/tsconfig.json
Normal file
5
test/tsconfig.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"extends": "../tsconfig.json",
|
||||||
|
"compilerOptions": { "noEmit": true, "declaration": false, "sourceMap": false, "inlineSources": false, "lib": ["es2020", "dom"] },
|
||||||
|
"files": ["bun-test.d.ts", "types.ts", "web.test.ts", "demo.test.ts", "index.test.ts"]
|
||||||
|
}
|
||||||
83
test/types.ts
Normal file
83
test/types.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
// Compile-only checks of the public API (`bun run test` runs `tsc -p test`). Never executed.
|
||||||
|
import type { PluginListenerHandle } from '@capacitor/core';
|
||||||
|
|
||||||
|
import { LEVELPLAY_DEMO, LevelPlay } from '../src';
|
||||||
|
import type {
|
||||||
|
AdEvent,
|
||||||
|
BannerErrorEvent,
|
||||||
|
BannerEvent,
|
||||||
|
BannerResult,
|
||||||
|
Capabilities,
|
||||||
|
CreateBannerOptions,
|
||||||
|
ImpressionData,
|
||||||
|
LevelPlayPlugin,
|
||||||
|
LoadNativeAdOptions,
|
||||||
|
NativeAdEvent,
|
||||||
|
SegmentOptions,
|
||||||
|
ShowResult,
|
||||||
|
} from '../src';
|
||||||
|
|
||||||
|
type Equals<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
|
||||||
|
function assertType<T extends true>(_: T): void {}
|
||||||
|
|
||||||
|
export async function usage(plugin: LevelPlayPlugin = LevelPlay): Promise<void> {
|
||||||
|
const ids = LEVELPLAY_DEMO.android;
|
||||||
|
const init = await plugin.initialize({ appKey: ids.appKey, userId: 'u1', consent: { gdprConsent: true } });
|
||||||
|
assertType<Equals<typeof init.sdkVersion, string>>(true);
|
||||||
|
|
||||||
|
const caps: Capabilities = await plugin.getCapabilities();
|
||||||
|
if (caps.nativeAdTemplates.includes('medium')) {
|
||||||
|
/* typed template list */
|
||||||
|
}
|
||||||
|
|
||||||
|
const shown: ShowResult = await plugin.showRewarded({ adUnitId: ids.rewarded, placementName: 'Home' });
|
||||||
|
if (shown.rewarded && shown.reward) assertType<Equals<typeof shown.reward.amount, number>>(true);
|
||||||
|
|
||||||
|
const banner: CreateBannerOptions = {
|
||||||
|
adUnitId: ids.banner,
|
||||||
|
bannerId: 'main',
|
||||||
|
size: 'ADAPTIVE',
|
||||||
|
position: 'bottom',
|
||||||
|
offsetY: 8,
|
||||||
|
respectSafeArea: true,
|
||||||
|
};
|
||||||
|
const res: BannerResult = await plugin.createBanner(banner);
|
||||||
|
assertType<Equals<typeof res.frame.width, number>>(true);
|
||||||
|
await plugin.loadBanner({ bannerId: res.bannerId });
|
||||||
|
await plugin.setBannerLayout({ bannerId: 'main', position: 'top' });
|
||||||
|
await plugin.createBanner({ adUnitId: ids.banner, size: 'CUSTOM', customSize: { width: 320, height: 60 } });
|
||||||
|
|
||||||
|
const native: LoadNativeAdOptions = {
|
||||||
|
nativeAdId: 'n1',
|
||||||
|
template: 'small',
|
||||||
|
style: { backgroundColor: '#fff', cornerRadius: 8, titleFont: { family: 'fonts/Inter.ttf', size: 15 } },
|
||||||
|
};
|
||||||
|
await plugin.loadNativeAd(native);
|
||||||
|
await plugin.showNativeAd({ nativeAdId: 'n1', rect: { x: 0, y: 0, width: 300, height: 90 } });
|
||||||
|
|
||||||
|
const segment: SegmentOptions = { segmentName: 'whales', level: 3, isPaying: true, customParameters: { a: 'b' } };
|
||||||
|
await plugin.setSegment(segment);
|
||||||
|
const dyn = await plugin.setDynamicUserId({ userId: 'u2' });
|
||||||
|
assertType<Equals<typeof dyn.valid, boolean>>(true);
|
||||||
|
await plugin.isPlacementCapped({ format: 'interstitial', placementName: 'Level_End' });
|
||||||
|
|
||||||
|
const handles: PluginListenerHandle[] = [
|
||||||
|
await plugin.addListener('impressionData', (e: ImpressionData) => void e.revenue),
|
||||||
|
await plugin.addListener('bannerLoadFailed', (e: BannerErrorEvent) => void e.error.code),
|
||||||
|
await plugin.addListener('bannerLoaded', (e: BannerEvent) => void e.frame),
|
||||||
|
await plugin.addListener('nativeAdLoaded', (e: NativeAdEvent) => void e.nativeAdId),
|
||||||
|
await plugin.addListener('adInfoChanged', (e: AdEvent) => void e.adInfo?.revenue),
|
||||||
|
];
|
||||||
|
for (const h of handles) await h.remove();
|
||||||
|
|
||||||
|
// @ts-expect-error unknown banner size
|
||||||
|
await plugin.createBanner({ adUnitId: 'x', size: 'HUGE' });
|
||||||
|
// @ts-expect-error adUnitId is required
|
||||||
|
await plugin.createBanner({ bannerId: 'x' });
|
||||||
|
// @ts-expect-error unknown native template
|
||||||
|
await plugin.loadNativeAd({ nativeAdId: 'n', template: 'large' });
|
||||||
|
// @ts-expect-error banner is not a full-screen format
|
||||||
|
await plugin.isPlacementCapped({ format: 'banner', placementName: 'p' });
|
||||||
|
// @ts-expect-error demo ids are readonly
|
||||||
|
LEVELPLAY_DEMO.ios.appKey = 'x';
|
||||||
|
}
|
||||||
90
test/web.test.ts
Normal file
90
test/web.test.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import { describe, expect, test } from 'bun:test';
|
||||||
|
|
||||||
|
import type { LevelPlayPlugin } from '../src/definitions';
|
||||||
|
import { LevelPlayWeb } from '../src/web';
|
||||||
|
|
||||||
|
const web: LevelPlayPlugin = new LevelPlayWeb();
|
||||||
|
|
||||||
|
async function rejection(p: Promise<unknown>): Promise<{ code?: string; message?: string }> {
|
||||||
|
try {
|
||||||
|
await p;
|
||||||
|
} catch (e) {
|
||||||
|
return e as { code?: string; message?: string };
|
||||||
|
}
|
||||||
|
throw new Error('expected a rejection');
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('web stub: rejects as unimplemented (design §11)', () => {
|
||||||
|
const cases: [string, () => Promise<unknown>][] = [
|
||||||
|
['initialize', () => web.initialize({ appKey: 'k' })],
|
||||||
|
['launchTestSuite', () => web.launchTestSuite()],
|
||||||
|
['createBanner', () => web.createBanner({ adUnitId: 'u' })],
|
||||||
|
['showBanner', () => web.showBanner({ bannerId: 'b' })],
|
||||||
|
['setBannerLayout', () => web.setBannerLayout({ bannerId: 'b', position: 'top' })],
|
||||||
|
['loadNativeAd', () => web.loadNativeAd({ nativeAdId: 'n' })],
|
||||||
|
['showNativeAd', () => web.showNativeAd({ nativeAdId: 'n', rect: { x: 0, y: 0, width: 300, height: 90 } })],
|
||||||
|
];
|
||||||
|
for (const [name, call] of cases) {
|
||||||
|
test(name, async () => {
|
||||||
|
const err = await rejection(call());
|
||||||
|
expect(err.code).toBe('UNIMPLEMENTED');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('web stub: no-ops and fixed results', () => {
|
||||||
|
test('void no-ops resolve undefined', async () => {
|
||||||
|
const calls: Promise<void>[] = [
|
||||||
|
web.setConsent({ gdprConsent: true }),
|
||||||
|
web.setMetaData({ key: 'k', value: 'v' }),
|
||||||
|
web.loadRewarded({ adUnitId: 'u' }),
|
||||||
|
web.loadInterstitial({ adUnitId: 'u' }),
|
||||||
|
web.validateIntegration(),
|
||||||
|
web.setSegment({ level: 1 }),
|
||||||
|
web.loadBanner({ bannerId: 'b' }),
|
||||||
|
web.hideBanner({ bannerId: 'b' }),
|
||||||
|
web.destroyBanner({ bannerId: 'b' }),
|
||||||
|
web.setNativeAdRect({ nativeAdId: 'n', rect: { x: 0, y: 0, width: 1, height: 1 } }),
|
||||||
|
web.hideNativeAd({ nativeAdId: 'n' }),
|
||||||
|
web.destroyNativeAd({ nativeAdId: 'n' }),
|
||||||
|
];
|
||||||
|
for (const r of await Promise.all(calls)) expect(r).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sdk version and tracking', async () => {
|
||||||
|
expect(await web.getSdkVersion()).toEqual({ sdkVersion: 'web' });
|
||||||
|
expect(await web.requestTrackingAuthorization()).toEqual({ status: 'unavailable' });
|
||||||
|
expect(await web.getTrackingAuthorizationStatus()).toEqual({ status: 'unavailable' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('full-screen ads never fill', async () => {
|
||||||
|
expect(await web.isReady({ adUnitId: 'u', format: 'rewarded' })).toEqual({ ready: false });
|
||||||
|
for (const r of [await web.showRewarded({ adUnitId: 'u' }), await web.showInterstitial({ adUnitId: 'u' })]) {
|
||||||
|
expect(r.shown).toBe(false);
|
||||||
|
expect(r.rewarded).toBe(false);
|
||||||
|
expect(r.error?.code).toBe(-1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('show results are fresh copies', async () => {
|
||||||
|
const a = await web.showRewarded({ adUnitId: 'u' });
|
||||||
|
a.shown = true;
|
||||||
|
expect((await web.showRewarded({ adUnitId: 'u' })).shown).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('capabilities all false', async () => {
|
||||||
|
expect(await web.getCapabilities()).toEqual({
|
||||||
|
banner: false,
|
||||||
|
nativeAd: false,
|
||||||
|
nativeAdTemplates: [],
|
||||||
|
impressionData: false,
|
||||||
|
adInfoChanged: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('user and placement helpers', async () => {
|
||||||
|
expect(await web.setDynamicUserId({ userId: 'x' })).toEqual({ valid: false });
|
||||||
|
expect(await web.isPlacementCapped({ format: 'rewarded', placementName: 'p' })).toEqual({ capped: false });
|
||||||
|
expect(await web.getReward({ adUnitId: 'u' })).toEqual({ name: '', amount: 0 });
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue