@capillarytech/creatives-library 9.1.3 → 9.1.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.
@@ -30,6 +30,7 @@ import './index.scss';
30
30
  import {
31
31
  SUCCESS_ALERT,
32
32
  REQUEST,
33
+ SUCCESS,
33
34
  EMBEDDED,
34
35
  DEFAULT,
35
36
  TAG,
@@ -82,6 +83,7 @@ export const Zalo = (props) => {
82
83
  showTestAndPreviewSlidebox: propsShowTestAndPreviewSlidebox,
83
84
  handleTestAndPreview: propsHandleTestAndPreview,
84
85
  handleCloseTestAndPreview: propsHandleCloseTestAndPreview,
86
+ handleClose,
85
87
  onContentValidityChange,
86
88
  } = props || {};
87
89
  // Since zalo supports now multiple accounts, we need to get the hostname from the selected account or passed props.
@@ -95,6 +97,9 @@ export const Zalo = (props) => {
95
97
  const [templateListParams, setTemplateListParams] = useState([]);
96
98
  const [templatePreviewUrl, setTemplatePreviewUrl] = useState('');
97
99
  const [showTestAndPreviewSlidebox, setShowTestAndPreviewSlidebox] = useState(false);
100
+ // Set when opened from listing "Preview and test"; closing then returns to the listing.
101
+ const isTestAndPreviewFromListingRef = useRef(false);
102
+ const hasAutoOpenedTestAndPreviewRef = useRef(false);
98
103
  const [templateStatus, setZaloTemplateStatus] = useState(
99
104
  ZALO_STATUSES.ENABLE,
100
105
  );
@@ -510,8 +515,21 @@ export const Zalo = (props) => {
510
515
  if (propsHandleCloseTestAndPreview) {
511
516
  propsHandleCloseTestAndPreview();
512
517
  }
518
+ if (isTestAndPreviewFromListingRef.current && handleClose) {
519
+ isTestAndPreviewFromListingRef.current = false;
520
+ handleClose();
521
+ }
513
522
  };
514
523
 
524
+ // Listing "Preview and test": open Test & Preview once the template is loaded.
525
+ useEffect(() => {
526
+ if (!isFullMode || !templateData?.openTestAndPreview || hasAutoOpenedTestAndPreviewRef.current) return;
527
+ if (zaloTemplateInfoStatus !== SUCCESS || !templateId) return;
528
+ hasAutoOpenedTestAndPreviewRef.current = true;
529
+ isTestAndPreviewFromListingRef.current = true;
530
+ handleTestAndPreview();
531
+ }, [isFullMode, templateData?.openTestAndPreview, zaloTemplateInfoStatus, templateId]);
532
+
515
533
  return (
516
534
  <CapSpin spinning={[zaloTemplateInfoStatus, metaDataStatus].includes(REQUEST)}>
517
535
  <CapRow type="flex" className="cap-zalo-creatives">
@@ -0,0 +1,98 @@
1
+ import React from 'react';
2
+ import { injectIntl } from 'react-intl';
3
+ import '@testing-library/jest-dom';
4
+ import { Provider } from 'react-redux';
5
+ import { configureStore } from '@capillarytech/vulcan-react-sdk/utils';
6
+ import history from '../../../utils/history';
7
+ import { initialReducer } from '../../../initialReducer';
8
+ import { Zalo } from '..';
9
+ import { SUCCESS } from '../constants';
10
+ import { metaEntities, location, getDefaultTags, injectedTags, accountData } from './mockData';
11
+ import { render, screen, fireEvent } from '../../../utils/test-utils';
12
+
13
+ jest.mock('../../../v2Containers/TagList/index.js', () => ({
14
+ __esModule: true,
15
+ default: () => <div className="tag-mock">TagList</div>,
16
+ }));
17
+ // Stub exposes only open state + close action.
18
+ jest.mock('../../../v2Components/TestAndPreviewSlidebox', () => ({
19
+ __esModule: true,
20
+ default: ({ show, onClose }) => (show ? <button type="button" data-testid="mock-test-preview-close" onClick={onClose} /> : null),
21
+ }));
22
+
23
+ let store;
24
+ beforeAll(() => {
25
+ store = configureStore({}, initialReducer, history);
26
+ });
27
+
28
+ const buildEditData = () => ({
29
+ zaloTemplateInfoStatus: SUCCESS,
30
+ zaloTemplateInfoValue: {
31
+ _id: 'zalo_1',
32
+ name: 'Zalo Template',
33
+ versions: {
34
+ base: {
35
+ content: {
36
+ zalo: {
37
+ templateId: '1',
38
+ listParams: [
39
+ { name: 'danh_xung', require: true, type: 'STRING', maxLength: 30, minLength: 0, error: '', value: '' },
40
+ ],
41
+ previewUrl: 'https://account.zalo.cloud/preview',
42
+ status: 'ENABLE',
43
+ },
44
+ },
45
+ },
46
+ },
47
+ },
48
+ zaloTemplateInfoError: null,
49
+ });
50
+
51
+ const ComponentToRender = injectIntl(Zalo);
52
+
53
+ describe('Zalo - Preview and test from listing', () => {
54
+ const handleClose = jest.fn();
55
+ const handleTestAndPreview = jest.fn();
56
+
57
+ const renderZalo = (templateData) => render(
58
+ <Provider store={store}>
59
+ <ComponentToRender
60
+ actions={{ getTemplateInfoById: jest.fn(), resetTemplateInfoData: jest.fn() }}
61
+ globalActions={{ fetchSchemaForEntity: jest.fn() }}
62
+ editData={buildEditData()}
63
+ metaEntities={metaEntities}
64
+ location={location}
65
+ getDefaultTags={getDefaultTags}
66
+ injectedTags={injectedTags}
67
+ accountData={accountData}
68
+ templateData={templateData}
69
+ isFullMode
70
+ handleClose={handleClose}
71
+ handleTestAndPreview={handleTestAndPreview}
72
+ />
73
+ </Provider>,
74
+ );
75
+
76
+ it('auto-opens Test & Preview and returns to the listing on close', async () => {
77
+ renderZalo({ _id: 'zalo_1', templateConfigs: { id: '', varMapped: {} }, openTestAndPreview: true });
78
+ const closeButton = await screen.findByTestId('mock-test-preview-close');
79
+ expect(handleTestAndPreview).toHaveBeenCalledTimes(1);
80
+
81
+ fireEvent.click(closeButton);
82
+ expect(handleClose).toHaveBeenCalledTimes(1);
83
+ });
84
+
85
+ it('does not auto-open without the listing flag', () => {
86
+ renderZalo({ _id: 'zalo_1', templateConfigs: { id: '', varMapped: {} } });
87
+ expect(screen.queryByTestId('mock-test-preview-close')).not.toBeInTheDocument();
88
+ expect(handleTestAndPreview).not.toHaveBeenCalled();
89
+ });
90
+
91
+ it('keeps the editor open when Test & Preview was opened from the footer', async () => {
92
+ const { container } = renderZalo({ _id: 'zalo_1', templateConfigs: { id: '', varMapped: {} } });
93
+ fireEvent.click(container.querySelector('.test-and-preview-button-zalo'));
94
+
95
+ fireEvent.click(await screen.findByTestId('mock-test-preview-close'));
96
+ expect(handleClose).not.toHaveBeenCalled();
97
+ });
98
+ });