|
| 1 | +import { Platform } from 'react-native'; |
| 2 | + |
| 3 | +export type RNPlatform = 'ios' | 'android'; |
| 4 | + |
| 5 | +type PlatformFilter = |
| 6 | + | RNPlatform |
| 7 | + | RNPlatform[] |
| 8 | + | { only?: RNPlatform | RNPlatform[]; skip?: RNPlatform[] }; |
| 9 | + |
| 10 | +const SCOPE_KEY = '__MM_TEST_PLATFORM_SCOPE__'; |
| 11 | + |
| 12 | +function normalizeArray<T>(value: T | T[] | undefined): T[] { |
| 13 | + if (!value) { |
| 14 | + return []; |
| 15 | + } |
| 16 | + return Array.isArray(value) ? value : [value]; |
| 17 | +} |
| 18 | + |
| 19 | +function getDefaultPlatforms(): RNPlatform[] { |
| 20 | + const scoped = (globalThis as Record<string, unknown>)[SCOPE_KEY] as |
| 21 | + | RNPlatform |
| 22 | + | undefined; |
| 23 | + if (scoped === 'ios' || scoped === 'android') { |
| 24 | + return [scoped]; |
| 25 | + } |
| 26 | + const env = (process.env.TEST_OS ?? '').toLowerCase(); |
| 27 | + if (env === 'ios' || env === 'android') { |
| 28 | + return [env as RNPlatform]; |
| 29 | + } |
| 30 | + return ['ios', 'android']; |
| 31 | +} |
| 32 | + |
| 33 | +function resolveTargetPlatforms(filter?: PlatformFilter): RNPlatform[] { |
| 34 | + const defaultTargets = getDefaultPlatforms(); |
| 35 | + |
| 36 | + if (!filter) { |
| 37 | + return defaultTargets; |
| 38 | + } |
| 39 | + |
| 40 | + if (typeof filter === 'string' || Array.isArray(filter)) { |
| 41 | + const only = normalizeArray(filter) as RNPlatform[]; |
| 42 | + return only.length ? only : defaultTargets; |
| 43 | + } |
| 44 | + |
| 45 | + const only = normalizeArray(filter.only) as RNPlatform[]; |
| 46 | + const skip = new Set(normalizeArray(filter.skip) as RNPlatform[]); |
| 47 | + |
| 48 | + const base = (only.length ? only : defaultTargets) as RNPlatform[]; |
| 49 | + return base.filter((p) => !skip.has(p)); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Runs a test body for each targeted platform, defaulting to both iOS and Android. |
| 54 | + * Use TEST_OS=ios|android to globally filter, or the filter parameter to narrow per test. |
| 55 | + */ |
| 56 | +export function itForPlatforms( |
| 57 | + name: string, |
| 58 | + testFn: (ctx: { os: RNPlatform }) => void | Promise<void>, |
| 59 | + filter?: PlatformFilter, |
| 60 | +) { |
| 61 | + const targets = resolveTargetPlatforms(filter); |
| 62 | + for (const os of targets) { |
| 63 | + it(`${name} [${os}]`, async () => { |
| 64 | + const originalOS = Platform.OS; |
| 65 | + Platform.OS = os; |
| 66 | + try { |
| 67 | + await testFn({ os }); |
| 68 | + } finally { |
| 69 | + Platform.OS = originalOS; |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Same as itForPlatforms but focused (runs only these tests). |
| 77 | + */ |
| 78 | +export function itOnlyForPlatforms( |
| 79 | + name: string, |
| 80 | + testFn: (ctx: { os: RNPlatform }) => void | Promise<void>, |
| 81 | + filter?: PlatformFilter, |
| 82 | +) { |
| 83 | + const targets = resolveTargetPlatforms(filter); |
| 84 | + for (const os of targets) { |
| 85 | + it.only(`${name} [${os}]`, async () => { |
| 86 | + const originalOS = Platform.OS; |
| 87 | + Platform.OS = os; |
| 88 | + try { |
| 89 | + await testFn({ os }); |
| 90 | + } finally { |
| 91 | + Platform.OS = originalOS; |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Group tests under a describe for each targeted platform. |
| 99 | + */ |
| 100 | +export function describeForPlatforms( |
| 101 | + name: string, |
| 102 | + define: (ctx: { os: RNPlatform }) => void, |
| 103 | + filter?: PlatformFilter, |
| 104 | +) { |
| 105 | + const targets = resolveTargetPlatforms(filter); |
| 106 | + for (const os of targets) { |
| 107 | + describe(`${name} [${os}]`, () => { |
| 108 | + const originalOS = Platform.OS; |
| 109 | + beforeAll(() => { |
| 110 | + Platform.OS = os; |
| 111 | + (globalThis as Record<string, unknown>)[SCOPE_KEY] = os; |
| 112 | + }); |
| 113 | + afterAll(() => { |
| 114 | + Platform.OS = originalOS; |
| 115 | + delete (globalThis as Record<string, unknown>)[SCOPE_KEY]; |
| 116 | + }); |
| 117 | + define({ os }); |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Utility to compute target platforms if you need custom loops. |
| 124 | + */ |
| 125 | +export function getTargetPlatforms(filter?: PlatformFilter): RNPlatform[] { |
| 126 | + return resolveTargetPlatforms(filter); |
| 127 | +} |
0 commit comments