@applicaster/zapp-react-native-utils 14.0.1 → 14.0.2-rc.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "14.0.
|
|
3
|
+
"version": "14.0.2-rc.0",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "14.0.
|
|
30
|
+
"@applicaster/applicaster-types": "14.0.2-rc.0",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import { WrappedWithProviders } from "@applicaster/zapp-react-native-utils/testUtils";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getInflatedDataSourceUrl,
|
|
7
|
+
getSearchContext,
|
|
8
|
+
useGetUrlInflater,
|
|
9
|
+
} from "../useInflatedUrl";
|
|
10
|
+
|
|
11
|
+
import { reactHooksLogger } from "../../logger";
|
|
12
|
+
|
|
13
|
+
jest.mock("../../navigation", () => ({
|
|
14
|
+
useRoute: () => ({ pathname: "/mock/path" }),
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
// mock contexts hooks
|
|
18
|
+
jest.mock("@applicaster/zapp-react-native-ui-components/Contexts", () => ({
|
|
19
|
+
ZappPipesEntryContext: {
|
|
20
|
+
useZappPipesContext: () => [
|
|
21
|
+
{ id: "entry-1", extensions: { showId: "A123" } },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
ZappPipesSearchContext: {
|
|
25
|
+
useZappPipesContext: () => ["user%20query"],
|
|
26
|
+
},
|
|
27
|
+
ZappPipesScreenContext: {
|
|
28
|
+
useZappPipesContext: () => [{ id: "screen-1" }],
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
|
|
1
32
|
jest.mock("../../logger", () => ({
|
|
2
33
|
reactHooksLogger: {
|
|
3
34
|
warning: jest.fn(),
|
|
@@ -5,13 +36,6 @@ jest.mock("../../logger", () => ({
|
|
|
5
36
|
},
|
|
6
37
|
}));
|
|
7
38
|
|
|
8
|
-
const {
|
|
9
|
-
getInflatedDataSourceUrl,
|
|
10
|
-
getSearchContext,
|
|
11
|
-
} = require("../useInflatedUrl");
|
|
12
|
-
|
|
13
|
-
const { reactHooksLogger } = require("../../logger");
|
|
14
|
-
|
|
15
39
|
let mockStore;
|
|
16
40
|
|
|
17
41
|
describe("getInflatedDataSourceUrl", () => {
|
|
@@ -188,3 +212,34 @@ describe("getSearchContext", () => {
|
|
|
188
212
|
expect(result).toHaveProperty(mapping.queryParam.property, searchContext);
|
|
189
213
|
});
|
|
190
214
|
});
|
|
215
|
+
|
|
216
|
+
describe("useGetUrlInflater", () => {
|
|
217
|
+
const { renderHook } = require("@testing-library/react-hooks");
|
|
218
|
+
|
|
219
|
+
it("returns original url when mapping is not provided", () => {
|
|
220
|
+
const { result } = renderHook(() => useGetUrlInflater(), {
|
|
221
|
+
wrapper: WrappedWithProviders,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const src = "https://foo.com/feed";
|
|
225
|
+
expect(result.current(src)).toBe(src);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("inflates url using entry/search/screen contexts when mapping provided", () => {
|
|
229
|
+
const { result } = renderHook(() => useGetUrlInflater(), {
|
|
230
|
+
wrapper: ({ children }) => (
|
|
231
|
+
<WrappedWithProviders>{children}</WrappedWithProviders>
|
|
232
|
+
),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const source = "https://foo.com/shows/{{showId}}?q={{q}}";
|
|
236
|
+
|
|
237
|
+
const mapping = {
|
|
238
|
+
showId: { source: "entry", property: "extensions.showId" },
|
|
239
|
+
q: { source: "search", property: "q" },
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const url = result.current(source, mapping);
|
|
243
|
+
expect(url).toBe("https://foo.com/shows/A123?q=user%20query");
|
|
244
|
+
});
|
|
245
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
1
|
+
import { useCallback, useMemo, useRef } from "react";
|
|
2
2
|
import * as R from "ramda";
|
|
3
3
|
|
|
4
4
|
import {
|
|
@@ -169,7 +169,7 @@ const encodeIfNeeded: (string) => string = R.tryCatch(
|
|
|
169
169
|
|
|
170
170
|
export function getSearchContext(
|
|
171
171
|
searchContext: string,
|
|
172
|
-
mapping: ZappTypeMapping
|
|
172
|
+
mapping: Nullable<ZappTypeMapping>
|
|
173
173
|
) {
|
|
174
174
|
if (!mapping) {
|
|
175
175
|
return {};
|
|
@@ -183,31 +183,57 @@ export function getSearchContext(
|
|
|
183
183
|
return { [property]: encodeIfNeeded(searchContext) };
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
mapping?: ZappTypeMapping;
|
|
192
|
-
}) {
|
|
186
|
+
/**
|
|
187
|
+
* Hook returns a function that replace placeholders with context values
|
|
188
|
+
* @returns function that inflates urls based on contexts
|
|
189
|
+
*/
|
|
190
|
+
export const useGetUrlInflater = () => {
|
|
193
191
|
const { pathname } = useRoute();
|
|
194
192
|
|
|
195
193
|
const [entryContext] = ZappPipesEntryContext.useZappPipesContext(pathname);
|
|
196
194
|
const [searchContext] = ZappPipesSearchContext.useZappPipesContext();
|
|
197
195
|
const [screenContext] = ZappPipesScreenContext.useZappPipesContext();
|
|
198
196
|
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
const entryContextRef = useRef(entryContext);
|
|
198
|
+
entryContextRef.current = entryContext;
|
|
199
|
+
|
|
200
|
+
const screenContextRef = useRef(screenContext);
|
|
201
|
+
screenContextRef.current = screenContext;
|
|
202
|
+
|
|
203
|
+
const searchContextRef = useRef(searchContext);
|
|
204
|
+
searchContextRef.current = searchContext;
|
|
205
|
+
|
|
206
|
+
return useCallback(
|
|
207
|
+
(
|
|
208
|
+
feedUrl: Nullable<string>,
|
|
209
|
+
mapping?: Nullable<ZappTypeMapping>
|
|
210
|
+
): Nullable<string> => {
|
|
211
|
+
return getInflatedDataSourceUrl({
|
|
202
212
|
source: feedUrl,
|
|
203
213
|
contexts: {
|
|
204
|
-
entry:
|
|
205
|
-
screen:
|
|
206
|
-
search: getSearchContext(
|
|
214
|
+
entry: entryContextRef.current,
|
|
215
|
+
screen: screenContextRef.current,
|
|
216
|
+
search: getSearchContext(searchContextRef.current, mapping),
|
|
207
217
|
},
|
|
208
218
|
mapping,
|
|
209
|
-
})
|
|
210
|
-
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
[]
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export function useInflatedUrl({
|
|
226
|
+
feedUrl,
|
|
227
|
+
mapping,
|
|
228
|
+
}: {
|
|
229
|
+
feedUrl?: string;
|
|
230
|
+
mapping?: ZappTypeMapping;
|
|
231
|
+
}) {
|
|
232
|
+
const urlInflater = useGetUrlInflater();
|
|
233
|
+
|
|
234
|
+
const url = useMemo(
|
|
235
|
+
() => urlInflater(feedUrl, mapping),
|
|
236
|
+
[urlInflater, feedUrl, mapping]
|
|
211
237
|
);
|
|
212
238
|
|
|
213
239
|
return url;
|