@squiz/resource-browser 2.1.8-rc.0 → 2.1.9-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/CHANGELOG.md
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
5
|
|
6
|
+
## [2.1.9-rc.0](https://gitlab.squiz.net/dxp/dxp-shared-ui/resource-browser/compare/@squiz/resource-browser@2.1.8-rc.0...@squiz/resource-browser@2.1.9-rc.0) (2024-06-03)
|
7
|
+
|
8
|
+
**Note:** Version bump only for package @squiz/resource-browser
|
9
|
+
|
6
10
|
## 2.1.8-rc.0 (2024-05-30)
|
7
11
|
|
8
12
|
**Note:** Version bump only for package @squiz/resource-browser
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { ResourceBrowserSource, ResourceBrowserPlugin } from '../types';
|
2
|
-
type UseSourcesProps = {
|
2
|
+
export type UseSourcesProps = {
|
3
3
|
onRequestSources: () => Promise<ResourceBrowserSource[]>;
|
4
4
|
plugins: Array<ResourceBrowserPlugin>;
|
5
5
|
};
|
@@ -12,4 +12,3 @@ export declare const useSources: ({ onRequestSources, plugins }: UseSourcesProps
|
|
12
12
|
isLoading: boolean;
|
13
13
|
reload: () => void;
|
14
14
|
};
|
15
|
-
export {};
|
package/lib/Hooks/useSources.js
CHANGED
@@ -6,6 +6,7 @@ const generic_browser_lib_1 = require("@squiz/generic-browser-lib");
|
|
6
6
|
* Loads and caches the source list when a component using the hook is mounted.
|
7
7
|
*/
|
8
8
|
const useSources = ({ onRequestSources, plugins }) => {
|
9
|
+
const pluginsKey = plugins.reduce((acc, plugin) => acc + plugin.type, '');
|
9
10
|
return (0, generic_browser_lib_1.useAsync)({
|
10
11
|
callback: () => {
|
11
12
|
return new Promise((resolve, reject) => {
|
@@ -28,6 +29,6 @@ const useSources = ({ onRequestSources, plugins }) => {
|
|
28
29
|
});
|
29
30
|
},
|
30
31
|
defaultValue: [],
|
31
|
-
}, [
|
32
|
+
}, [onRequestSources, pluginsKey]);
|
32
33
|
};
|
33
34
|
exports.useSources = useSources;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@squiz/resource-browser",
|
3
|
-
"version": "2.1.
|
3
|
+
"version": "2.1.9-rc.0",
|
4
4
|
"main": "lib/index.js",
|
5
5
|
"types": "lib/index.d.ts",
|
6
6
|
"private": false,
|
@@ -81,5 +81,5 @@
|
|
81
81
|
"volta": {
|
82
82
|
"node": "18.18.0"
|
83
83
|
},
|
84
|
-
"gitHead": "
|
84
|
+
"gitHead": "9ea5dc75a03930aaedf856fe5fb09f67a5a23364"
|
85
85
|
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { renderHook, waitFor } from '@testing-library/react';
|
2
2
|
import { mockSource, mockPlugin } from '../__mocks__/MockModels';
|
3
|
-
import { useSources } from './useSources';
|
3
|
+
import { useSources, UseSourcesProps } from './useSources';
|
4
4
|
|
5
5
|
describe('useSources', () => {
|
6
6
|
it('Should trigger and load the sources', async () => {
|
@@ -35,4 +35,33 @@ describe('useSources', () => {
|
|
35
35
|
expect(result.current.data).toEqual([sources[0]]);
|
36
36
|
expect(result.current.data.length).toEqual(1);
|
37
37
|
});
|
38
|
+
|
39
|
+
it('Should calculate the async key from the plugins type so it wont always change on re-renders', async () => {
|
40
|
+
const sources = [mockSource()];
|
41
|
+
const plugins = [mockPlugin()];
|
42
|
+
const onRequestSources = jest.fn().mockResolvedValue(sources);
|
43
|
+
const { rerender, result } = renderHook((props: UseSourcesProps) =>
|
44
|
+
useSources({ onRequestSources: props?.onRequestSources || onRequestSources, plugins: props?.plugins || plugins }),
|
45
|
+
);
|
46
|
+
|
47
|
+
expect(result.current.isLoading).toBe(true);
|
48
|
+
expect(result.current.error).toBe(null);
|
49
|
+
expect(result.current.data).toEqual([]);
|
50
|
+
|
51
|
+
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
52
|
+
expect(result.current.isLoading).toBe(false);
|
53
|
+
expect(result.current.data).toEqual(sources);
|
54
|
+
|
55
|
+
// Wont go back to loading data (plugin keys havent changed)
|
56
|
+
rerender({ onRequestSources, plugins: [mockPlugin()] });
|
57
|
+
expect(result.current.isLoading).toBe(false);
|
58
|
+
|
59
|
+
// Will go back to loading as change in keys
|
60
|
+
rerender({ onRequestSources, plugins: [mockPlugin(), mockPlugin({ type: 'matrix' })] });
|
61
|
+
expect(result.current.isLoading).toBe(true);
|
62
|
+
|
63
|
+
// Will go back to loading as change in keys (order change will cause reload)
|
64
|
+
rerender({ onRequestSources, plugins: [mockPlugin({ type: 'matrix' }), mockPlugin()] });
|
65
|
+
expect(result.current.isLoading).toBe(true);
|
66
|
+
});
|
38
67
|
});
|
package/src/Hooks/useSources.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ResourceBrowserSource, ResourceBrowserPlugin } from '../types';
|
2
2
|
import { useAsync } from '@squiz/generic-browser-lib';
|
3
3
|
|
4
|
-
type UseSourcesProps = {
|
4
|
+
export type UseSourcesProps = {
|
5
5
|
onRequestSources: () => Promise<ResourceBrowserSource[]>;
|
6
6
|
plugins: Array<ResourceBrowserPlugin>;
|
7
7
|
};
|
@@ -10,6 +10,7 @@ type UseSourcesProps = {
|
|
10
10
|
* Loads and caches the source list when a component using the hook is mounted.
|
11
11
|
*/
|
12
12
|
export const useSources = ({ onRequestSources, plugins }: UseSourcesProps) => {
|
13
|
+
const pluginsKey = plugins.reduce((acc, plugin) => acc + plugin.type, '');
|
13
14
|
return useAsync(
|
14
15
|
{
|
15
16
|
callback: () => {
|
@@ -36,6 +37,6 @@ export const useSources = ({ onRequestSources, plugins }: UseSourcesProps) => {
|
|
36
37
|
},
|
37
38
|
defaultValue: [] as ResourceBrowserSource[],
|
38
39
|
},
|
39
|
-
[
|
40
|
+
[onRequestSources, pluginsKey],
|
40
41
|
);
|
41
42
|
};
|