@symbiote-native/cellular 0.0.1 → 0.1.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/build/angular/services/permissions.service/index.js +1 -1
- package/build/core/cellular.js +2 -2
- package/build/react/hooks/use-permissions/index.js +1 -1
- package/build/vue/composables/use-permissions/index.js +1 -1
- package/package.json +9 -6
- package/src/angular/services/permissions.service/permissions.service.test.ts +0 -109
- package/src/core/cellular.test.ts +0 -193
- package/src/react/hooks/use-permissions/use-permissions.test.tsx +0 -115
- package/src/vue/composables/use-permissions/use-permissions.test.ts +0 -103
|
@@ -38,7 +38,7 @@ var __runInitializers = (this && this.__runInitializers) || function (thisArg, i
|
|
|
38
38
|
// readonly status = inject(PermissionsService).connect();
|
|
39
39
|
// // template: {{ status()?.granted }}
|
|
40
40
|
import { Injectable, signal } from '@angular/core';
|
|
41
|
-
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core';
|
|
41
|
+
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core/index.js';
|
|
42
42
|
let PermissionsService = (() => {
|
|
43
43
|
let _classDecorators = [Injectable({ providedIn: 'root' })];
|
|
44
44
|
let _classDescriptor;
|
package/build/core/cellular.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
// all and resolves a plain GRANTED literal. Upstream's web branch (navigator.connection) is out
|
|
7
7
|
// of scope — this repo has no web target for this package's native-module resolution path.
|
|
8
8
|
import { Platform, PermissionStatus, UnavailabilityError, } from 'expo-modules-core';
|
|
9
|
-
import { expoCellular } from './native-module';
|
|
10
|
-
import { CellularGeneration } from './types';
|
|
9
|
+
import { expoCellular } from './native-module.js';
|
|
10
|
+
import { CellularGeneration } from './types.js';
|
|
11
11
|
const NATIVE_MODULE_NAME = 'expo-cellular';
|
|
12
12
|
const GRANTED_PERMISSION_RESPONSE = {
|
|
13
13
|
status: PermissionStatus.GRANTED,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// React lifecycle wiring over the framework-agnostic core (core/cellular.ts) — fetches the
|
|
2
2
|
// current permission status on mount, then exposes request/get to re-check imperatively.
|
|
3
3
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
|
-
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core';
|
|
4
|
+
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core/index.js';
|
|
5
5
|
export function usePermissions() {
|
|
6
6
|
const isMounted = useRef(true);
|
|
7
7
|
const [status, setStatus] = useState(null);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Vue lifecycle wiring over the framework-agnostic core (core/cellular.ts) — fetches the
|
|
2
2
|
// current permission status on mount, then exposes request/get to re-check imperatively.
|
|
3
3
|
import { onMounted, ref } from '@vue/runtime-core';
|
|
4
|
-
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core';
|
|
4
|
+
import { getPermissionsAsync, requestPermissionsAsync, } from '../../../core/index.js';
|
|
5
5
|
export function usePermissions() {
|
|
6
6
|
const status = ref(null);
|
|
7
7
|
const get = async () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@symbiote-native/cellular",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "expo-cellular wrapped for SymbioteNative — one framework-agnostic core, built once and reachable from the React, Vue, and Angular adapters. Cellular generation (2G/3G/4G/5G), carrier/SIM info on Android (ISO country code, carrier name, MCC/MNC), VoIP-support detection, and the permission get/request pair gating carrier info on Android.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,7 +40,10 @@
|
|
|
40
40
|
"src",
|
|
41
41
|
"build",
|
|
42
42
|
"build-ngc",
|
|
43
|
-
"native-link.json"
|
|
43
|
+
"native-link.json",
|
|
44
|
+
"!src/**/*.test.*",
|
|
45
|
+
"!src/**/*.spec.*",
|
|
46
|
+
"!src/**/*.detox.*"
|
|
44
47
|
],
|
|
45
48
|
"publishConfig": {
|
|
46
49
|
"access": "public"
|
|
@@ -56,7 +59,7 @@
|
|
|
56
59
|
"react": ">=19.0.0",
|
|
57
60
|
"react-native": ">=0.86",
|
|
58
61
|
"vue": ">=3.5.0",
|
|
59
|
-
"@symbiote-native/angular": "0.6.
|
|
62
|
+
"@symbiote-native/angular": "0.6.2",
|
|
60
63
|
"@symbiote-native/react": "0.2.8",
|
|
61
64
|
"@symbiote-native/vue": "0.3.8"
|
|
62
65
|
},
|
|
@@ -92,11 +95,11 @@
|
|
|
92
95
|
"@vue/runtime-core": "^3.5.13",
|
|
93
96
|
"react": "19.2.3",
|
|
94
97
|
"typescript": "~6.0.0",
|
|
95
|
-
"@symbiote-native/
|
|
96
|
-
"@symbiote-native/
|
|
98
|
+
"@symbiote-native/angular": "0.6.2",
|
|
99
|
+
"@symbiote-native/engine": "0.1.7",
|
|
97
100
|
"@symbiote-native/react": "0.2.8",
|
|
98
101
|
"@symbiote-native/test-utils": "0.1.6",
|
|
99
|
-
"@symbiote-native/
|
|
102
|
+
"@symbiote-native/vue": "0.3.8"
|
|
100
103
|
},
|
|
101
104
|
"scripts": {
|
|
102
105
|
"typecheck": "tsc --build",
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
// Co-located Angular-driven test (ADR 0025) for PermissionsService. See
|
|
2
|
-
// packages/battery/src/angular/services/battery-state.service/battery-state.service.test.ts for
|
|
3
|
-
// the shared rationale, adapted here to test the get/request permission pair instead of a
|
|
4
|
-
// listener subscription.
|
|
5
|
-
|
|
6
|
-
import '@angular/compiler';
|
|
7
|
-
import { Component, inject, type Signal } from '@angular/core';
|
|
8
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
9
|
-
import { mount, unmount } from '@symbiote-native/angular';
|
|
10
|
-
import { installFabric } from '@symbiote-native/test-utils';
|
|
11
|
-
import { PermissionsService } from './index';
|
|
12
|
-
|
|
13
|
-
type IPermissionResponse = {
|
|
14
|
-
status: string;
|
|
15
|
-
expires: 'never';
|
|
16
|
-
granted: boolean;
|
|
17
|
-
canAskAgain: boolean;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const GRANTED: IPermissionResponse = {
|
|
21
|
-
status: 'granted',
|
|
22
|
-
expires: 'never',
|
|
23
|
-
granted: true,
|
|
24
|
-
canAskAgain: true,
|
|
25
|
-
};
|
|
26
|
-
const DENIED: IPermissionResponse = {
|
|
27
|
-
status: 'denied',
|
|
28
|
-
expires: 'never',
|
|
29
|
-
granted: false,
|
|
30
|
-
canAskAgain: true,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const getPermissionsAsyncMock = vi.fn(async () => GRANTED);
|
|
34
|
-
const requestPermissionsAsyncMock = vi.fn(async () => GRANTED);
|
|
35
|
-
|
|
36
|
-
vi.mock('../../../core', () => ({
|
|
37
|
-
getPermissionsAsync: () => getPermissionsAsyncMock(),
|
|
38
|
-
requestPermissionsAsync: () => requestPermissionsAsyncMock(),
|
|
39
|
-
}));
|
|
40
|
-
|
|
41
|
-
const ROOT_TAG = 973;
|
|
42
|
-
const fabric = installFabric();
|
|
43
|
-
const tick = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 0));
|
|
44
|
-
|
|
45
|
-
let capturedResult: Signal<IPermissionResponse | null> | undefined;
|
|
46
|
-
let capturedService: PermissionsService | undefined;
|
|
47
|
-
|
|
48
|
-
@Component({
|
|
49
|
-
selector: 'symbiote-permissions-host',
|
|
50
|
-
standalone: true,
|
|
51
|
-
template: '',
|
|
52
|
-
})
|
|
53
|
-
class PermissionsHost {
|
|
54
|
-
private readonly service = inject(PermissionsService);
|
|
55
|
-
readonly status = this.service.connect();
|
|
56
|
-
|
|
57
|
-
constructor() {
|
|
58
|
-
capturedResult = this.status;
|
|
59
|
-
capturedService = this.service;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
beforeEach(() => {
|
|
64
|
-
fabric.reset();
|
|
65
|
-
capturedResult = undefined;
|
|
66
|
-
capturedService = undefined;
|
|
67
|
-
getPermissionsAsyncMock.mockClear();
|
|
68
|
-
requestPermissionsAsyncMock.mockClear();
|
|
69
|
-
getPermissionsAsyncMock.mockResolvedValue(GRANTED);
|
|
70
|
-
requestPermissionsAsyncMock.mockResolvedValue(GRANTED);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
afterEach(() => unmount(ROOT_TAG));
|
|
74
|
-
|
|
75
|
-
describe('PermissionsService.connect', () => {
|
|
76
|
-
it('starts at null before the initial get() resolves', () => {
|
|
77
|
-
mount(ROOT_TAG, PermissionsHost);
|
|
78
|
-
|
|
79
|
-
expect(capturedResult?.()).toBeNull();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('fetches the permission status once connect() is called', async () => {
|
|
83
|
-
mount(ROOT_TAG, PermissionsHost);
|
|
84
|
-
await tick();
|
|
85
|
-
|
|
86
|
-
expect(capturedResult?.()).toEqual(GRANTED);
|
|
87
|
-
expect(getPermissionsAsyncMock).toHaveBeenCalledTimes(1);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('request() re-fetches and updates the signal', async () => {
|
|
91
|
-
mount(ROOT_TAG, PermissionsHost);
|
|
92
|
-
await tick();
|
|
93
|
-
|
|
94
|
-
requestPermissionsAsyncMock.mockResolvedValueOnce(DENIED);
|
|
95
|
-
await capturedService?.request();
|
|
96
|
-
|
|
97
|
-
expect(capturedResult?.()).toEqual(DENIED);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('get() re-fetches and updates the signal', async () => {
|
|
101
|
-
mount(ROOT_TAG, PermissionsHost);
|
|
102
|
-
await tick();
|
|
103
|
-
|
|
104
|
-
getPermissionsAsyncMock.mockResolvedValueOnce(DENIED);
|
|
105
|
-
await capturedService?.get();
|
|
106
|
-
|
|
107
|
-
expect(capturedResult?.()).toEqual(DENIED);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
|
-
const FAKE_NATIVE_CELLULAR = {
|
|
4
|
-
getCellularGenerationAsync: vi.fn(async () => 3),
|
|
5
|
-
allowsVoipAsync: vi.fn(async () => true),
|
|
6
|
-
getIsoCountryCodeAsync: vi.fn(async () => 'us'),
|
|
7
|
-
getCarrierNameAsync: vi.fn(async () => 'Fake Carrier'),
|
|
8
|
-
getMobileCountryCodeAsync: vi.fn(async () => '310'),
|
|
9
|
-
getMobileNetworkCodeAsync: vi.fn(async () => '260'),
|
|
10
|
-
getPermissionsAsync: vi.fn(async () => ({
|
|
11
|
-
status: 'granted',
|
|
12
|
-
expires: 'never',
|
|
13
|
-
granted: true,
|
|
14
|
-
canAskAgain: true,
|
|
15
|
-
})),
|
|
16
|
-
requestPermissionsAsync: vi.fn(async () => ({
|
|
17
|
-
status: 'granted',
|
|
18
|
-
expires: 'never',
|
|
19
|
-
granted: true,
|
|
20
|
-
canAskAgain: true,
|
|
21
|
-
})),
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// The real ExpoCellular native module only exists on device — resolving it via
|
|
25
|
-
// requireNativeModule() at import time would throw in this headless test run, so the
|
|
26
|
-
// module-lookup file is faked in place of expo-modules-core's runtime resolution, same pattern
|
|
27
|
-
// packages/haptics/src/core/haptics.test.ts uses.
|
|
28
|
-
vi.mock('./native-module', () => ({
|
|
29
|
-
expoCellular: FAKE_NATIVE_CELLULAR,
|
|
30
|
-
}));
|
|
31
|
-
|
|
32
|
-
// expo-modules-core's real entry transitively imports 'react-native' for Platform/
|
|
33
|
-
// TurboModuleRegistry, whose Flow-typed source Vitest's Oxc transform can't parse — same fake
|
|
34
|
-
// packages/haptics/src/core/haptics.test.ts uses. PermissionStatus/UnavailabilityError are faked
|
|
35
|
-
// alongside it for the same reason.
|
|
36
|
-
const fakePlatform = { OS: 'android' as 'ios' | 'android' };
|
|
37
|
-
|
|
38
|
-
vi.mock('expo-modules-core', () => ({
|
|
39
|
-
Platform: fakePlatform,
|
|
40
|
-
PermissionStatus: { GRANTED: 'granted', DENIED: 'denied', UNDETERMINED: 'undetermined' },
|
|
41
|
-
UnavailabilityError: class UnavailabilityError extends Error {
|
|
42
|
-
constructor(moduleName: string, propertyName: string) {
|
|
43
|
-
super(`${propertyName} is not available on ${moduleName}`);
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
}));
|
|
47
|
-
|
|
48
|
-
const {
|
|
49
|
-
getCellularGenerationAsync,
|
|
50
|
-
allowsVoipAsync,
|
|
51
|
-
getIsoCountryCodeAsync,
|
|
52
|
-
getCarrierNameAsync,
|
|
53
|
-
getMobileCountryCodeAsync,
|
|
54
|
-
getMobileNetworkCodeAsync,
|
|
55
|
-
getPermissionsAsync,
|
|
56
|
-
requestPermissionsAsync,
|
|
57
|
-
} = await import('./cellular');
|
|
58
|
-
|
|
59
|
-
afterEach(() => {
|
|
60
|
-
fakePlatform.OS = 'android';
|
|
61
|
-
vi.clearAllMocks();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe('getCellularGenerationAsync', () => {
|
|
65
|
-
it('delegates to the native module unconditionally (no platform gate)', async () => {
|
|
66
|
-
await expect(getCellularGenerationAsync()).resolves.toBe(3);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('still delegates on iOS', async () => {
|
|
70
|
-
fakePlatform.OS = 'ios';
|
|
71
|
-
await expect(getCellularGenerationAsync()).resolves.toBe(3);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('throws an UnavailabilityError-shaped error when the native method is absent', async () => {
|
|
75
|
-
const { getCellularGenerationAsync: native } = FAKE_NATIVE_CELLULAR;
|
|
76
|
-
// @ts-expect-error -- simulating a platform where the native module has no such method
|
|
77
|
-
FAKE_NATIVE_CELLULAR.getCellularGenerationAsync = undefined;
|
|
78
|
-
|
|
79
|
-
await expect(getCellularGenerationAsync()).rejects.toThrow(
|
|
80
|
-
'getCellularGenerationAsync is not available on expo-cellular',
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
FAKE_NATIVE_CELLULAR.getCellularGenerationAsync = native;
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('allowsVoipAsync', () => {
|
|
88
|
-
it('resolves null on iOS without calling the native module', async () => {
|
|
89
|
-
fakePlatform.OS = 'ios';
|
|
90
|
-
await expect(allowsVoipAsync()).resolves.toBeNull();
|
|
91
|
-
expect(FAKE_NATIVE_CELLULAR.allowsVoipAsync).not.toHaveBeenCalled();
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('delegates to the native module on Android', async () => {
|
|
95
|
-
await expect(allowsVoipAsync()).resolves.toBe(true);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('throws an UnavailabilityError-shaped error when the native method is absent on Android', async () => {
|
|
99
|
-
const { allowsVoipAsync: native } = FAKE_NATIVE_CELLULAR;
|
|
100
|
-
// @ts-expect-error -- simulating a platform where the native module has no such method
|
|
101
|
-
FAKE_NATIVE_CELLULAR.allowsVoipAsync = undefined;
|
|
102
|
-
|
|
103
|
-
await expect(allowsVoipAsync()).rejects.toThrow(
|
|
104
|
-
'allowsVoipAsync is not available on expo-cellular',
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
FAKE_NATIVE_CELLULAR.allowsVoipAsync = native;
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('getIsoCountryCodeAsync', () => {
|
|
112
|
-
it('resolves null on iOS without calling the native module', async () => {
|
|
113
|
-
fakePlatform.OS = 'ios';
|
|
114
|
-
await expect(getIsoCountryCodeAsync()).resolves.toBeNull();
|
|
115
|
-
expect(FAKE_NATIVE_CELLULAR.getIsoCountryCodeAsync).not.toHaveBeenCalled();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('delegates to the native module on Android', async () => {
|
|
119
|
-
await expect(getIsoCountryCodeAsync()).resolves.toBe('us');
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
describe('getCarrierNameAsync', () => {
|
|
124
|
-
it('resolves null on iOS without calling the native module', async () => {
|
|
125
|
-
fakePlatform.OS = 'ios';
|
|
126
|
-
await expect(getCarrierNameAsync()).resolves.toBeNull();
|
|
127
|
-
expect(FAKE_NATIVE_CELLULAR.getCarrierNameAsync).not.toHaveBeenCalled();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('delegates to the native module on Android', async () => {
|
|
131
|
-
await expect(getCarrierNameAsync()).resolves.toBe('Fake Carrier');
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe('getMobileCountryCodeAsync', () => {
|
|
136
|
-
it('resolves null on iOS without calling the native module', async () => {
|
|
137
|
-
fakePlatform.OS = 'ios';
|
|
138
|
-
await expect(getMobileCountryCodeAsync()).resolves.toBeNull();
|
|
139
|
-
expect(FAKE_NATIVE_CELLULAR.getMobileCountryCodeAsync).not.toHaveBeenCalled();
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('delegates to the native module on Android', async () => {
|
|
143
|
-
await expect(getMobileCountryCodeAsync()).resolves.toBe('310');
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
describe('getMobileNetworkCodeAsync', () => {
|
|
148
|
-
it('resolves null on iOS without calling the native module', async () => {
|
|
149
|
-
fakePlatform.OS = 'ios';
|
|
150
|
-
await expect(getMobileNetworkCodeAsync()).resolves.toBeNull();
|
|
151
|
-
expect(FAKE_NATIVE_CELLULAR.getMobileNetworkCodeAsync).not.toHaveBeenCalled();
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('delegates to the native module on Android', async () => {
|
|
155
|
-
await expect(getMobileNetworkCodeAsync()).resolves.toBe('260');
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe('getPermissionsAsync', () => {
|
|
160
|
-
it('delegates to the native module on Android', async () => {
|
|
161
|
-
await getPermissionsAsync();
|
|
162
|
-
expect(FAKE_NATIVE_CELLULAR.getPermissionsAsync).toHaveBeenCalledTimes(1);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('resolves a plain GRANTED literal on iOS without calling the native module', async () => {
|
|
166
|
-
fakePlatform.OS = 'ios';
|
|
167
|
-
await expect(getPermissionsAsync()).resolves.toEqual({
|
|
168
|
-
status: 'granted',
|
|
169
|
-
expires: 'never',
|
|
170
|
-
granted: true,
|
|
171
|
-
canAskAgain: true,
|
|
172
|
-
});
|
|
173
|
-
expect(FAKE_NATIVE_CELLULAR.getPermissionsAsync).not.toHaveBeenCalled();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
describe('requestPermissionsAsync', () => {
|
|
178
|
-
it('delegates to the native module on Android', async () => {
|
|
179
|
-
await requestPermissionsAsync();
|
|
180
|
-
expect(FAKE_NATIVE_CELLULAR.requestPermissionsAsync).toHaveBeenCalledTimes(1);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('resolves a plain GRANTED literal on iOS without calling the native module', async () => {
|
|
184
|
-
fakePlatform.OS = 'ios';
|
|
185
|
-
await expect(requestPermissionsAsync()).resolves.toEqual({
|
|
186
|
-
status: 'granted',
|
|
187
|
-
expires: 'never',
|
|
188
|
-
granted: true,
|
|
189
|
-
canAskAgain: true,
|
|
190
|
-
});
|
|
191
|
-
expect(FAKE_NATIVE_CELLULAR.requestPermissionsAsync).not.toHaveBeenCalled();
|
|
192
|
-
});
|
|
193
|
-
});
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
// Co-located React-driven test (ADR 0025) for usePermissions. See
|
|
2
|
-
// packages/battery/src/react/hooks/use-battery-state/use-battery-state.test.tsx for the shared
|
|
3
|
-
// rationale (mocks `core`, not expo-modules-core internals), adapted here to test the
|
|
4
|
-
// get/request permission pair instead of a listener subscription.
|
|
5
|
-
|
|
6
|
-
import { createElement, type ReactElement } from 'react';
|
|
7
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
8
|
-
import { mount, unmount, View } from '@symbiote-native/react';
|
|
9
|
-
import { installFabric } from '@symbiote-native/test-utils';
|
|
10
|
-
import { usePermissions } from './index';
|
|
11
|
-
|
|
12
|
-
type IPermissionResponse = {
|
|
13
|
-
status: string;
|
|
14
|
-
expires: 'never';
|
|
15
|
-
granted: boolean;
|
|
16
|
-
canAskAgain: boolean;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const GRANTED: IPermissionResponse = {
|
|
20
|
-
status: 'granted',
|
|
21
|
-
expires: 'never',
|
|
22
|
-
granted: true,
|
|
23
|
-
canAskAgain: true,
|
|
24
|
-
};
|
|
25
|
-
const DENIED: IPermissionResponse = {
|
|
26
|
-
status: 'denied',
|
|
27
|
-
expires: 'never',
|
|
28
|
-
granted: false,
|
|
29
|
-
canAskAgain: true,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const { getPermissionsAsync, requestPermissionsAsync } = vi.hoisted(() => ({
|
|
33
|
-
getPermissionsAsync: vi.fn(async () => GRANTED),
|
|
34
|
-
requestPermissionsAsync: vi.fn(async () => GRANTED),
|
|
35
|
-
}));
|
|
36
|
-
|
|
37
|
-
vi.mock('../../../core', () => ({
|
|
38
|
-
getPermissionsAsync,
|
|
39
|
-
requestPermissionsAsync,
|
|
40
|
-
}));
|
|
41
|
-
|
|
42
|
-
const ROOT_TAG = 953;
|
|
43
|
-
|
|
44
|
-
let latestStatus: IPermissionResponse | null = null;
|
|
45
|
-
let requestFn: (() => Promise<IPermissionResponse>) | undefined;
|
|
46
|
-
let getFn: (() => Promise<IPermissionResponse>) | undefined;
|
|
47
|
-
|
|
48
|
-
function Probe(): ReactElement {
|
|
49
|
-
const [status, request, get] = usePermissions();
|
|
50
|
-
latestStatus = status;
|
|
51
|
-
requestFn = request;
|
|
52
|
-
getFn = get;
|
|
53
|
-
return createElement(View);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const fabric = installFabric();
|
|
57
|
-
|
|
58
|
-
beforeEach(() => {
|
|
59
|
-
fabric.reset();
|
|
60
|
-
latestStatus = null;
|
|
61
|
-
requestFn = undefined;
|
|
62
|
-
getFn = undefined;
|
|
63
|
-
vi.clearAllMocks();
|
|
64
|
-
getPermissionsAsync.mockResolvedValue(GRANTED);
|
|
65
|
-
requestPermissionsAsync.mockResolvedValue(GRANTED);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
afterEach(() => unmount(ROOT_TAG));
|
|
69
|
-
|
|
70
|
-
describe('usePermissions', () => {
|
|
71
|
-
it('starts at null before the initial getPermissionsAsync() resolves', () => {
|
|
72
|
-
mount(ROOT_TAG, createElement(Probe));
|
|
73
|
-
|
|
74
|
-
expect(latestStatus).toBeNull();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('fetches the permission status on mount', async () => {
|
|
78
|
-
mount(ROOT_TAG, createElement(Probe));
|
|
79
|
-
|
|
80
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(GRANTED));
|
|
81
|
-
expect(getPermissionsAsync).toHaveBeenCalledTimes(1);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('requestPermission() re-fetches and updates the status', async () => {
|
|
85
|
-
mount(ROOT_TAG, createElement(Probe));
|
|
86
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(GRANTED));
|
|
87
|
-
|
|
88
|
-
requestPermissionsAsync.mockResolvedValueOnce(DENIED);
|
|
89
|
-
await requestFn?.();
|
|
90
|
-
|
|
91
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(DENIED));
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('getPermission() re-fetches and updates the status', async () => {
|
|
95
|
-
mount(ROOT_TAG, createElement(Probe));
|
|
96
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(GRANTED));
|
|
97
|
-
|
|
98
|
-
getPermissionsAsync.mockResolvedValueOnce(DENIED);
|
|
99
|
-
await getFn?.();
|
|
100
|
-
|
|
101
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(DENIED));
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('does not update state after unmount', async () => {
|
|
105
|
-
mount(ROOT_TAG, createElement(Probe));
|
|
106
|
-
await vi.waitFor(() => expect(latestStatus).toEqual(GRANTED));
|
|
107
|
-
|
|
108
|
-
unmount(ROOT_TAG);
|
|
109
|
-
getPermissionsAsync.mockResolvedValueOnce(DENIED);
|
|
110
|
-
await getFn?.();
|
|
111
|
-
|
|
112
|
-
// state setter is guarded by isMounted, so the captured value from before unmount stands
|
|
113
|
-
expect(latestStatus).toEqual(GRANTED);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
// Co-located Vue-driven test (ADR 0025) for usePermissions. See
|
|
2
|
-
// packages/battery/src/vue/composables/use-battery-state/use-battery-state.test.ts for the
|
|
3
|
-
// shared rationale, adapted here to test the get/request permission pair instead of a listener
|
|
4
|
-
// subscription.
|
|
5
|
-
|
|
6
|
-
import { defineComponent, h } from '@vue/runtime-core';
|
|
7
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
8
|
-
import { mount, unmount } from '@symbiote-native/vue';
|
|
9
|
-
import { installFabric } from '@symbiote-native/test-utils';
|
|
10
|
-
import { usePermissions } from './index';
|
|
11
|
-
|
|
12
|
-
type IPermissionResponse = {
|
|
13
|
-
status: string;
|
|
14
|
-
expires: 'never';
|
|
15
|
-
granted: boolean;
|
|
16
|
-
canAskAgain: boolean;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const GRANTED: IPermissionResponse = {
|
|
20
|
-
status: 'granted',
|
|
21
|
-
expires: 'never',
|
|
22
|
-
granted: true,
|
|
23
|
-
canAskAgain: true,
|
|
24
|
-
};
|
|
25
|
-
const DENIED: IPermissionResponse = {
|
|
26
|
-
status: 'denied',
|
|
27
|
-
expires: 'never',
|
|
28
|
-
granted: false,
|
|
29
|
-
canAskAgain: true,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getPermissionsAsyncMock = vi.fn(async () => GRANTED);
|
|
33
|
-
const requestPermissionsAsyncMock = vi.fn(async () => GRANTED);
|
|
34
|
-
|
|
35
|
-
vi.mock('../../../core', () => ({
|
|
36
|
-
getPermissionsAsync: () => getPermissionsAsyncMock(),
|
|
37
|
-
requestPermissionsAsync: () => requestPermissionsAsyncMock(),
|
|
38
|
-
}));
|
|
39
|
-
|
|
40
|
-
const ROOT_TAG = 9953;
|
|
41
|
-
const fabric = installFabric();
|
|
42
|
-
|
|
43
|
-
beforeEach(() => {
|
|
44
|
-
fabric.reset();
|
|
45
|
-
getPermissionsAsyncMock.mockClear();
|
|
46
|
-
requestPermissionsAsyncMock.mockClear();
|
|
47
|
-
getPermissionsAsyncMock.mockResolvedValue(GRANTED);
|
|
48
|
-
requestPermissionsAsyncMock.mockResolvedValue(GRANTED);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
afterEach(() => unmount(ROOT_TAG));
|
|
52
|
-
|
|
53
|
-
function mountPermissions(): ReturnType<typeof usePermissions> {
|
|
54
|
-
let result: ReturnType<typeof usePermissions> | undefined;
|
|
55
|
-
mount(
|
|
56
|
-
ROOT_TAG,
|
|
57
|
-
defineComponent({
|
|
58
|
-
setup: () => {
|
|
59
|
-
result = usePermissions();
|
|
60
|
-
return () => h('symbiote-text', {}, 'cellular');
|
|
61
|
-
},
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
if (result === undefined) {
|
|
65
|
-
throw new Error('setup() did not run');
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
describe('usePermissions (Vue)', () => {
|
|
71
|
-
it('starts at null before the initial get() resolves', () => {
|
|
72
|
-
const { status } = mountPermissions();
|
|
73
|
-
|
|
74
|
-
expect(status.value).toBeNull();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('fetches the permission status on mount', async () => {
|
|
78
|
-
const { status } = mountPermissions();
|
|
79
|
-
|
|
80
|
-
await vi.waitFor(() => expect(status.value).toEqual(GRANTED));
|
|
81
|
-
expect(getPermissionsAsyncMock).toHaveBeenCalledTimes(1);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('request() re-fetches and updates the ref', async () => {
|
|
85
|
-
const { status, request } = mountPermissions();
|
|
86
|
-
await vi.waitFor(() => expect(status.value).toEqual(GRANTED));
|
|
87
|
-
|
|
88
|
-
requestPermissionsAsyncMock.mockResolvedValueOnce(DENIED);
|
|
89
|
-
await request();
|
|
90
|
-
|
|
91
|
-
expect(status.value).toEqual(DENIED);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('get() re-fetches and updates the ref', async () => {
|
|
95
|
-
const { status, get } = mountPermissions();
|
|
96
|
-
await vi.waitFor(() => expect(status.value).toEqual(GRANTED));
|
|
97
|
-
|
|
98
|
-
getPermissionsAsyncMock.mockResolvedValueOnce(DENIED);
|
|
99
|
-
await get();
|
|
100
|
-
|
|
101
|
-
expect(status.value).toEqual(DENIED);
|
|
102
|
-
});
|
|
103
|
-
});
|