@simba-dev/react-native-media-player 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -39
- package/package.json +2 -2
- package/src/components/PlayerProvider.tsx +288 -7
- package/src/hooks/SimbaPlayer.tsx +144 -0
- package/src/hooks/SimbaPlayerRoot.tsx +73 -0
- package/src/hooks/useLaunchParams.tsx +64 -0
- package/src/hooks/useOpenFromUrl.tsx +174 -0
- package/src/hooks/useOpenWithResume.tsx +236 -0
- package/src/hooks/usePlayerActivity.ts +103 -0
- package/src/hooks/useSimbaPlayerLookup.tsx +58 -0
- package/src/index.ts +26 -0
- package/src/types/__tests__/player.test.tsx +494 -0
- package/src/types/player.ts +947 -72
- package/src/types/__tests__/player.test.ts +0 -166
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unit tests for `src/types/player.ts` — the player state/commands API.
|
|
3
|
-
*
|
|
4
|
-
* Spec §Phase 34:
|
|
5
|
-
* - 34.2 usePlayer returns initial state — covered below.
|
|
6
|
-
* - 34.3 usePlayerProgress updates on event — current implementation
|
|
7
|
-
* is a Phase 24 stub (returns `{ positionMs: 0, durationMs: 0 }`).
|
|
8
|
-
* The "updates on event" wiring is Phase 25+/W7+; we test the
|
|
9
|
-
* initial-state contract here and leave the update contract for
|
|
10
|
-
* Phase 39 (instrumented test).
|
|
11
|
-
*
|
|
12
|
-
* The MpvPlayerModule bridge mock is installed globally in
|
|
13
|
-
* `jest.setup.ts` (it extends the @react-native/jest-preset's own
|
|
14
|
-
* `NativeModules` mock rather than overriding it).
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { renderHook } from '@testing-library/react-native';
|
|
18
|
-
import { NativeModules } from 'react-native';
|
|
19
|
-
import { usePlayer, usePlayerProgress } from '../player';
|
|
20
|
-
import type { PlayerState } from '../player';
|
|
21
|
-
|
|
22
|
-
// The default state from `player.ts` is module-private (DEFAULT_STATE,
|
|
23
|
-
// not exported). Pin the values here so the test catches an
|
|
24
|
-
// accidental refactor — any change to the documented baseline must
|
|
25
|
-
// be a deliberate choice.
|
|
26
|
-
const EXPECTED_DEFAULT_STATE: PlayerState = {
|
|
27
|
-
isPlaying: false,
|
|
28
|
-
title: 'Simba Player',
|
|
29
|
-
artist: '',
|
|
30
|
-
album: '',
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// Helper to clear all bridge mock call counts between tests. The
|
|
34
|
-
// global mock is shared across all test files in this module — see
|
|
35
|
-
// jest.setup.ts.
|
|
36
|
-
function clearBridgeMocks() {
|
|
37
|
-
for (const key of Object.keys(NativeModules.MpvPlayerModule)) {
|
|
38
|
-
const fn = (NativeModules.MpvPlayerModule as Record<string, jest.Mock>)[key];
|
|
39
|
-
if (typeof fn?.mockClear === 'function') {
|
|
40
|
-
fn.mockClear();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
describe('usePlayer', () => {
|
|
46
|
-
beforeEach(() => {
|
|
47
|
-
clearBridgeMocks();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('returns the documented initial state', async () => {
|
|
51
|
-
// Spec §Phase 34.2: usePlayer returns initial state.
|
|
52
|
-
const { result } = await renderHook(() => usePlayer());
|
|
53
|
-
expect(result.current.state).toEqual(EXPECTED_DEFAULT_STATE);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('initial state has isPlaying=false', async () => {
|
|
57
|
-
// The player can't be playing before any media has loaded.
|
|
58
|
-
const { result } = await renderHook(() => usePlayer());
|
|
59
|
-
expect(result.current.state.isPlaying).toBe(false);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('initial state has the documented baseline metadata', async () => {
|
|
63
|
-
// The current default title is "Simba Player" (placeholder shown
|
|
64
|
-
// before the first onFileLoaded event arrives). artist/album are
|
|
65
|
-
// empty strings (not undefined) so DefaultControls can render
|
|
66
|
-
// placeholder text without conditional checks at every consumer.
|
|
67
|
-
const { result } = await renderHook(() => usePlayer());
|
|
68
|
-
expect(result.current.state.title).toBe('Simba Player');
|
|
69
|
-
expect(result.current.state.artist).toBe('');
|
|
70
|
-
expect(result.current.state.album).toBe('');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('exposes the documented commands object', async () => {
|
|
74
|
-
// Pin the contract: usePlayer returns { state, commands }. A
|
|
75
|
-
// refactor that changes the shape would silently break
|
|
76
|
-
// DefaultControls.
|
|
77
|
-
const { result } = await renderHook(() => usePlayer());
|
|
78
|
-
expect(result.current.commands).toBeDefined();
|
|
79
|
-
expect(typeof result.current.commands.play).toBe('function');
|
|
80
|
-
expect(typeof result.current.commands.pause).toBe('function');
|
|
81
|
-
expect(typeof result.current.commands.seek).toBe('function');
|
|
82
|
-
expect(typeof result.current.commands.skipBackward).toBe('function');
|
|
83
|
-
expect(typeof result.current.commands.skipForward).toBe('function');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('commands.play delegates to the native bridge', async () => {
|
|
87
|
-
// Verify the bridge wiring (Phase 24): play() calls
|
|
88
|
-
// NativeModules.MpvPlayerModule.play() (the typed wrapper is
|
|
89
|
-
// `bridge.play` in MpvPlayerModule.ts).
|
|
90
|
-
const { result } = await renderHook(() => usePlayer());
|
|
91
|
-
await result.current.commands.play();
|
|
92
|
-
expect(NativeModules.MpvPlayerModule.play).toHaveBeenCalledTimes(1);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('commands.pause delegates to the native bridge', async () => {
|
|
96
|
-
const { result } = await renderHook(() => usePlayer());
|
|
97
|
-
await result.current.commands.pause();
|
|
98
|
-
expect(NativeModules.MpvPlayerModule.pause).toHaveBeenCalledTimes(1);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('commands.seek(ms) calls seekAbsolute on the bridge', async () => {
|
|
102
|
-
// Phase 24 wiring: commands.seek() translates to
|
|
103
|
-
// bridge.seekAbsolute(positionSec) — note seconds vs ms. The
|
|
104
|
-
// command takes ms (consumer-friendly) and converts internally.
|
|
105
|
-
const { result } = await renderHook(() => usePlayer());
|
|
106
|
-
await result.current.commands.seek(10000); // 10s in ms
|
|
107
|
-
expect(NativeModules.MpvPlayerModule.seekAbsolute).toHaveBeenCalledWith(
|
|
108
|
-
10, // 10000ms / 1000 = 10s
|
|
109
|
-
);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('commands.seek accepts zero', async () => {
|
|
113
|
-
// Edge case: seek to the start of the media. Should not throw.
|
|
114
|
-
const { result } = await renderHook(() => usePlayer());
|
|
115
|
-
expect(() => result.current.commands.seek(0)).not.toThrow();
|
|
116
|
-
expect(NativeModules.MpvPlayerModule.seekAbsolute).toHaveBeenCalledWith(0);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('commands.skipForward(seconds) calls seekForward on bridge', async () => {
|
|
120
|
-
const { result } = await renderHook(() => usePlayer());
|
|
121
|
-
await result.current.commands.skipForward(15);
|
|
122
|
-
expect(NativeModules.MpvPlayerModule.seekForward).toHaveBeenCalledWith(15);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('commands.skipBackward(seconds) calls seekBackward on bridge', async () => {
|
|
126
|
-
const { result } = await renderHook(() => usePlayer());
|
|
127
|
-
await result.current.commands.skipBackward(10);
|
|
128
|
-
expect(NativeModules.MpvPlayerModule.seekBackward).toHaveBeenCalledWith(10);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('returns stable command references across renders', async () => {
|
|
132
|
-
// Spec §Phase 34.5: PlayerProvider must not cause unnecessary
|
|
133
|
-
// re-renders. Stable command references let DefaultControls memo
|
|
134
|
-
// its handlers.
|
|
135
|
-
const { result, rerender } = await renderHook(() => usePlayer());
|
|
136
|
-
const firstCommands = result.current.commands;
|
|
137
|
-
await rerender({});
|
|
138
|
-
expect(result.current.commands).toBe(firstCommands);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('usePlayerProgress', () => {
|
|
143
|
-
it('returns the documented initial state', async () => {
|
|
144
|
-
// Spec §Phase 34.3: usePlayerProgress returns initial state. The
|
|
145
|
-
// current implementation is a Phase 24 stub; we pin the contract
|
|
146
|
-
// here so the eventual wiring (Phase 39 instrumented test) is a
|
|
147
|
-
// drop-in replacement.
|
|
148
|
-
const { result } = await renderHook(() => usePlayerProgress());
|
|
149
|
-
expect(result.current).toEqual({ positionMs: 0, durationMs: 0 });
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('returns 0/0 when called outside any provider', async () => {
|
|
153
|
-
// The hook must be safe to call anywhere — consumers can use
|
|
154
|
-
// it without wrapping in PlayerProvider. (Unlike usePlayerConfig,
|
|
155
|
-
// usePlayerProgress doesn't throw; see Phase 24 design notes.)
|
|
156
|
-
const { result } = await renderHook(() => usePlayerProgress());
|
|
157
|
-
expect(result.current.positionMs).toBe(0);
|
|
158
|
-
expect(result.current.durationMs).toBe(0);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('progress object has the documented fields', async () => {
|
|
162
|
-
const { result } = await renderHook(() => usePlayerProgress());
|
|
163
|
-
expect(typeof result.current.positionMs).toBe('number');
|
|
164
|
-
expect(typeof result.current.durationMs).toBe('number');
|
|
165
|
-
});
|
|
166
|
-
});
|