@elementor/editor-responsive 0.10.5 → 0.11.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.
@@ -1,158 +0,0 @@
1
- import syncStore from '../sync-store';
2
- import { slice } from '../../store';
3
- import { BreakpointId, ExtendedWindow } from '../../types';
4
- import { __createStore, __dispatch, __registerSlice, SliceState, Store } from '@elementor/store';
5
- import { selectActiveBreakpoint, selectEntities } from '../../store/selectors';
6
-
7
- describe( '@elementor/editor-responsive - Sync Store', () => {
8
- let store: Store<SliceState<typeof slice>>;
9
- let extendedWindow: ExtendedWindow;
10
-
11
- beforeEach( () => {
12
- __registerSlice( slice );
13
- store = __createStore();
14
-
15
- syncStore();
16
-
17
- extendedWindow = ( window as unknown as ExtendedWindow );
18
- } );
19
-
20
- it( 'should initialize the store when V1 is ready', () => {
21
- // Arrange.
22
- mockV1BreakpointsConfig();
23
-
24
- // Act.
25
- dispatchEvent( new CustomEvent( 'elementor/initialized' ) );
26
-
27
- // Assert.
28
- expect( selectEntities( store.getState() ) ).toEqual( {
29
- desktop: { id: 'desktop', label: 'Desktop' },
30
- mobile: { id: 'mobile', label: 'Mobile Portrait', width: 767, type: 'max-width' },
31
- tablet: { id: 'tablet', label: 'Tablet Portrait', width: 1024, type: 'max-width' },
32
- laptop: { id: 'laptop', label: 'Laptop', width: 1366, type: 'max-width' },
33
- widescreen: { id: 'widescreen', label: 'Widescreen', width: 2400, type: 'min-width' },
34
- mobile_extra: { id: 'mobile_extra', label: 'Mobile Landscape', width: 880, type: 'max-width' },
35
- tablet_extra: { id: 'tablet_extra', label: 'Tablet Landscape', width: 1200, type: 'max-width' },
36
- } );
37
-
38
- expect( extendedWindow.elementor.channels.deviceMode.request ).toHaveBeenCalledTimes( 1 );
39
- expect( extendedWindow.elementor.channels.deviceMode.request ).toHaveBeenCalledWith( 'currentMode' );
40
-
41
- expect( selectActiveBreakpoint( store.getState() ) ).toEqual( {
42
- id: 'mobile',
43
- label: 'Mobile Portrait',
44
- width: 767,
45
- type: 'max-width',
46
- } );
47
- } );
48
-
49
- it( 'should initialize an empty store when V1 breakpoints config is not available', () => {
50
- // Act.
51
- dispatchEvent( new CustomEvent( 'elementor/initialized' ) );
52
-
53
- // Assert.
54
- expect( selectEntities( store.getState() ) ).toEqual( {} );
55
- } );
56
-
57
- it( 'should sync the active breakpoint on change', () => {
58
- // Arrange.
59
- mockV1BreakpointsConfig();
60
-
61
- __dispatch( slice.actions.init( {
62
- entities: [
63
- { id: 'desktop', label: 'Desktop' },
64
- { id: 'mobile', label: 'Mobile Portrait', width: 767, type: 'max-width' },
65
- ],
66
- activeId: 'mobile',
67
- } ) );
68
-
69
- // Act - Mock a change.
70
- jest.mocked( extendedWindow.elementor.channels.deviceMode.request ).mockReturnValue( 'desktop' );
71
- dispatchEvent( new CustomEvent( 'elementor/device-mode/change' ) );
72
-
73
- // Assert.
74
- expect( selectActiveBreakpoint( store.getState() ) ).toEqual( { id: 'desktop', label: 'Desktop' } );
75
- } );
76
-
77
- it( "should not change the active breakpoint when it's empty", () => {
78
- // Arrange.
79
- mockV1BreakpointsConfig();
80
-
81
- __dispatch( slice.actions.init( {
82
- entities: [
83
- { id: 'desktop', label: 'Desktop' },
84
- { id: 'mobile', label: 'Mobile Portrait', width: 767, type: 'max-width' },
85
- ],
86
- activeId: 'desktop',
87
- } ) );
88
-
89
- // Act - Mock a change.
90
- jest.mocked( extendedWindow.elementor.channels.deviceMode.request ).mockReturnValue( '' as BreakpointId );
91
- dispatchEvent( new CustomEvent( 'elementor/device-mode/change' ) );
92
-
93
- // Assert.
94
- expect( selectActiveBreakpoint( store.getState() ) ).toEqual( { id: 'desktop', label: 'Desktop' } );
95
- } );
96
- } );
97
-
98
- function mockV1BreakpointsConfig() {
99
- ( window as unknown as ExtendedWindow ).elementor = {
100
- channels: {
101
- deviceMode: { request: jest.fn( () => 'mobile' ) },
102
- },
103
- config: {
104
- responsive: {
105
- breakpoints: {
106
- mobile: {
107
- label: 'Mobile Portrait',
108
- value: 767,
109
- direction: 'max',
110
- is_enabled: true,
111
- },
112
- mobile_extra: {
113
- label: 'Mobile Landscape',
114
- value: 880,
115
- direction: 'max',
116
- is_enabled: true,
117
- },
118
- tablet: {
119
- label: 'Tablet Portrait',
120
- value: 1024,
121
- direction: 'max',
122
- is_enabled: true,
123
- },
124
- tablet_extra: {
125
- label: 'Tablet Landscape',
126
- value: 1200,
127
- direction: 'max',
128
- is_enabled: true,
129
- },
130
- laptop: {
131
- label: 'Laptop',
132
- value: 1366,
133
- direction: 'max',
134
- is_enabled: true,
135
- },
136
- widescreen: {
137
- label: 'Widescreen',
138
- value: 2400,
139
- direction: 'min',
140
- is_enabled: true,
141
- },
142
- } as const,
143
- },
144
- },
145
- editorEvents: {
146
- config: {
147
- elements: {},
148
- locations: {},
149
- names: {
150
- topBar: {},
151
- },
152
- secondaryLocations: {},
153
- triggers: {},
154
- },
155
- dispatchEvent: () => null,
156
- },
157
- };
158
- }
@@ -1,75 +0,0 @@
1
- import { slice } from '../store';
2
- import { __dispatch } from '@elementor/store';
3
- import { Breakpoint, ExtendedWindow } from '../types';
4
- import { __privateListenTo as listenTo, v1ReadyEvent, windowEvent } from '@elementor/editor-v1-adapters';
5
- import { __ } from '@wordpress/i18n';
6
-
7
- export default function syncStore() {
8
- syncInitialization();
9
- syncOnChange();
10
- }
11
-
12
- function syncInitialization() {
13
- const { init } = slice.actions;
14
-
15
- listenTo(
16
- v1ReadyEvent(),
17
- () => {
18
- __dispatch( init( {
19
- entities: getBreakpoints(),
20
- activeId: getActiveBreakpoint(),
21
- } ) );
22
- }
23
- );
24
- }
25
-
26
- function syncOnChange() {
27
- const { activateBreakpoint } = slice.actions;
28
-
29
- listenTo(
30
- deviceModeChangeEvent(),
31
- () => {
32
- const activeBreakpoint = getActiveBreakpoint();
33
-
34
- __dispatch( activateBreakpoint( activeBreakpoint ) );
35
- },
36
- );
37
- }
38
-
39
- function getBreakpoints() {
40
- const { breakpoints } = ( window as unknown as ExtendedWindow ).elementor?.config?.responsive || {};
41
-
42
- if ( ! breakpoints ) {
43
- return [];
44
- }
45
-
46
- const entities = Object
47
- .entries( breakpoints )
48
- .filter( ( [ , breakpoint ] ) => breakpoint.is_enabled )
49
- .map( ( [ id, { value, direction, label } ] ) => {
50
- return {
51
- id,
52
- label,
53
- width: value,
54
- type: direction === 'min' ? 'min-width' : 'max-width',
55
- } as Breakpoint;
56
- } );
57
-
58
- // Desktop breakpoint is not included in V1 config.
59
- entities.push( {
60
- id: 'desktop',
61
- label: __( 'Desktop', 'elementor' ),
62
- } );
63
-
64
- return entities;
65
- }
66
-
67
- function getActiveBreakpoint() {
68
- const extendedWindow = window as unknown as ExtendedWindow;
69
-
70
- return extendedWindow.elementor?.channels?.deviceMode?.request?.( 'currentMode' ) || null;
71
- }
72
-
73
- function deviceModeChangeEvent() {
74
- return windowEvent( 'elementor/device-mode/change' );
75
- }