@elementor/editor-v1-adapters 0.8.0 → 0.8.2
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 +10 -0
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/package.json +10 -3
- package/src/dispatchers/types.ts +1 -1
- package/src/dispatchers/__tests__/index.test.ts +0 -218
- package/src/hooks/__tests__/test-utils.ts +0 -21
- package/src/hooks/__tests__/use-is-preview-mode.test.ts +0 -47
- package/src/hooks/__tests__/use-is-route-active.test.ts +0 -93
- package/src/hooks/__tests__/use-route-status.test.ts +0 -124
- package/src/listeners/__tests__/index.test.ts +0 -300
- package/src/readers/__tests__/index.test.ts +0 -70
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
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
|
+
## [0.8.2](https://github.com/elementor/elementor-packages/compare/@elementor/editor-v1-adapters@0.8.1...@elementor/editor-v1-adapters@0.8.2) (2024-08-20)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @elementor/editor-v1-adapters
|
|
9
|
+
|
|
10
|
+
## [0.8.1](https://github.com/elementor/elementor-packages/compare/@elementor/editor-v1-adapters@0.8.0...@elementor/editor-v1-adapters@0.8.1) (2024-08-05)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
- publish only necessary files to npm ([#226](https://github.com/elementor/elementor-packages/issues/226)) ([d808e2f](https://github.com/elementor/elementor-packages/commit/d808e2f60eb7ca2d7b8560d0b79c0e62c2f969a8))
|
|
15
|
+
|
|
6
16
|
# [0.8.0](https://github.com/elementor/elementor-packages/compare/@elementor/editor-v1-adapters@0.7.0...@elementor/editor-v1-adapters@0.8.0) (2024-07-16)
|
|
7
17
|
|
|
8
18
|
### Features
|
package/dist/index.d.mts
CHANGED
|
@@ -46,11 +46,11 @@ type EventDescriptor = CommandEventDescriptor | WindowEventDescriptor | RouteEve
|
|
|
46
46
|
type ListenerEvent = WindowEvent | CommandEvent | RouteEvent;
|
|
47
47
|
type ListenerCallback = (e: ListenerEvent) => void;
|
|
48
48
|
|
|
49
|
-
declare const commandStartEvent: (command: CommandEventDescriptor[
|
|
50
|
-
declare const commandEndEvent: (command: CommandEventDescriptor[
|
|
51
|
-
declare const routeOpenEvent: (route: RouteEventDescriptor[
|
|
52
|
-
declare const routeCloseEvent: (route: RouteEventDescriptor[
|
|
53
|
-
declare const windowEvent: (event: WindowEventDescriptor[
|
|
49
|
+
declare const commandStartEvent: (command: CommandEventDescriptor["name"]) => CommandEventDescriptor;
|
|
50
|
+
declare const commandEndEvent: (command: CommandEventDescriptor["name"]) => CommandEventDescriptor;
|
|
51
|
+
declare const routeOpenEvent: (route: RouteEventDescriptor["name"]) => RouteEventDescriptor;
|
|
52
|
+
declare const routeCloseEvent: (route: RouteEventDescriptor["name"]) => RouteEventDescriptor;
|
|
53
|
+
declare const windowEvent: (event: WindowEventDescriptor["name"]) => WindowEventDescriptor;
|
|
54
54
|
declare const v1ReadyEvent: () => WindowEventDescriptor;
|
|
55
55
|
declare const editModeChangeEvent: () => WindowEventDescriptor;
|
|
56
56
|
|
|
@@ -84,4 +84,4 @@ type EditMode = 'edit' | 'preview' | 'picker';
|
|
|
84
84
|
declare function isRouteActive(route: string): boolean;
|
|
85
85
|
declare function getCurrentEditMode(): EditMode;
|
|
86
86
|
|
|
87
|
-
export { CommandEvent, CommandEventDescriptor, EventDescriptor, ExtendedWindow, ListenerCallback, ListenerEvent, RouteEvent, RouteEventDescriptor, WindowEvent, WindowEventDescriptor, blockDataCommand as __privateBlockDataCommand, dispatchReadyEvent as __privateDispatchReadyEvent, flushListeners as __privateFlushListeners, getCurrentEditMode as __privateGetCurrentEditMode, isRouteActive as __privateIsRouteActive, listenTo as __privateListenTo, openRoute as __privateOpenRoute, registerRoute as __privateRegisterRoute, runCommand as __privateRunCommand, setReady as __privateSetReady, useIsPreviewMode as __privateUseIsPreviewMode, useIsRouteActive as __privateUseIsRouteActive, useListenTo as __privateUseListenTo, useRouteStatus as __privateUseRouteStatus, commandEndEvent, commandStartEvent, dispatchReadyEvent, editModeChangeEvent, flushListeners, isReady, listenTo, routeCloseEvent, routeOpenEvent, setReady, v1ReadyEvent, windowEvent };
|
|
87
|
+
export { type CommandEvent, type CommandEventDescriptor, type EventDescriptor, type ExtendedWindow, type ListenerCallback, type ListenerEvent, type RouteEvent, type RouteEventDescriptor, type WindowEvent, type WindowEventDescriptor, blockDataCommand as __privateBlockDataCommand, dispatchReadyEvent as __privateDispatchReadyEvent, flushListeners as __privateFlushListeners, getCurrentEditMode as __privateGetCurrentEditMode, isRouteActive as __privateIsRouteActive, listenTo as __privateListenTo, openRoute as __privateOpenRoute, registerRoute as __privateRegisterRoute, runCommand as __privateRunCommand, setReady as __privateSetReady, useIsPreviewMode as __privateUseIsPreviewMode, useIsRouteActive as __privateUseIsRouteActive, useListenTo as __privateUseListenTo, useRouteStatus as __privateUseRouteStatus, commandEndEvent, commandStartEvent, dispatchReadyEvent, editModeChangeEvent, flushListeners, isReady, listenTo, routeCloseEvent, routeOpenEvent, setReady, v1ReadyEvent, windowEvent };
|
package/dist/index.d.ts
CHANGED
|
@@ -46,11 +46,11 @@ type EventDescriptor = CommandEventDescriptor | WindowEventDescriptor | RouteEve
|
|
|
46
46
|
type ListenerEvent = WindowEvent | CommandEvent | RouteEvent;
|
|
47
47
|
type ListenerCallback = (e: ListenerEvent) => void;
|
|
48
48
|
|
|
49
|
-
declare const commandStartEvent: (command: CommandEventDescriptor[
|
|
50
|
-
declare const commandEndEvent: (command: CommandEventDescriptor[
|
|
51
|
-
declare const routeOpenEvent: (route: RouteEventDescriptor[
|
|
52
|
-
declare const routeCloseEvent: (route: RouteEventDescriptor[
|
|
53
|
-
declare const windowEvent: (event: WindowEventDescriptor[
|
|
49
|
+
declare const commandStartEvent: (command: CommandEventDescriptor["name"]) => CommandEventDescriptor;
|
|
50
|
+
declare const commandEndEvent: (command: CommandEventDescriptor["name"]) => CommandEventDescriptor;
|
|
51
|
+
declare const routeOpenEvent: (route: RouteEventDescriptor["name"]) => RouteEventDescriptor;
|
|
52
|
+
declare const routeCloseEvent: (route: RouteEventDescriptor["name"]) => RouteEventDescriptor;
|
|
53
|
+
declare const windowEvent: (event: WindowEventDescriptor["name"]) => WindowEventDescriptor;
|
|
54
54
|
declare const v1ReadyEvent: () => WindowEventDescriptor;
|
|
55
55
|
declare const editModeChangeEvent: () => WindowEventDescriptor;
|
|
56
56
|
|
|
@@ -84,4 +84,4 @@ type EditMode = 'edit' | 'preview' | 'picker';
|
|
|
84
84
|
declare function isRouteActive(route: string): boolean;
|
|
85
85
|
declare function getCurrentEditMode(): EditMode;
|
|
86
86
|
|
|
87
|
-
export { CommandEvent, CommandEventDescriptor, EventDescriptor, ExtendedWindow, ListenerCallback, ListenerEvent, RouteEvent, RouteEventDescriptor, WindowEvent, WindowEventDescriptor, blockDataCommand as __privateBlockDataCommand, dispatchReadyEvent as __privateDispatchReadyEvent, flushListeners as __privateFlushListeners, getCurrentEditMode as __privateGetCurrentEditMode, isRouteActive as __privateIsRouteActive, listenTo as __privateListenTo, openRoute as __privateOpenRoute, registerRoute as __privateRegisterRoute, runCommand as __privateRunCommand, setReady as __privateSetReady, useIsPreviewMode as __privateUseIsPreviewMode, useIsRouteActive as __privateUseIsRouteActive, useListenTo as __privateUseListenTo, useRouteStatus as __privateUseRouteStatus, commandEndEvent, commandStartEvent, dispatchReadyEvent, editModeChangeEvent, flushListeners, isReady, listenTo, routeCloseEvent, routeOpenEvent, setReady, v1ReadyEvent, windowEvent };
|
|
87
|
+
export { type CommandEvent, type CommandEventDescriptor, type EventDescriptor, type ExtendedWindow, type ListenerCallback, type ListenerEvent, type RouteEvent, type RouteEventDescriptor, type WindowEvent, type WindowEventDescriptor, blockDataCommand as __privateBlockDataCommand, dispatchReadyEvent as __privateDispatchReadyEvent, flushListeners as __privateFlushListeners, getCurrentEditMode as __privateGetCurrentEditMode, isRouteActive as __privateIsRouteActive, listenTo as __privateListenTo, openRoute as __privateOpenRoute, registerRoute as __privateRegisterRoute, runCommand as __privateRunCommand, setReady as __privateSetReady, useIsPreviewMode as __privateUseIsPreviewMode, useIsRouteActive as __privateUseIsRouteActive, useListenTo as __privateUseListenTo, useRouteStatus as __privateUseRouteStatus, commandEndEvent, commandStartEvent, dispatchReadyEvent, editModeChangeEvent, flushListeners, isReady, listenTo, routeCloseEvent, routeOpenEvent, setReady, v1ReadyEvent, windowEvent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-v1-adapters",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/elementor/elementor-packages.git",
|
|
21
|
+
"url": "git+https://github.com/elementor/elementor-packages.git",
|
|
22
22
|
"directory": "packages/core/editor-v1-adapters"
|
|
23
23
|
},
|
|
24
24
|
"bugs": {
|
|
@@ -27,6 +27,13 @@
|
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
+
"files": [
|
|
31
|
+
"README.md",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"/dist",
|
|
34
|
+
"/src",
|
|
35
|
+
"!**/__tests__"
|
|
36
|
+
],
|
|
30
37
|
"scripts": {
|
|
31
38
|
"build": "tsup --config=../../tsup.build.ts",
|
|
32
39
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
@@ -34,5 +41,5 @@
|
|
|
34
41
|
"peerDependencies": {
|
|
35
42
|
"react": "^18.3.1"
|
|
36
43
|
},
|
|
37
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "fceae13994a4b8c16955e37448179b13ed78a71a"
|
|
38
45
|
}
|
package/src/dispatchers/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type jQueryDeferred< T > = {
|
|
2
|
-
then< U >( onFulfill: ( value: T ) => U, onReject?: ( error: unknown ) => U )
|
|
2
|
+
then: < U >( onFulfill: ( value: T ) => U, onReject?: ( error: unknown ) => U ) => jQueryDeferred< U >;
|
|
3
3
|
};
|
|
4
4
|
|
|
5
5
|
export type ExtendedWindow = Window & {
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { openRoute, blockDataCommand, registerRoute, runCommand } from '../index';
|
|
2
|
-
import { DependencyHook, ExtendedWindow } from '../types';
|
|
3
|
-
|
|
4
|
-
describe( '@elementor/editor-v1-adapters/dispatchers', () => {
|
|
5
|
-
let eRun: jest.Mock, eRoute: jest.Mock, eRegisterRoute: jest.Mock, eDependencyRegisterSpy: jest.SpyInstance;
|
|
6
|
-
|
|
7
|
-
let hooks: DependencyHook[] = [];
|
|
8
|
-
|
|
9
|
-
beforeEach( () => {
|
|
10
|
-
hooks = [];
|
|
11
|
-
const extendedWindow = window as unknown as ExtendedWindow;
|
|
12
|
-
|
|
13
|
-
extendedWindow.$e = {
|
|
14
|
-
run: jest.fn(),
|
|
15
|
-
route: jest.fn(),
|
|
16
|
-
routes: {
|
|
17
|
-
register: jest.fn(),
|
|
18
|
-
},
|
|
19
|
-
modules: {
|
|
20
|
-
hookData: {
|
|
21
|
-
Dependency: class {
|
|
22
|
-
register() {
|
|
23
|
-
hooks.push( this as never );
|
|
24
|
-
}
|
|
25
|
-
} as typeof DependencyHook,
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
eRun = jest.mocked( extendedWindow.$e.run );
|
|
31
|
-
eRoute = jest.mocked( extendedWindow.$e.route );
|
|
32
|
-
eRegisterRoute = jest.mocked( extendedWindow.$e?.routes?.register || jest.fn() );
|
|
33
|
-
eDependencyRegisterSpy = jest.spyOn(
|
|
34
|
-
extendedWindow.$e?.modules?.hookData?.Dependency?.prototype as DependencyHook,
|
|
35
|
-
'register'
|
|
36
|
-
);
|
|
37
|
-
} );
|
|
38
|
-
|
|
39
|
-
it( 'should run a V1 command that returns Promise', () => {
|
|
40
|
-
// Arrange.
|
|
41
|
-
const command = 'editor/documents/open',
|
|
42
|
-
args = { test: 'arg' };
|
|
43
|
-
|
|
44
|
-
eRun.mockReturnValue( Promise.resolve( 'result' ) );
|
|
45
|
-
|
|
46
|
-
// Act.
|
|
47
|
-
const result = runCommand( command, args );
|
|
48
|
-
|
|
49
|
-
// Assert.
|
|
50
|
-
expect( eRun ).toHaveBeenCalledWith( command, args );
|
|
51
|
-
expect( result ).toEqual( Promise.resolve( 'result' ) );
|
|
52
|
-
} );
|
|
53
|
-
|
|
54
|
-
it( 'should run a V1 command that returns jQuery.Deferred object', () => {
|
|
55
|
-
// Arrange.
|
|
56
|
-
const command = 'editor/documents/open',
|
|
57
|
-
args = { test: 'arg' };
|
|
58
|
-
|
|
59
|
-
eRun.mockReturnValue( makeJQueryDeferred( 'result' ) );
|
|
60
|
-
|
|
61
|
-
// Act.
|
|
62
|
-
const result = runCommand( command, args );
|
|
63
|
-
|
|
64
|
-
// Assert.
|
|
65
|
-
expect( eRun ).toHaveBeenCalledWith( command, args );
|
|
66
|
-
expect( result ).toEqual( Promise.resolve( 'result' ) );
|
|
67
|
-
} );
|
|
68
|
-
|
|
69
|
-
it( 'should run a V1 command that returns a plain value', () => {
|
|
70
|
-
// Arrange.
|
|
71
|
-
const command = 'editor/documents/open',
|
|
72
|
-
args = { test: 'arg' };
|
|
73
|
-
|
|
74
|
-
eRun.mockReturnValue( 'result' );
|
|
75
|
-
|
|
76
|
-
// Act.
|
|
77
|
-
const result = runCommand( command, args );
|
|
78
|
-
|
|
79
|
-
// Assert.
|
|
80
|
-
expect( eRun ).toHaveBeenCalledWith( command, args );
|
|
81
|
-
expect( result ).toEqual( Promise.resolve( 'result' ) );
|
|
82
|
-
} );
|
|
83
|
-
|
|
84
|
-
it( 'should open a V1 route', () => {
|
|
85
|
-
// Arrange.
|
|
86
|
-
const route = 'test/route';
|
|
87
|
-
|
|
88
|
-
// Act.
|
|
89
|
-
const result = openRoute( route );
|
|
90
|
-
|
|
91
|
-
// Assert.
|
|
92
|
-
expect( eRoute ).toHaveBeenCalledWith( route );
|
|
93
|
-
expect( result ).toEqual( Promise.resolve() );
|
|
94
|
-
} );
|
|
95
|
-
|
|
96
|
-
it( 'should reject when failing to open a V1 route', () => {
|
|
97
|
-
// Arrange.
|
|
98
|
-
const route = 'test/route';
|
|
99
|
-
|
|
100
|
-
eRoute.mockImplementation( ( r: string ) => {
|
|
101
|
-
throw `Cannot find ${ r }`;
|
|
102
|
-
} );
|
|
103
|
-
|
|
104
|
-
// Act.
|
|
105
|
-
expect( () => openRoute( route ) ).rejects.toEqual( 'Cannot find test/route' );
|
|
106
|
-
} );
|
|
107
|
-
|
|
108
|
-
it.each( [
|
|
109
|
-
[ 'test/route', [ 'test', 'route' ] ],
|
|
110
|
-
[ 'test/test2/route', [ 'test/test2', 'route' ] ],
|
|
111
|
-
] )( 'should register the route `%s` in v1', ( route, params ) => {
|
|
112
|
-
// Act.
|
|
113
|
-
registerRoute( route );
|
|
114
|
-
|
|
115
|
-
// Assert.
|
|
116
|
-
expect( eRegisterRoute ).toHaveBeenCalledWith( ...params, expect.any( Function ) );
|
|
117
|
-
} );
|
|
118
|
-
|
|
119
|
-
it( 'should throw if trying to register invalid route', () => {
|
|
120
|
-
// Act & Assert.
|
|
121
|
-
expect( () => registerRoute( 'test' ) ).rejects.toEqual( '`test` is an invalid route' );
|
|
122
|
-
} );
|
|
123
|
-
|
|
124
|
-
it.each( [
|
|
125
|
-
[ '$e.route()', () => openRoute( 'test/route' ) ],
|
|
126
|
-
[ '$e.run()', () => runCommand( 'editor/documents/open' ) ],
|
|
127
|
-
[ '$e.routes.register()', () => registerRoute( 'test/route' ) ],
|
|
128
|
-
[
|
|
129
|
-
'$e.modules.hookData.Dependency',
|
|
130
|
-
() =>
|
|
131
|
-
blockDataCommand( {
|
|
132
|
-
command: '',
|
|
133
|
-
condition: () => true,
|
|
134
|
-
} ),
|
|
135
|
-
],
|
|
136
|
-
] )( 'should reject when trying to use V1 and `%s` is unavailable', ( v1Method, action ) => {
|
|
137
|
-
// Arrange.
|
|
138
|
-
delete ( window as { $e?: unknown } ).$e;
|
|
139
|
-
|
|
140
|
-
// Act & Assert.
|
|
141
|
-
expect( () => action() ).rejects.toEqual( `\`${ v1Method }\` is not available` );
|
|
142
|
-
} );
|
|
143
|
-
|
|
144
|
-
it( 'should register a V1 dependency hook that blocks a V1 command', () => {
|
|
145
|
-
// Act.
|
|
146
|
-
const result = blockDataCommand( {
|
|
147
|
-
command: '',
|
|
148
|
-
condition: () => true,
|
|
149
|
-
} );
|
|
150
|
-
|
|
151
|
-
// Assert.
|
|
152
|
-
expect( eDependencyRegisterSpy ).toHaveBeenCalled();
|
|
153
|
-
expect( result ).toEqual( Promise.resolve() );
|
|
154
|
-
} );
|
|
155
|
-
|
|
156
|
-
it( 'should reject when failing to register a V1 dependency hook that blocks a V1 command', () => {
|
|
157
|
-
// Arrange.
|
|
158
|
-
eDependencyRegisterSpy.mockImplementation( () => {
|
|
159
|
-
throw 'Error';
|
|
160
|
-
} );
|
|
161
|
-
|
|
162
|
-
// Act & Assert.
|
|
163
|
-
expect( () =>
|
|
164
|
-
blockDataCommand( {
|
|
165
|
-
command: '',
|
|
166
|
-
condition: () => true,
|
|
167
|
-
} )
|
|
168
|
-
).rejects.toEqual( 'Error' );
|
|
169
|
-
} );
|
|
170
|
-
|
|
171
|
-
it( 'should get the right command name, id and condition when blocking a v1 command', () => {
|
|
172
|
-
// Arrange.
|
|
173
|
-
blockDataCommand( {
|
|
174
|
-
command: 'editor/documents/open',
|
|
175
|
-
condition: ( args ) => 'key' in args,
|
|
176
|
-
} );
|
|
177
|
-
|
|
178
|
-
blockDataCommand( {
|
|
179
|
-
command: 'editor/documents/open',
|
|
180
|
-
condition: () => false,
|
|
181
|
-
} );
|
|
182
|
-
|
|
183
|
-
blockDataCommand( {
|
|
184
|
-
command: 'editor/documents/close',
|
|
185
|
-
condition: () => false,
|
|
186
|
-
} );
|
|
187
|
-
|
|
188
|
-
// Act & Assert.
|
|
189
|
-
expect( hooks ).toHaveLength( 3 );
|
|
190
|
-
|
|
191
|
-
expect( hooks[ 0 ].getCommand() ).toEqual( 'editor/documents/open' );
|
|
192
|
-
expect( hooks[ 1 ].getCommand() ).toEqual( 'editor/documents/open' );
|
|
193
|
-
expect( hooks[ 2 ].getCommand() ).toEqual( 'editor/documents/close' );
|
|
194
|
-
|
|
195
|
-
const hookId1 = hooks[ 0 ].getId();
|
|
196
|
-
const hookId2 = hooks[ 1 ].getId();
|
|
197
|
-
const hookId3 = hooks[ 2 ].getId();
|
|
198
|
-
|
|
199
|
-
expect( hookId1 ).not.toEqual( hookId2 );
|
|
200
|
-
|
|
201
|
-
expect( hookId1 ).toMatch( /^editor\/documents\/open--block--\d+$/ );
|
|
202
|
-
expect( hookId2 ).toMatch( /^editor\/documents\/open--block--\d+$/ );
|
|
203
|
-
expect( hookId3 ).toMatch( /^editor\/documents\/close--block--\d+$/ );
|
|
204
|
-
|
|
205
|
-
expect( hooks[ 0 ].apply( { key: 'value' } ) ).toBe( false );
|
|
206
|
-
expect( hooks[ 0 ].apply( { value: 'value' } ) ).toBe( true );
|
|
207
|
-
} );
|
|
208
|
-
} );
|
|
209
|
-
|
|
210
|
-
function makeJQueryDeferred( value: unknown ) {
|
|
211
|
-
return {
|
|
212
|
-
then: () => value,
|
|
213
|
-
promise: () => value,
|
|
214
|
-
fail: () => {
|
|
215
|
-
throw 'Error';
|
|
216
|
-
},
|
|
217
|
-
};
|
|
218
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { EditMode, ExtendedWindow } from '../../readers/types';
|
|
2
|
-
|
|
3
|
-
const extendedWindow = window as unknown as ExtendedWindow;
|
|
4
|
-
|
|
5
|
-
export function mockIsRouteActive( implementation: ( route: string ) => boolean ) {
|
|
6
|
-
extendedWindow.$e = {
|
|
7
|
-
routes: {
|
|
8
|
-
isPartOf: jest.fn( implementation ),
|
|
9
|
-
},
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function mockGetCurrentEditMode( implementation: () => EditMode ) {
|
|
14
|
-
extendedWindow.elementor = {
|
|
15
|
-
channels: {
|
|
16
|
-
dataEditMode: {
|
|
17
|
-
request: jest.fn( implementation ),
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { act, renderHook } from '@testing-library/react';
|
|
2
|
-
import { __privateUseIsPreviewMode as useIsPreviewMode } from '../../index';
|
|
3
|
-
import { dispatchEditModeChange } from 'test-utils';
|
|
4
|
-
import { mockGetCurrentEditMode } from './test-utils';
|
|
5
|
-
|
|
6
|
-
describe( '@elementor/editor-v1-adapters - useIsPreviewMode', () => {
|
|
7
|
-
it.each( [
|
|
8
|
-
{ mode: 'preview', expected: true },
|
|
9
|
-
{ mode: 'edit', expected: false },
|
|
10
|
-
{ mode: 'picker', expected: false },
|
|
11
|
-
] as const )( 'should return $expected when the current edit mode is $mode', ( { mode, expected } ) => {
|
|
12
|
-
// Arrange.
|
|
13
|
-
mockGetCurrentEditMode( () => mode );
|
|
14
|
-
|
|
15
|
-
// Act.
|
|
16
|
-
const { result } = renderHook( () => useIsPreviewMode() );
|
|
17
|
-
|
|
18
|
-
// Assert.
|
|
19
|
-
expect( result.current ).toBe( expected );
|
|
20
|
-
} );
|
|
21
|
-
|
|
22
|
-
it( 'should update the state when the edit mode changes', () => {
|
|
23
|
-
// Arrange.
|
|
24
|
-
const { result } = renderHook( () => useIsPreviewMode() );
|
|
25
|
-
|
|
26
|
-
// Assert.
|
|
27
|
-
expect( result.current ).toBe( false );
|
|
28
|
-
|
|
29
|
-
// Act.
|
|
30
|
-
act( () => {
|
|
31
|
-
mockGetCurrentEditMode( () => 'preview' );
|
|
32
|
-
dispatchEditModeChange();
|
|
33
|
-
} );
|
|
34
|
-
|
|
35
|
-
// Assert.
|
|
36
|
-
expect( result.current ).toBe( true );
|
|
37
|
-
|
|
38
|
-
// Act.
|
|
39
|
-
act( () => {
|
|
40
|
-
mockGetCurrentEditMode( () => 'edit' );
|
|
41
|
-
dispatchEditModeChange();
|
|
42
|
-
} );
|
|
43
|
-
|
|
44
|
-
// Assert.
|
|
45
|
-
expect( result.current ).toBe( false );
|
|
46
|
-
} );
|
|
47
|
-
} );
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { act, renderHook } from '@testing-library/react';
|
|
2
|
-
import useIsRouteActive from '../use-is-route-active';
|
|
3
|
-
import { dispatchRouteClose, dispatchRouteOpen } from 'test-utils';
|
|
4
|
-
import { mockIsRouteActive } from './test-utils';
|
|
5
|
-
|
|
6
|
-
describe( '@elementor/editor-v1-adapters - useIsRouteActive', () => {
|
|
7
|
-
it( 'should return false when a route is inactive by default', () => {
|
|
8
|
-
// Arrange.
|
|
9
|
-
const route = 'panel/menu';
|
|
10
|
-
|
|
11
|
-
mockIsRouteActive( () => false );
|
|
12
|
-
|
|
13
|
-
// Act.
|
|
14
|
-
const { result } = renderHook( () => useIsRouteActive( route ) );
|
|
15
|
-
|
|
16
|
-
// Assert.
|
|
17
|
-
expect( result.current ).toBe( false );
|
|
18
|
-
} );
|
|
19
|
-
|
|
20
|
-
it( 'should return true when a route is active by default', () => {
|
|
21
|
-
// Arrange.
|
|
22
|
-
const route = 'panel/menu';
|
|
23
|
-
|
|
24
|
-
mockIsRouteActive( () => true );
|
|
25
|
-
|
|
26
|
-
// Act.
|
|
27
|
-
const { result } = renderHook( () => useIsRouteActive( route ) );
|
|
28
|
-
|
|
29
|
-
// Assert.
|
|
30
|
-
expect( result.current ).toBe( true );
|
|
31
|
-
} );
|
|
32
|
-
|
|
33
|
-
it( 'should return true when a route gets activated', () => {
|
|
34
|
-
// Arrange.
|
|
35
|
-
const route = 'panel/menu';
|
|
36
|
-
|
|
37
|
-
mockIsRouteActive( () => false );
|
|
38
|
-
|
|
39
|
-
// Act.
|
|
40
|
-
const { result } = renderHook( () => useIsRouteActive( route ) );
|
|
41
|
-
|
|
42
|
-
act( () => {
|
|
43
|
-
mockIsRouteActive( () => true );
|
|
44
|
-
dispatchRouteOpen( route );
|
|
45
|
-
} );
|
|
46
|
-
|
|
47
|
-
// Assert.
|
|
48
|
-
expect( result.current ).toBe( true );
|
|
49
|
-
} );
|
|
50
|
-
|
|
51
|
-
it( 'should return false when a route gets deactivated', () => {
|
|
52
|
-
// Arrange.
|
|
53
|
-
const route = 'panel/menu';
|
|
54
|
-
|
|
55
|
-
mockIsRouteActive( () => true );
|
|
56
|
-
|
|
57
|
-
// Act.
|
|
58
|
-
const { result } = renderHook( () => useIsRouteActive( route ) );
|
|
59
|
-
|
|
60
|
-
act( () => {
|
|
61
|
-
mockIsRouteActive( () => false );
|
|
62
|
-
dispatchRouteClose( route );
|
|
63
|
-
} );
|
|
64
|
-
|
|
65
|
-
// Assert.
|
|
66
|
-
expect( result.current ).toBe( false );
|
|
67
|
-
} );
|
|
68
|
-
|
|
69
|
-
it( 'should re-check whether the route is active when changing it', () => {
|
|
70
|
-
// Arrange.
|
|
71
|
-
mockIsRouteActive( ( r ) => {
|
|
72
|
-
return 'active/route' === r;
|
|
73
|
-
} );
|
|
74
|
-
|
|
75
|
-
// Act.
|
|
76
|
-
const { result, rerender } = renderHook( ( { route } ) => useIsRouteActive( route ), {
|
|
77
|
-
initialProps: {
|
|
78
|
-
route: 'active/route',
|
|
79
|
-
},
|
|
80
|
-
} );
|
|
81
|
-
|
|
82
|
-
// Assert.
|
|
83
|
-
expect( result.current ).toBe( true );
|
|
84
|
-
|
|
85
|
-
// Act.
|
|
86
|
-
rerender( {
|
|
87
|
-
route: 'inactive/route',
|
|
88
|
-
} );
|
|
89
|
-
|
|
90
|
-
// Assert.
|
|
91
|
-
expect( result.current ).toBe( false );
|
|
92
|
-
} );
|
|
93
|
-
} );
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { renderHook } from '@testing-library/react';
|
|
2
|
-
import useRouteStatus from '../use-route-status';
|
|
3
|
-
import { mockGetCurrentEditMode, mockIsRouteActive } from './test-utils';
|
|
4
|
-
|
|
5
|
-
describe( '@elementor/editor-v1-adapters - useRouteStatus', () => {
|
|
6
|
-
it.each( [
|
|
7
|
-
{
|
|
8
|
-
input: {
|
|
9
|
-
isRouteActive: true,
|
|
10
|
-
isKitRouteActive: false,
|
|
11
|
-
isPreviewMode: false,
|
|
12
|
-
options: {},
|
|
13
|
-
},
|
|
14
|
-
expected: true,
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
input: {
|
|
18
|
-
isRouteActive: true,
|
|
19
|
-
isKitRouteActive: false,
|
|
20
|
-
isPreviewMode: true,
|
|
21
|
-
options: {
|
|
22
|
-
blockOnPreviewMode: false,
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
expected: true,
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
input: {
|
|
29
|
-
isRouteActive: false,
|
|
30
|
-
isKitRouteActive: false,
|
|
31
|
-
isPreviewMode: false,
|
|
32
|
-
options: {},
|
|
33
|
-
},
|
|
34
|
-
expected: false,
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
input: {
|
|
38
|
-
isRouteActive: true,
|
|
39
|
-
isKitRouteActive: false,
|
|
40
|
-
isPreviewMode: true,
|
|
41
|
-
options: {},
|
|
42
|
-
},
|
|
43
|
-
expected: false,
|
|
44
|
-
},
|
|
45
|
-
] )(
|
|
46
|
-
'should check if the route is active: isRouteActive = $input.isRouteActive, isPreviewMode = $input.isPreviewMode, options: $input.options',
|
|
47
|
-
( { input: { isRouteActive, isKitRouteActive, isPreviewMode, options }, expected } ) => {
|
|
48
|
-
// Arrange
|
|
49
|
-
mockIsRouteActive( ( route ) => ( route === 'panel/global' ? isKitRouteActive : isRouteActive ) );
|
|
50
|
-
mockGetCurrentEditMode( () => ( isPreviewMode ? 'preview' : 'edit' ) );
|
|
51
|
-
|
|
52
|
-
// Act.
|
|
53
|
-
const { result } = renderHook( () => useRouteStatus( 'panel/page-settings', options ) );
|
|
54
|
-
|
|
55
|
-
// Assert.
|
|
56
|
-
expect( result.current.isActive ).toBe( expected );
|
|
57
|
-
}
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
it.each( [
|
|
61
|
-
{
|
|
62
|
-
input: {
|
|
63
|
-
isRouteActive: false,
|
|
64
|
-
isKitRouteActive: false,
|
|
65
|
-
isPreviewMode: false,
|
|
66
|
-
options: {},
|
|
67
|
-
},
|
|
68
|
-
expected: false,
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
input: {
|
|
72
|
-
isRouteActive: false,
|
|
73
|
-
isKitRouteActive: false,
|
|
74
|
-
isPreviewMode: true,
|
|
75
|
-
options: {
|
|
76
|
-
blockOnPreviewMode: false,
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
expected: false,
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
input: {
|
|
83
|
-
isRouteActive: false,
|
|
84
|
-
isKitRouteActive: true,
|
|
85
|
-
isPreviewMode: false,
|
|
86
|
-
options: {
|
|
87
|
-
blockOnKitRoutes: false,
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
expected: false,
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
input: {
|
|
94
|
-
isRouteActive: false,
|
|
95
|
-
isKitRouteActive: true,
|
|
96
|
-
isPreviewMode: false,
|
|
97
|
-
options: {},
|
|
98
|
-
},
|
|
99
|
-
expected: true,
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
input: {
|
|
103
|
-
isRouteActive: false,
|
|
104
|
-
isKitRouteActive: false,
|
|
105
|
-
isPreviewMode: true,
|
|
106
|
-
options: {},
|
|
107
|
-
},
|
|
108
|
-
expected: true,
|
|
109
|
-
},
|
|
110
|
-
] )(
|
|
111
|
-
'should check if the route is blocked: isKitRouteActive = $input.isKitRouteActive, isPreviewMode = $input.isPreviewMode, options: $input.options',
|
|
112
|
-
( { input: { isRouteActive, isKitRouteActive, isPreviewMode, options }, expected } ) => {
|
|
113
|
-
// Arrange
|
|
114
|
-
mockIsRouteActive( ( route ) => ( route === 'panel/global' ? isKitRouteActive : isRouteActive ) );
|
|
115
|
-
mockGetCurrentEditMode( () => ( isPreviewMode ? 'preview' : 'edit' ) );
|
|
116
|
-
|
|
117
|
-
// Act.
|
|
118
|
-
const { result } = renderHook( () => useRouteStatus( 'panel/page-settings', options ) );
|
|
119
|
-
|
|
120
|
-
// Assert.
|
|
121
|
-
expect( result.current.isBlocked ).toBe( expected );
|
|
122
|
-
}
|
|
123
|
-
);
|
|
124
|
-
} );
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
commandEndEvent,
|
|
3
|
-
commandStartEvent,
|
|
4
|
-
dispatchReadyEvent,
|
|
5
|
-
listenTo,
|
|
6
|
-
routeOpenEvent,
|
|
7
|
-
routeCloseEvent,
|
|
8
|
-
windowEvent,
|
|
9
|
-
v1ReadyEvent,
|
|
10
|
-
setReady,
|
|
11
|
-
ExtendedWindow,
|
|
12
|
-
} from '../index';
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
dispatchCommandAfter,
|
|
16
|
-
dispatchCommandBefore,
|
|
17
|
-
dispatchRouteClose,
|
|
18
|
-
dispatchRouteOpen,
|
|
19
|
-
dispatchWindowEvent,
|
|
20
|
-
} from 'test-utils';
|
|
21
|
-
|
|
22
|
-
describe( '@elementor/editor-v1-adapters/listeners', () => {
|
|
23
|
-
beforeEach( () => {
|
|
24
|
-
jest.useFakeTimers();
|
|
25
|
-
} );
|
|
26
|
-
|
|
27
|
-
afterEach( () => {
|
|
28
|
-
jest.useRealTimers();
|
|
29
|
-
} );
|
|
30
|
-
|
|
31
|
-
it( 'should listen to command start', () => {
|
|
32
|
-
// Arrange.
|
|
33
|
-
const commandToListen = 'editor/documents/open',
|
|
34
|
-
anotherCommand = 'non/related/command',
|
|
35
|
-
callback = jest.fn();
|
|
36
|
-
|
|
37
|
-
// Act.
|
|
38
|
-
listenTo( commandStartEvent( commandToListen ), callback );
|
|
39
|
-
|
|
40
|
-
// Dispatch the command to test.
|
|
41
|
-
dispatchCommandBefore( commandToListen );
|
|
42
|
-
dispatchCommandAfter( commandToListen );
|
|
43
|
-
|
|
44
|
-
// Dispatch another command to make sure we don't listen to it.
|
|
45
|
-
dispatchCommandBefore( anotherCommand );
|
|
46
|
-
dispatchCommandAfter( anotherCommand );
|
|
47
|
-
|
|
48
|
-
// Assert.
|
|
49
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
50
|
-
expect( callback ).toHaveBeenCalledWith( {
|
|
51
|
-
type: 'command',
|
|
52
|
-
command: 'editor/documents/open',
|
|
53
|
-
originalEvent: expect.any( CustomEvent ),
|
|
54
|
-
} );
|
|
55
|
-
} );
|
|
56
|
-
|
|
57
|
-
it( 'should listen to command end', () => {
|
|
58
|
-
// Arrange.
|
|
59
|
-
const commandToListen = 'editor/documents/open',
|
|
60
|
-
anotherCommand = 'non/related/command',
|
|
61
|
-
callback = jest.fn();
|
|
62
|
-
|
|
63
|
-
// Act.
|
|
64
|
-
listenTo( commandEndEvent( commandToListen ), callback );
|
|
65
|
-
|
|
66
|
-
// Dispatch the command to test.
|
|
67
|
-
dispatchCommandBefore( commandToListen );
|
|
68
|
-
dispatchCommandAfter( commandToListen );
|
|
69
|
-
|
|
70
|
-
// Dispatch another command to make sure we don't listen to it.
|
|
71
|
-
dispatchCommandBefore( anotherCommand );
|
|
72
|
-
dispatchCommandAfter( anotherCommand );
|
|
73
|
-
|
|
74
|
-
// Assert.
|
|
75
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
76
|
-
expect( callback ).toHaveBeenCalledWith( {
|
|
77
|
-
type: 'command',
|
|
78
|
-
command: 'editor/documents/open',
|
|
79
|
-
originalEvent: expect.any( CustomEvent ),
|
|
80
|
-
} );
|
|
81
|
-
} );
|
|
82
|
-
|
|
83
|
-
it( 'should listen to route open', () => {
|
|
84
|
-
// Arrange.
|
|
85
|
-
const routeToListen = 'panel/menu',
|
|
86
|
-
anotherRoute = 'non/related/route',
|
|
87
|
-
callback = jest.fn();
|
|
88
|
-
|
|
89
|
-
// Act.
|
|
90
|
-
listenTo( routeOpenEvent( routeToListen ), callback );
|
|
91
|
-
|
|
92
|
-
// Dispatch the route to test.
|
|
93
|
-
dispatchRouteOpen( routeToListen );
|
|
94
|
-
|
|
95
|
-
// Dispatch another route to make sure we don't listen to it.
|
|
96
|
-
dispatchRouteOpen( anotherRoute );
|
|
97
|
-
|
|
98
|
-
// Assert.
|
|
99
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
100
|
-
expect( callback ).toHaveBeenCalledWith( {
|
|
101
|
-
type: 'route',
|
|
102
|
-
route: 'panel/menu',
|
|
103
|
-
originalEvent: expect.any( CustomEvent ),
|
|
104
|
-
} );
|
|
105
|
-
} );
|
|
106
|
-
|
|
107
|
-
it( 'should listen to route close', () => {
|
|
108
|
-
// Arrange.
|
|
109
|
-
const routeToListen = 'panel/menu',
|
|
110
|
-
anotherRoute = 'non/related/route',
|
|
111
|
-
callback = jest.fn();
|
|
112
|
-
|
|
113
|
-
// Act.
|
|
114
|
-
listenTo( routeCloseEvent( routeToListen ), callback );
|
|
115
|
-
|
|
116
|
-
// Dispatch the route to test.
|
|
117
|
-
dispatchRouteClose( routeToListen );
|
|
118
|
-
|
|
119
|
-
// Dispatch another route to make sure we don't listen to it.
|
|
120
|
-
dispatchRouteClose( anotherRoute );
|
|
121
|
-
|
|
122
|
-
// Assert.
|
|
123
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
124
|
-
expect( callback ).toHaveBeenCalledWith( {
|
|
125
|
-
type: 'route',
|
|
126
|
-
route: 'panel/menu',
|
|
127
|
-
originalEvent: expect.any( CustomEvent ),
|
|
128
|
-
} );
|
|
129
|
-
} );
|
|
130
|
-
|
|
131
|
-
it( 'should listen to window events', () => {
|
|
132
|
-
// Arrange.
|
|
133
|
-
const event = 'test-event',
|
|
134
|
-
callback = jest.fn();
|
|
135
|
-
|
|
136
|
-
// Act.
|
|
137
|
-
listenTo( windowEvent( event ), callback );
|
|
138
|
-
|
|
139
|
-
// Dispatch events.
|
|
140
|
-
dispatchWindowEvent( event );
|
|
141
|
-
|
|
142
|
-
// Assert.
|
|
143
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
144
|
-
expect( callback ).toHaveBeenCalledWith( {
|
|
145
|
-
type: 'window-event',
|
|
146
|
-
event: 'test-event',
|
|
147
|
-
originalEvent: expect.any( Event ),
|
|
148
|
-
} );
|
|
149
|
-
} );
|
|
150
|
-
|
|
151
|
-
it( 'should listen to the same event with multiple callbacks', () => {
|
|
152
|
-
// Arrange.
|
|
153
|
-
const event = 'test-event',
|
|
154
|
-
callback1 = jest.fn(),
|
|
155
|
-
callback2 = jest.fn();
|
|
156
|
-
|
|
157
|
-
// Act.
|
|
158
|
-
listenTo( windowEvent( event ), callback1 );
|
|
159
|
-
|
|
160
|
-
listenTo( windowEvent( event ), callback2 );
|
|
161
|
-
|
|
162
|
-
// Dispatch events.
|
|
163
|
-
dispatchWindowEvent( event );
|
|
164
|
-
|
|
165
|
-
// Assert.
|
|
166
|
-
expect( callback1 ).toHaveBeenCalledTimes( 1 );
|
|
167
|
-
expect( callback2 ).toHaveBeenCalledTimes( 1 );
|
|
168
|
-
} );
|
|
169
|
-
|
|
170
|
-
it( 'should listen to an array of events', () => {
|
|
171
|
-
// Arrange.
|
|
172
|
-
const command = 'test-command',
|
|
173
|
-
route = 'test-route',
|
|
174
|
-
event = 'test-event',
|
|
175
|
-
callback = jest.fn();
|
|
176
|
-
|
|
177
|
-
// Act.
|
|
178
|
-
listenTo( [ windowEvent( event ), commandStartEvent( command ), routeOpenEvent( route ) ], callback );
|
|
179
|
-
|
|
180
|
-
// Dispatch events.
|
|
181
|
-
dispatchCommandBefore( command );
|
|
182
|
-
dispatchCommandAfter( command );
|
|
183
|
-
|
|
184
|
-
dispatchRouteOpen( route );
|
|
185
|
-
dispatchRouteClose( route );
|
|
186
|
-
|
|
187
|
-
dispatchWindowEvent( event );
|
|
188
|
-
|
|
189
|
-
// Assert.
|
|
190
|
-
expect( callback ).toHaveBeenCalledTimes( 3 );
|
|
191
|
-
|
|
192
|
-
expect( callback ).toHaveBeenNthCalledWith( 1, {
|
|
193
|
-
type: 'command',
|
|
194
|
-
command: 'test-command',
|
|
195
|
-
originalEvent: expect.any( CustomEvent ),
|
|
196
|
-
} );
|
|
197
|
-
|
|
198
|
-
expect( callback ).toHaveBeenNthCalledWith( 2, {
|
|
199
|
-
type: 'route',
|
|
200
|
-
route: 'test-route',
|
|
201
|
-
originalEvent: expect.any( CustomEvent ),
|
|
202
|
-
} );
|
|
203
|
-
|
|
204
|
-
expect( callback ).toHaveBeenNthCalledWith( 3, {
|
|
205
|
-
type: 'window-event',
|
|
206
|
-
event: 'test-event',
|
|
207
|
-
originalEvent: expect.any( Event ),
|
|
208
|
-
} );
|
|
209
|
-
} );
|
|
210
|
-
|
|
211
|
-
it( 'should cleanup listeners', () => {
|
|
212
|
-
// Arrange.
|
|
213
|
-
const event1 = 'test-event-1',
|
|
214
|
-
event2 = 'test-event-2',
|
|
215
|
-
callback1 = jest.fn(),
|
|
216
|
-
callback2 = jest.fn();
|
|
217
|
-
|
|
218
|
-
const cleanup1 = listenTo( [ windowEvent( event1 ), windowEvent( event2 ) ], callback1 );
|
|
219
|
-
|
|
220
|
-
const cleanup2 = listenTo( windowEvent( event1 ), callback2 );
|
|
221
|
-
|
|
222
|
-
// Act.
|
|
223
|
-
cleanup1();
|
|
224
|
-
cleanup2();
|
|
225
|
-
|
|
226
|
-
// Dispatch events.
|
|
227
|
-
dispatchWindowEvent( event1 );
|
|
228
|
-
dispatchWindowEvent( event2 );
|
|
229
|
-
|
|
230
|
-
// Assert.
|
|
231
|
-
expect( callback1 ).toHaveBeenCalledTimes( 0 );
|
|
232
|
-
expect( callback2 ).toHaveBeenCalledTimes( 0 );
|
|
233
|
-
} );
|
|
234
|
-
|
|
235
|
-
it( 'should not fail when calling the same cleanup twice', () => {
|
|
236
|
-
// Arrange.
|
|
237
|
-
const event = 'test-event',
|
|
238
|
-
callback = jest.fn();
|
|
239
|
-
|
|
240
|
-
const cleanup = listenTo( windowEvent( event ), callback );
|
|
241
|
-
|
|
242
|
-
// Act.
|
|
243
|
-
cleanup();
|
|
244
|
-
cleanup();
|
|
245
|
-
|
|
246
|
-
// Dispatch events.
|
|
247
|
-
dispatchWindowEvent( event );
|
|
248
|
-
|
|
249
|
-
// Assert.
|
|
250
|
-
expect( callback ).toHaveBeenCalledTimes( 0 );
|
|
251
|
-
} );
|
|
252
|
-
|
|
253
|
-
it( 'should trigger v1 ready when v1 is loaded after v2', async () => {
|
|
254
|
-
// Arrange.
|
|
255
|
-
const callback = jest.fn();
|
|
256
|
-
const extendedWindow = window as unknown as ExtendedWindow;
|
|
257
|
-
|
|
258
|
-
extendedWindow.__elementorEditorV1LoadingPromise = new Promise( ( resolve ) => {
|
|
259
|
-
setTimeout( resolve, 1000 );
|
|
260
|
-
} );
|
|
261
|
-
|
|
262
|
-
// Act.
|
|
263
|
-
listenTo( v1ReadyEvent(), callback );
|
|
264
|
-
|
|
265
|
-
dispatchReadyEvent();
|
|
266
|
-
|
|
267
|
-
await jest.runAllTimers();
|
|
268
|
-
|
|
269
|
-
// Assert.
|
|
270
|
-
expect( callback ).toHaveBeenCalledTimes( 1 );
|
|
271
|
-
} );
|
|
272
|
-
|
|
273
|
-
it( 'should not trigger callback when the application is not ready', () => {
|
|
274
|
-
// Arrange
|
|
275
|
-
setReady( false );
|
|
276
|
-
|
|
277
|
-
const event = 'test-event';
|
|
278
|
-
const callback = jest.fn();
|
|
279
|
-
|
|
280
|
-
// Act.
|
|
281
|
-
listenTo( windowEvent( event ), callback );
|
|
282
|
-
|
|
283
|
-
// Dispatch events.
|
|
284
|
-
dispatchWindowEvent( event );
|
|
285
|
-
|
|
286
|
-
// Assert.
|
|
287
|
-
expect( callback ).not.toHaveBeenCalled();
|
|
288
|
-
} );
|
|
289
|
-
|
|
290
|
-
it( 'should throw when v1 is not loaded', async () => {
|
|
291
|
-
// Act & Assert.
|
|
292
|
-
try {
|
|
293
|
-
await dispatchReadyEvent();
|
|
294
|
-
} catch ( e ) {
|
|
295
|
-
expect( e ).toBe( 'Elementor Editor V1 is not loaded' );
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
expect.assertions( 1 );
|
|
299
|
-
} );
|
|
300
|
-
} );
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { isRouteActive, getCurrentEditMode } from '../index';
|
|
2
|
-
|
|
3
|
-
type ExtendedWindow = Window & {
|
|
4
|
-
$e: {
|
|
5
|
-
routes: {
|
|
6
|
-
isPartOf: jest.Mock;
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
elementor: {
|
|
10
|
-
channels: {
|
|
11
|
-
dataEditMode: {
|
|
12
|
-
request: jest.Mock;
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
describe( '@elementor/editor-v1-adapters - Readers', () => {
|
|
19
|
-
let eIsPartOf: jest.Mock, eGetEditMode: jest.Mock;
|
|
20
|
-
|
|
21
|
-
beforeEach( () => {
|
|
22
|
-
const extendedWindow = window as unknown as ExtendedWindow;
|
|
23
|
-
|
|
24
|
-
extendedWindow.$e = {
|
|
25
|
-
routes: {
|
|
26
|
-
isPartOf: jest.fn(),
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
extendedWindow.elementor = {
|
|
31
|
-
channels: {
|
|
32
|
-
dataEditMode: {
|
|
33
|
-
request: jest.fn(),
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
eIsPartOf = extendedWindow.$e.routes.isPartOf;
|
|
39
|
-
eGetEditMode = extendedWindow.elementor.channels.dataEditMode.request;
|
|
40
|
-
} );
|
|
41
|
-
|
|
42
|
-
it( 'should determine if a route is active', () => {
|
|
43
|
-
// Arrange.
|
|
44
|
-
const route = 'test/route';
|
|
45
|
-
|
|
46
|
-
eIsPartOf.mockReturnValue( true );
|
|
47
|
-
|
|
48
|
-
// Act.
|
|
49
|
-
const result = isRouteActive( route );
|
|
50
|
-
|
|
51
|
-
// Assert.
|
|
52
|
-
expect( result ).toEqual( true );
|
|
53
|
-
expect( eIsPartOf ).toHaveBeenCalledTimes( 1 );
|
|
54
|
-
expect( eIsPartOf ).toHaveBeenCalledWith( route );
|
|
55
|
-
} );
|
|
56
|
-
|
|
57
|
-
it( 'should return the current edit mode', () => {
|
|
58
|
-
// Arrange.
|
|
59
|
-
const editMode = 'edit';
|
|
60
|
-
|
|
61
|
-
eGetEditMode.mockReturnValue( editMode );
|
|
62
|
-
|
|
63
|
-
// Act.
|
|
64
|
-
const result = getCurrentEditMode();
|
|
65
|
-
|
|
66
|
-
// Assert.
|
|
67
|
-
expect( result ).toEqual( editMode );
|
|
68
|
-
expect( eGetEditMode ).toHaveBeenCalledTimes( 1 );
|
|
69
|
-
} );
|
|
70
|
-
} );
|