@mapsindoors/react-native-maps-indoors-mapbox 2.9.0 → 2.9.1

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.
@@ -1,107 +0,0 @@
1
- import MPSolution from '../MPSolution';
2
-
3
- /**
4
- * Unit tests for MPSolution's language handling.
5
- *
6
- * hasLanguage used to be an exact, case-sensitive Array.includes, which put it out of step with
7
- * both native SDKs: they canonicalize a tag before matching it, so hasLanguage('zh-CN') answered
8
- * false for a solution where setLanguage('zh-CN') then succeeded. Two APIs on the same object
9
- * disagreeing about the same solution is worse than either answer alone, and it disagreed in
10
- * exactly the Chinese case the language work exists for.
11
- *
12
- * The tables use the language sets of the solutions this repo actually tests against, so they
13
- * double as a record of what those solutions publish.
14
- */
15
- const solution = (availableLanguages: string[], defaultLanguage = 'en') =>
16
- MPSolution.create({
17
- id: 'solution',
18
- name: 'Solution',
19
- solutionConfig: { id: 'config' },
20
- defaultLanguage,
21
- availableLanguages,
22
- modules: [],
23
- } as unknown as Parameters<typeof MPSolution.create>[0]);
24
-
25
- /** 2fb83f9461634b29b6c0fcb6, "MapsIndoors - Office (Chinese)" - the Chinese test solution. */
26
- const CHINESE = ['da', 'en', 'zh-Hans', 'zh-Hant'];
27
- /** The `mapspeople` solution the on-device suites use. */
28
- const MAPSPEOPLE = ['de', 'en', 'da'];
29
- /** The `lapalmeraie` solution both example apps load. */
30
- const LAPALMERAIE = ['pt', 'fr', 'en'];
31
-
32
- describe('MPSolution.create', () => {
33
- it('round-trips the language fields', () => {
34
- const mapspeople = solution(MAPSPEOPLE, 'da');
35
-
36
- expect(mapspeople.availableLanguages).toEqual(MAPSPEOPLE);
37
- expect(mapspeople.defaultLanguage).toBe('da');
38
- });
39
- });
40
-
41
- describe('MPSolution.hasLanguage', () => {
42
- it.each([
43
- ['en', MAPSPEOPLE],
44
- ['de', MAPSPEOPLE],
45
- ['fr', LAPALMERAIE],
46
- ['zh-Hans', CHINESE],
47
- ['zh-Hant', CHINESE],
48
- ])('accepts %s, which the solution advertises verbatim', (language, available) => {
49
- expect(solution(available).hasLanguage(language)).toBe(true);
50
- });
51
-
52
- it.each([
53
- ['zh-CN', CHINESE],
54
- ['zh-SG', CHINESE],
55
- ['zh-TW', CHINESE],
56
- ['zh-HK', CHINESE],
57
- ['zh_Hans', CHINESE],
58
- ['ZH-HANT', CHINESE],
59
- ['zh-Hant-TW', CHINESE],
60
- ])('accepts %s, which resolves onto an advertised Chinese tag', (language, available) => {
61
- // Every one of these was false before the tags were canonicalized, while
62
- // MapsIndoors.setLanguage accepted them natively.
63
- expect(solution(available).hasLanguage(language)).toBe(true);
64
- });
65
-
66
- it('accepts a regional tag whose base language the solution has', () => {
67
- expect(solution(MAPSPEOPLE).hasLanguage('en-US')).toBe(true);
68
- expect(solution(LAPALMERAIE).hasLanguage('pt-BR')).toBe(true);
69
- });
70
-
71
- it('refuses bare zh against a solution that only publishes script variants', () => {
72
- // Ambiguous: there is no way to tell whether the caller wants Simplified or Traditional, and
73
- // Android's setLanguage refuses it too.
74
- expect(solution(CHINESE).hasLanguage('zh')).toBe(false);
75
- });
76
-
77
- it.each([
78
- ['zh-Hans', MAPSPEOPLE],
79
- ['fr', MAPSPEOPLE],
80
- ['de', LAPALMERAIE],
81
- ])('refuses %s, which the solution does not have in any form', (language, available) => {
82
- expect(solution(available).hasLanguage(language)).toBe(false);
83
- });
84
-
85
- it.each([undefined, '', ' '])('refuses the empty input %p', (language) => {
86
- expect(solution(CHINESE).hasLanguage(language)).toBe(false);
87
- });
88
- });
89
-
90
- describe('MPSolution.resolveLanguage', () => {
91
- it.each([
92
- ['zh-CN', 'zh-Hans'],
93
- ['zh-TW', 'zh-Hant'],
94
- ['zh-Hant-TW', 'zh-Hant'],
95
- ['ZH-HANS', 'zh-Hans'],
96
- ['en-US', 'en'],
97
- ])('resolves %s to %s, spelled the way the solution publishes it', (language, expected) => {
98
- // This is the answer hasLanguage cannot give: what to actually pass to setLanguage.
99
- expect(solution(CHINESE).resolveLanguage(language)).toBe(expected);
100
- });
101
-
102
- it('returns undefined when nothing matches', () => {
103
- expect(solution(CHINESE).resolveLanguage('fr')).toBeUndefined();
104
- expect(solution(CHINESE).resolveLanguage('zh')).toBeUndefined();
105
- expect(solution(CHINESE).resolveLanguage()).toBeUndefined();
106
- });
107
- });
@@ -1,180 +0,0 @@
1
- import { NativeModules, resetNativeModules } from 'react-native';
2
- import MapsIndoors from '../MapsIndoors';
3
- import MPError from '../MPError';
4
-
5
- /**
6
- * Unit tests for the language methods' contract at the JavaScript boundary.
7
- *
8
- * These are bridge wrappers, so there is nothing here to compute - which is exactly the point.
9
- * The tag has to reach native byte-for-byte, because both native SDKs canonicalize it themselves
10
- * and any rewriting on this side would be a third, invisible set of rules. And the promise has to
11
- * settle the way the declared types say: setLanguage is typed Promise<boolean> but resolved null on
12
- * iOS for as long as it has existed, and the getters map a native rejection to MPError only if the
13
- * native side rejects with JSON.
14
- *
15
- * Behaviour that depends on a real solution - whether a given tag is accepted, what getLanguage
16
- * reads back - belongs in the on-device suites in mapsindoors-sdk-react-native.
17
- */
18
- const nativeModule = NativeModules.MapsIndoorsModule;
19
-
20
- beforeEach(resetNativeModules);
21
-
22
- /** The shape both native modules reject with: a JSON-encoded MPError. */
23
- const nativeRejection = (code: number, message: string) =>
24
- new Error(JSON.stringify({ code, message }));
25
-
26
- describe('MapsIndoors.setLanguage', () => {
27
- it.each(['zh-Hans', 'zh-Hant', 'zh-CN', 'zh_Hans', 'ZH-HANT', 'en', 'pt-BR'])(
28
- 'forwards %s to native unchanged',
29
- async (language) => {
30
- // No lower-casing, no splitting, no normalizing. The native SDKs own that, and doing any of
31
- // it here would silently disagree with them.
32
- nativeModule.setLanguage.mockResolvedValue(true);
33
-
34
- await MapsIndoors.setLanguage(language);
35
-
36
- expect(nativeModule.setLanguage).toHaveBeenCalledWith(language);
37
- },
38
- );
39
-
40
- it.each([true, false])('resolves the native result, %p', async (result) => {
41
- // iOS used to resolve null here regardless, despite the declared Promise<boolean>.
42
- nativeModule.setLanguage.mockResolvedValue(result);
43
-
44
- await expect(MapsIndoors.setLanguage('zh-Hans')).resolves.toBe(result);
45
- });
46
-
47
- it.each(['', ' '])('resolves false for %p without calling native', async (language) => {
48
- // Android rejects this in its own preconditions, but iOS accepts it and stores an empty
49
- // language, which corrupts the `lr=` parameter on every subsequent request.
50
- await expect(MapsIndoors.setLanguage(language)).resolves.toBe(false);
51
-
52
- expect(nativeModule.setLanguage).not.toHaveBeenCalled();
53
- });
54
- });
55
-
56
- describe('MapsIndoors.getAvailableLanguages', () => {
57
- it('passes the solution languages through', async () => {
58
- nativeModule.getAvailableLanguages.mockResolvedValue(['da', 'en', 'zh-Hans', 'zh-Hant']);
59
-
60
- await expect(MapsIndoors.getAvailableLanguages()).resolves.toEqual([
61
- 'da',
62
- 'en',
63
- 'zh-Hans',
64
- 'zh-Hant',
65
- ]);
66
- });
67
-
68
- it('turns a native rejection into an MPError', async () => {
69
- // Only works because both native modules reject with JSON - MPError.parse runs JSON.parse on
70
- // the message, so a raw string reject surfaces as a SyntaxError instead.
71
- nativeModule.getAvailableLanguages.mockRejectedValue(
72
- nativeRejection(20, 'No languages are available, try waiting until MapsIndoors is Ready'),
73
- );
74
-
75
- await expect(MapsIndoors.getAvailableLanguages()).rejects.toBeInstanceOf(MPError);
76
- await expect(MapsIndoors.getAvailableLanguages()).rejects.toMatchObject({ code: 20 });
77
- });
78
- });
79
-
80
- describe('MapsIndoors.getDefaultLanguage', () => {
81
- it('passes the default language through', async () => {
82
- nativeModule.getDefaultLanguage.mockResolvedValue('en');
83
-
84
- await expect(MapsIndoors.getDefaultLanguage()).resolves.toBe('en');
85
- });
86
-
87
- it('turns a native rejection into an MPError', async () => {
88
- // Android used to resolve null here against a declared Promise<string>; it now rejects, the
89
- // way iOS already did.
90
- nativeModule.getDefaultLanguage.mockRejectedValue(
91
- nativeRejection(20, 'No default language is available, try waiting until MapsIndoors is Ready'),
92
- );
93
-
94
- await expect(MapsIndoors.getDefaultLanguage()).rejects.toBeInstanceOf(MPError);
95
- });
96
- });
97
-
98
- describe('MapsIndoors.getLanguage', () => {
99
- it('passes the current language through unwrapped', async () => {
100
- // Deliberately has no MPError mapping: neither native has a reject path here - both return a
101
- // non-null string with their own fallback chain - so a catch would be dead code.
102
- nativeModule.getLanguage.mockResolvedValue('zh-Hant');
103
-
104
- await expect(MapsIndoors.getLanguage()).resolves.toBe('zh-Hant');
105
- });
106
- });
107
-
108
- describe('MapsIndoors.getSolution', () => {
109
- // Not a language method, but hasLanguage / resolveLanguage are only reachable through the
110
- // solution this returns, and Android used to resolve null here whenever the native solution was
111
- // momentarily unavailable - which is exactly what a language change causes, because it tears
112
- // down the data providers. JSON.parse(undefined) then threw "Unexpected character: u", and
113
- // JSON.parse(null) silently produced a solution whose availableLanguages was undefined, so
114
- // hasLanguage answered false for every tag.
115
- it('turns a missing solution into an MPError instead of a parse crash', async () => {
116
- nativeModule.getSolution.mockRejectedValue(
117
- nativeRejection(20, 'No solution is available, try waiting until MapsIndoors is Ready'),
118
- );
119
-
120
- await expect(MapsIndoors.getSolution()).rejects.toBeInstanceOf(MPError);
121
- });
122
-
123
- it('parses a solution and exposes its languages', async () => {
124
- nativeModule.getSolution.mockResolvedValue(
125
- JSON.stringify({
126
- id: 'solution',
127
- name: 'Solution',
128
- solutionConfig: { id: 'config' },
129
- defaultLanguage: 'en',
130
- availableLanguages: ['da', 'en', 'zh-Hans', 'zh-Hant'],
131
- modules: [],
132
- }),
133
- );
134
-
135
- const solution = await MapsIndoors.getSolution();
136
-
137
- expect(solution.availableLanguages).toEqual(['da', 'en', 'zh-Hans', 'zh-Hant']);
138
- expect(solution.hasLanguage('zh-CN')).toBe(true);
139
- });
140
- });
141
-
142
- describe('MapsIndoors.getLocations', () => {
143
- // Also not a language method, but a language change is what exposed it: Android rejects
144
- // getLocations while the locations reload, and the catch body here was `{Promise.reject(...)}` -
145
- // a statement block with no return, so the arrow resolved undefined and swallowed the rejection.
146
- // JSON.parse(undefined) then threw "Unexpected character: u", masking the real MPError.
147
- it('propagates a native rejection instead of swallowing it into a parse error', async () => {
148
- nativeModule.getLocations.mockRejectedValue(
149
- nativeRejection(20, 'Cannot fetch locations, try waiting until MapsIndoors is Ready'),
150
- );
151
-
152
- await expect(MapsIndoors.getLocations()).rejects.toBeInstanceOf(MPError);
153
- });
154
- });
155
-
156
- describe('MPError.parse', () => {
157
- // Every .catch(MPError.parse) in the SDK depends on this, and it used to assume the message was
158
- // always the native modules' JSON payload. A rejection from the bridge or from JavaScript has a
159
- // plain-text message, and parsing that threw a SyntaxError which replaced the real message with
160
- // "JSON Parse error: Unexpected character: ...".
161
- it('reads a native JSON payload', () => {
162
- const error = MPError.parse(new Error(JSON.stringify({ code: 10, message: 'No network' })));
163
-
164
- expect(error.code).toBe(10);
165
- expect(error.message).toBe('No network');
166
- });
167
-
168
- it.each([
169
- 'Exception in HostFunction: something went wrong',
170
- '',
171
- 'null',
172
- '42',
173
- ])('wraps the non-JSON message %p instead of throwing', (message) => {
174
- const error = MPError.parse(new Error(message));
175
-
176
- expect(error).toBeInstanceOf(MPError);
177
- expect(error.code).toBe(MPError.unknownError);
178
- expect(error.message).toBe(message);
179
- });
180
- });