@elementor/editor-documents 0.11.4 → 0.11.6

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/src/types.ts CHANGED
@@ -1,82 +1,82 @@
1
1
  export type ExitTo = 'dashboard' | 'all_posts' | 'this_post';
2
2
 
3
3
  export type Document = {
4
- id: number,
5
- title: string,
4
+ id: number;
5
+ title: string;
6
6
  type: {
7
- value: string,
8
- label: string,
9
- },
7
+ value: string;
8
+ label: string;
9
+ };
10
10
  status: {
11
- value: string,
12
- label: string,
13
- },
11
+ value: string;
12
+ label: string;
13
+ };
14
14
  links: {
15
- platformEdit: string,
16
- permalink: string,
17
- }
18
- isDirty: boolean,
19
- isSaving: boolean,
20
- isSavingDraft: boolean,
15
+ platformEdit: string;
16
+ permalink: string;
17
+ };
18
+ isDirty: boolean;
19
+ isSaving: boolean;
20
+ isSavingDraft: boolean;
21
21
  userCan: {
22
- publish?: boolean,
23
- },
22
+ publish?: boolean;
23
+ };
24
24
  permissions: {
25
- allowAddingWidgets: boolean,
26
- showCopyAndShare: boolean,
27
- },
25
+ allowAddingWidgets: boolean;
26
+ showCopyAndShare: boolean;
27
+ };
28
28
  };
29
29
 
30
30
  export type ExtendedWindow = Window & {
31
31
  elementor: {
32
32
  documents: {
33
- documents: Record<string, V1Document>,
34
- getCurrentId: () => number,
35
- getInitialId: () => number,
36
- getCurrent: () => V1Document,
37
- },
38
- getPreferences: ( key: 'exit_to' ) => ExitTo,
39
- }
40
- }
33
+ documents: Record< string, V1Document >;
34
+ getCurrentId: () => number;
35
+ getInitialId: () => number;
36
+ getCurrent: () => V1Document;
37
+ };
38
+ getPreferences: ( key: 'exit_to' ) => ExitTo;
39
+ };
40
+ };
41
41
 
42
42
  export type V1Document = {
43
- id: number,
43
+ id: number;
44
44
  config: {
45
- type: string,
45
+ type: string;
46
46
  user: {
47
- can_publish: boolean,
48
- },
47
+ can_publish: boolean;
48
+ };
49
49
  revisions: {
50
- current_id: number,
51
- }
50
+ current_id: number;
51
+ };
52
52
  panel: {
53
- title: string,
54
- allow_adding_widgets: boolean,
55
- show_copy_and_share: boolean,
56
- }
53
+ title: string;
54
+ allow_adding_widgets: boolean;
55
+ show_copy_and_share: boolean;
56
+ };
57
57
  status: {
58
- value: string,
59
- label: string,
60
- },
58
+ value: string;
59
+ label: string;
60
+ };
61
61
  urls: {
62
- exit_to_dashboard: string,
63
- permalink: string,
64
- main_dashboard: string,
65
- all_post_type: string,
66
- },
67
- },
62
+ exit_to_dashboard: string;
63
+ permalink: string;
64
+ main_dashboard: string;
65
+ all_post_type: string;
66
+ };
67
+ };
68
68
  editor: {
69
- isChanged: boolean,
70
- isSaving: boolean,
71
- },
69
+ isChanged: boolean;
70
+ isSaving: boolean;
71
+ };
72
72
  container: {
73
- settings: V1Model<{
74
- post_title: string,
75
- exit_to: ExitTo,
76
- }>,
77
- }
78
- }
73
+ settings: V1Model< {
74
+ post_title: string;
75
+ exit_to: ExitTo;
76
+ } >;
77
+ };
78
+ };
79
79
 
80
- type V1Model<T> = {
81
- get: <K extends keyof T = keyof T>( key: K ) => T[K],
82
- }
80
+ type V1Model< T > = {
81
+ get: < K extends keyof T = keyof T >( key: K ) => T[ K ];
82
+ };
@@ -1,60 +0,0 @@
1
- import useActiveDocumentActions from '../use-active-document-actions';
2
- import { __privateOpenRoute as openRoute, __privateRunCommand as runCommand } from '@elementor/editor-v1-adapters';
3
- import { __createStore, __registerSlice, SliceState, Store } from '@elementor/store';
4
- import { slice } from '../../store';
5
- import { renderHookWithStore } from 'test-utils';
6
-
7
- jest.mock( '@elementor/editor-v1-adapters' );
8
-
9
- describe( '@elementor/editor-documents - useActiveDocumentActions', () => {
10
- let store: Store<SliceState<typeof slice>>;
11
-
12
- beforeEach( () => {
13
- __registerSlice( slice );
14
- store = __createStore();
15
- } );
16
-
17
- it( 'should run documents actions', () => {
18
- // Arrange.
19
- const { result } = renderHookWithStore( useActiveDocumentActions, store );
20
-
21
- const {
22
- save,
23
- saveDraft,
24
- saveTemplate,
25
- } = result.current;
26
-
27
- // Act.
28
- save();
29
- saveDraft();
30
- saveTemplate();
31
-
32
- // Assert.
33
- expect( runCommand ).toHaveBeenCalledTimes( 2 );
34
-
35
- expect( runCommand ).toHaveBeenNthCalledWith( 1, 'document/save/default' );
36
- expect( runCommand ).toHaveBeenNthCalledWith( 2, 'document/save/draft' );
37
-
38
- expect( openRoute ).toHaveBeenCalledTimes( 1 );
39
- expect( openRoute ).toHaveBeenCalledWith( 'library/save-template' );
40
- } );
41
-
42
- it( 'should return memoized callbacks', () => {
43
- // Arrange.
44
- const { result, rerender } = renderHookWithStore( useActiveDocumentActions, store );
45
-
46
- const {
47
- save,
48
- saveDraft,
49
- saveTemplate,
50
- } = result.current;
51
-
52
- // Act.
53
- rerender();
54
-
55
- // Assert.
56
- expect( result.current.save ).toBe( save );
57
- expect( result.current.saveDraft ).toBe( saveDraft );
58
- expect( result.current.saveTemplate ).toBe( saveTemplate );
59
- } );
60
- } );
@@ -1,34 +0,0 @@
1
- import { slice } from '../../store';
2
- import useActiveDocument from '../use-active-document';
3
- import { __createStore, __dispatch, __registerSlice, SliceState, Store } from '@elementor/store';
4
- import { createMockDocument, renderHookWithStore } from 'test-utils';
5
-
6
- describe( '@elementor/editor-documents - useActiveDocument', () => {
7
- let store: Store<SliceState<typeof slice>>;
8
-
9
- beforeEach( () => {
10
- __registerSlice( slice );
11
- store = __createStore();
12
- } );
13
-
14
- it( 'should return the current document', () => {
15
- // Arrange.
16
- const mockDocument = createMockDocument();
17
-
18
- __dispatch( slice.actions.activateDocument( mockDocument ) );
19
-
20
- // Act.
21
- const { result } = renderHookWithStore( useActiveDocument, store );
22
-
23
- // Assert.
24
- expect( result.current ).toBe( mockDocument );
25
- } );
26
-
27
- it( 'should return null when the current document is not found', () => {
28
- // Act.
29
- const { result } = renderHookWithStore( useActiveDocument, store );
30
-
31
- // Assert.
32
- expect( result.current ).toBeNull();
33
- } );
34
- } );
@@ -1,45 +0,0 @@
1
- import { slice } from '../../store';
2
- import { __createStore, __dispatch, __registerSlice, SliceState, Store } from '@elementor/store';
3
- import useHostDocument from '../use-host-document';
4
- import { createMockDocument, renderHookWithStore } from 'test-utils';
5
-
6
- describe( '@elementor/editor-documents - useHostDocument', () => {
7
- const mockDocument = createMockDocument();
8
-
9
- let store: Store<SliceState<typeof slice>>;
10
-
11
- beforeEach( () => {
12
- __registerSlice( slice );
13
- store = __createStore();
14
- } );
15
-
16
- it( 'should return the host document', () => {
17
- // Arrange.
18
- __dispatch( slice.actions.init( {
19
- entities: { [ mockDocument.id ]: mockDocument },
20
- activeId: null,
21
- hostId: mockDocument.id,
22
- } ) );
23
-
24
- // Act.
25
- const { result } = renderHookWithStore( useHostDocument, store );
26
-
27
- // Assert.
28
- expect( result.current ).toEqual( expect.objectContaining( { id: mockDocument.id } ) );
29
- } );
30
-
31
- it( 'should return null when the host document is not found', () => {
32
- // Arrange.
33
- __dispatch( slice.actions.init( {
34
- entities: { [ mockDocument.id ]: mockDocument },
35
- activeId: null,
36
- hostId: null,
37
- } ) );
38
-
39
- // Act.
40
- const { result } = renderHookWithStore( useHostDocument, store );
41
-
42
- // Assert.
43
- expect( result.current ).toBeNull();
44
- } );
45
- } );
@@ -1,60 +0,0 @@
1
- import { renderHook } from '@testing-library/react';
2
- import { __privateRunCommand as runCommand } from '@elementor/editor-v1-adapters';
3
- import { useNavigateToDocument } from '../index';
4
-
5
- jest.mock( '@elementor/editor-v1-adapters' );
6
-
7
- describe( '@elementor/editor-documents - useNavigateToDocument', () => {
8
- const originalReplaceState = history.replaceState;
9
- const originalLocation = window.location;
10
-
11
- beforeEach( () => {
12
- jest.resetAllMocks();
13
-
14
- history.replaceState = jest.fn();
15
-
16
- /**
17
- * @see https://gist.github.com/the0neWhoKnocks/bdac1d09b93b8418d948558f7ab233d7#setting-props-on-windowlocation
18
- */
19
- Object.defineProperty( window, 'location', {
20
- writable: true,
21
- value: new URL( 'https://localhost/' ),
22
- } );
23
- } );
24
-
25
- afterAll( () => {
26
- history.replaceState = originalReplaceState;
27
-
28
- Object.defineProperty( window, 'location', {
29
- writable: false,
30
- value: originalLocation,
31
- } );
32
- } );
33
-
34
- it( 'should navigate to document and change query params', async () => {
35
- // Arrange.
36
- // TS doesn't allow modifying the location object.
37
- ( window as unknown as { location: URL } ).location = new URL( 'https://localhost/?post=1&active-document=3' );
38
-
39
- const { result } = renderHook( useNavigateToDocument );
40
-
41
- const navigateToDocument = result.current;
42
-
43
- // Act.
44
- await navigateToDocument( 123 );
45
-
46
- // Assert.
47
- expect( runCommand ).toHaveBeenCalledTimes( 1 );
48
- expect( runCommand ).toHaveBeenCalledWith( 'editor/documents/switch', {
49
- id: 123,
50
- setAsInitial: true,
51
- } );
52
-
53
- expect( history.replaceState ).toHaveBeenCalledTimes( 1 );
54
- expect( history.replaceState ).toHaveBeenCalledWith(
55
- expect.anything(),
56
- expect.anything(),
57
- expect.objectContaining( { href: 'https://localhost/?post=123' } )
58
- );
59
- } );
60
- } );
@@ -1,99 +0,0 @@
1
- import { createMockDocument } from 'test-utils';
2
- import { renderHook } from '@testing-library/react';
3
- import useHostDocument from '../use-host-document';
4
- import useActiveDocument from '../use-active-document';
5
- import useSyncDocumentTitle from '../use-sync-document-title';
6
-
7
- jest.mock( '../use-active-document' );
8
- jest.mock( '../use-host-document' );
9
-
10
- describe( 'useSyncDocumentTitle', () => {
11
- beforeEach( () => {
12
- jest.resetAllMocks();
13
- } );
14
-
15
- it( 'should set the document title to be the active document', () => {
16
- // Arrange - set the initial document.
17
- jest.mocked( useActiveDocument ).mockReturnValue( createMockDocument( {
18
- type: {
19
- value: 'page',
20
- label: 'Page',
21
- },
22
- title: 'Initial Document',
23
- } ) );
24
-
25
- // Act.
26
- const { rerender } = renderHook( useSyncDocumentTitle );
27
-
28
- // Assert.
29
- expect( window.document.title ).toBe( 'Edit "Initial Document" with Elementor' );
30
-
31
- // Arrange - set the new document.
32
- jest.mocked( useActiveDocument ).mockReturnValue( createMockDocument( {
33
- type: {
34
- value: 'page',
35
- label: 'Page',
36
- },
37
- title: 'New Document',
38
- } ) );
39
-
40
- // Act.
41
- rerender();
42
-
43
- // Assert.
44
- expect( window.document.title ).toBe( 'Edit "New Document" with Elementor' );
45
- } );
46
-
47
- it( 'should use the host document title when the active document is a kit', () => {
48
- // Arrange.
49
- jest.mocked( useActiveDocument ).mockReturnValue( createMockDocument( {
50
- type: {
51
- value: 'kit',
52
- label: 'Kit',
53
- },
54
- title: 'My Kit',
55
- } ) );
56
-
57
- jest.mocked( useHostDocument ).mockReturnValue( createMockDocument( {
58
- type: {
59
- value: 'page',
60
- label: 'Page',
61
- },
62
- title: 'My Page',
63
- } ) );
64
-
65
- window.document.title = 'Old title';
66
-
67
- // Act.
68
- renderHook( useSyncDocumentTitle );
69
-
70
- // Assert.
71
- expect( window.document.title ).toBe( 'Edit "My Page" with Elementor' );
72
- } );
73
-
74
- it( 'should not modify the title when there is no active document', () => {
75
- // Arrange.
76
- jest.mocked( useActiveDocument ).mockReturnValue( null );
77
-
78
- window.document.title = 'Old title';
79
-
80
- // Act.
81
- renderHook( useSyncDocumentTitle );
82
-
83
- // Assert.
84
- expect( window.document.title ).toBe( 'Old title' );
85
- } );
86
-
87
- it( 'should allow empty string as title', () => {
88
- // Arrange.
89
- jest.mocked( useActiveDocument ).mockReturnValue( createMockDocument( {
90
- title: '',
91
- } ) );
92
-
93
- // Act.
94
- renderHook( useSyncDocumentTitle );
95
-
96
- // Assert.
97
- expect( window.document.title ).toBe( 'Edit "" with Elementor' );
98
- } );
99
- } );