@squiz/resource-browser 3.1.0-rc.3 → 3.1.0-rc.4

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
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.1.0-rc.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 6503eba: clean selected mode on modal close
8
+
3
9
  ## 3.1.0-rc.3
4
10
 
5
11
  ### Patch Changes
@@ -13,7 +13,7 @@ const SelectedState = ({ resource, showThumbnail, icon, label, description, isDi
13
13
  isOpen: isModalOpen,
14
14
  onOpenChange: onModalStateChange,
15
15
  };
16
- const replaceAsset = (react_1.default.createElement(resource_browser_ui_lib_1.ModalTrigger, { overlayTriggerState: isModalOpen && onModalStateChange ? modalController : undefined, showLabel: false, label: "Replace selection", containerClasses: "text-gray-500 hover:text-gray-800 focus:text-gray-800 disabled:text-gray-500 disabled:cursor-not-allowed", icon: react_1.default.createElement(CircledLoopIcon_1.CircledLoopIcon, { "aria-hidden": true, className: "m-1" }), isDisabled: isDisabled, scope: "squiz-rb-scope" }, resourcePickerContainer));
16
+ const replaceAsset = (react_1.default.createElement(resource_browser_ui_lib_1.ModalTrigger, { overlayTriggerState: modalController, showLabel: false, label: "Replace selection", containerClasses: "text-gray-500 hover:text-gray-800 focus:text-gray-800 disabled:text-gray-500 disabled:cursor-not-allowed", icon: react_1.default.createElement(CircledLoopIcon_1.CircledLoopIcon, { "aria-hidden": true, className: "m-1" }), isDisabled: isDisabled, scope: "squiz-rb-scope" }, resourcePickerContainer));
17
17
  return (react_1.default.createElement(react_1.default.Fragment, null,
18
18
  showThumbnail && resource.squizImage ? (react_1.default.createElement("div", { className: "checkered-bg w-[56px] h-[56px] overflow-hidden flex justify-center items-center rounded" },
19
19
  react_1.default.createElement("img", { src: resource.squizImage.imageVariations.original.url || resource.url, className: "w-full h-full object-cover object-center", alt: resource.squizImage.name || resource.name }))) : (react_1.default.createElement("div", { className: "w-4 h-4 mt-1 flex self-start overflow-hidden" }, icon)),
package/lib/index.js CHANGED
@@ -102,6 +102,9 @@ const ResourceBrowser = (props) => {
102
102
  setSource(null);
103
103
  setMode(null);
104
104
  }
105
+ else if (!isModalOpen) {
106
+ setMode(null);
107
+ }
105
108
  }, [sources, isModalOpen]);
106
109
  // Reset function to allow user manual reload if sources fail in inline usage
107
110
  const handleReset = (0, react_1.useCallback)(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/resource-browser",
3
- "version": "3.1.0-rc.3",
3
+ "version": "3.1.0-rc.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "private": false,
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useState } from 'react';
2
2
  import { render, act, fireEvent, screen, waitFor } from '@testing-library/react';
3
3
  import { ResourceBrowserInput, ResourceBrowserInputProps } from './ResourceBrowserInput';
4
4
  import { mockSource, mockResource } from '../__mocks__/MockModels';
@@ -18,7 +18,7 @@ describe('Resource browser input', () => {
18
18
  const mockSetSource = jest.fn();
19
19
  const mockOnModalStateChange = jest.fn();
20
20
  const renderComponent = (props: Partial<ResourceBrowserInputProps> = {}) => {
21
- render(
21
+ return render(
22
22
  <ResourceBrowserInput
23
23
  modalTitle="Asset picker"
24
24
  value={null}
@@ -158,11 +158,16 @@ describe('Resource browser input', () => {
158
158
  },
159
159
  };
160
160
 
161
- renderComponent({ value: { sourceId: source.id, resourceId: '100' }, plugin: plugin as ResourceBrowserPlugin });
161
+ renderComponent({
162
+ value: { sourceId: source.id, resourceId: '100' },
163
+ plugin: plugin as ResourceBrowserPlugin,
164
+ });
162
165
 
163
166
  await waitFor(() => expect(screen.queryByLabelText('Loading selection')).not.toBeInTheDocument());
164
167
  await act(() => fireEvent.click(screen.getByRole('button', { name: 'Replace selection' })));
165
- expect(screen.getByRole('button', { name: 'Close Asset picker dialog' })).toBeInTheDocument();
168
+
169
+ // Modal index state change handler was instructed to open
170
+ expect(mockOnModalStateChange).toHaveBeenCalledWith(true);
166
171
  });
167
172
 
168
173
  it.each([
@@ -36,7 +36,7 @@ export const SelectedState = ({
36
36
  };
37
37
  const replaceAsset = (
38
38
  <ModalTrigger
39
- overlayTriggerState={isModalOpen && onModalStateChange ? modalController : undefined}
39
+ overlayTriggerState={modalController}
40
40
  showLabel={false}
41
41
  label="Replace selection"
42
42
  containerClasses="text-gray-500 hover:text-gray-800 focus:text-gray-800 disabled:text-gray-500 disabled:cursor-not-allowed"
@@ -8,6 +8,20 @@ import { ResourceBrowserPlugin, ResourceBrowserSource, ResourceBrowserSourceWith
8
8
  import * as RBI from './ResourceBrowserInput/ResourceBrowserInput';
9
9
  jest.spyOn(RBI, 'ResourceBrowserInput');
10
10
 
11
+ var useSourceReloadMock = jest.fn();
12
+ jest.mock('./Hooks/useSources', () => {
13
+ const actual = jest.requireActual('./Hooks/useSources');
14
+ return {
15
+ useSources: jest.fn((...args) => {
16
+ const actualResult = actual.useSources(...args);
17
+ return {
18
+ ...actualResult,
19
+ reload: useSourceReloadMock,
20
+ };
21
+ }),
22
+ };
23
+ });
24
+
11
25
  describe('Resource browser input', () => {
12
26
  const mockChange = jest.fn();
13
27
  const mockOnClear = jest.fn();
@@ -366,7 +380,7 @@ describe('Resource browser input', () => {
366
380
  });
367
381
 
368
382
  await waitFor(() => {
369
- expect(RBI.ResourceBrowserInput).toHaveBeenCalledWith(
383
+ expect(RBI.ResourceBrowserInput).toHaveBeenLastCalledWith(
370
384
  expect.objectContaining({
371
385
  source: calculatedSources[0],
372
386
  plugin: mockDamPlugin,
@@ -375,4 +389,59 @@ describe('Resource browser input', () => {
375
389
  );
376
390
  });
377
391
  });
392
+
393
+ it('onModalStateChange called with false will reset the Mode', async () => {
394
+ const sourcesInput = [mockSource({ type: 'dam' }), mockSource()];
395
+ mockRequestSources.mockResolvedValueOnce(sourcesInput);
396
+
397
+ renderComponent({ value: { sourceId: sourcesInput[0].id, resourceId: '123456' } });
398
+
399
+ // Select a component and plugin mode
400
+ (RBI.ResourceBrowserInput as unknown as jest.SpyInstance).mock.calls[0][0].setSource(sourcesInput[0], { type: 'browse' });
401
+ await waitFor(() => {
402
+ expect(RBI.ResourceBrowserInput).toHaveBeenCalledWith(
403
+ expect.objectContaining({
404
+ pluginMode: {
405
+ type: 'browse',
406
+ },
407
+ }),
408
+ {},
409
+ );
410
+ });
411
+
412
+ // Invoke modal close callback
413
+ const { onModalStateChange } = (RBI.ResourceBrowserInput as unknown as jest.SpyInstance).mock.calls[0][0];
414
+ act(() => {
415
+ onModalStateChange(false);
416
+ });
417
+
418
+ // Expect the mode to be reset
419
+ await waitFor(() => {
420
+ expect(RBI.ResourceBrowserInput).toHaveBeenLastCalledWith(
421
+ expect.objectContaining({
422
+ pluginMode: null,
423
+ }),
424
+ {},
425
+ );
426
+ });
427
+ });
428
+
429
+ it('onRetry reloads sources', async () => {
430
+ const sourcesInput = [mockSource({ type: 'dam' }), mockSource()];
431
+ mockRequestSources.mockResolvedValueOnce(sourcesInput);
432
+
433
+ renderComponent({ value: { sourceId: sourcesInput[0].id, resourceId: '123456' } });
434
+ await waitFor(() => {
435
+ expect(RBI.ResourceBrowserInput).toHaveBeenCalled();
436
+ });
437
+
438
+ const { onRetry } = (RBI.ResourceBrowserInput as unknown as jest.SpyInstance).mock.calls[0][0];
439
+ act(() => {
440
+ onRetry();
441
+ });
442
+
443
+ await waitFor(() => {
444
+ expect(useSourceReloadMock).toHaveBeenCalled();
445
+ });
446
+ });
378
447
  });
package/src/index.tsx CHANGED
@@ -106,6 +106,8 @@ export const ResourceBrowser = (props: ResourceBrowserProps) => {
106
106
  if (!isModalOpen && !value && (sources?.length > 1 || searchEnabled)) {
107
107
  setSource(null);
108
108
  setMode(null);
109
+ } else if (!isModalOpen) {
110
+ setMode(null);
109
111
  }
110
112
  }, [sources, isModalOpen]);
111
113