@wordpress/views 1.18.0 → 1.20.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.
- package/CHANGELOG.md +13 -0
- package/README.md +5 -0
- package/build/load-view.cjs +21 -21
- package/build/load-view.cjs.map +2 -2
- package/build/lock-unlock.cjs.map +2 -2
- package/build/resolve-view.cjs +133 -0
- package/build/resolve-view.cjs.map +7 -0
- package/build/types.cjs.map +1 -1
- package/build/use-view-config.cjs.map +2 -2
- package/build/use-view.cjs +37 -57
- package/build/use-view.cjs.map +2 -2
- package/build-module/load-view.mjs +21 -21
- package/build-module/load-view.mjs.map +2 -2
- package/build-module/lock-unlock.mjs.map +2 -2
- package/build-module/resolve-view.mjs +107 -0
- package/build-module/resolve-view.mjs.map +7 -0
- package/build-module/use-view-config.mjs.map +2 -2
- package/build-module/use-view.mjs +37 -60
- package/build-module/use-view.mjs.map +2 -2
- package/build-types/load-view.d.ts +5 -2
- package/build-types/load-view.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts.map +1 -1
- package/build-types/resolve-view.d.ts +54 -0
- package/build-types/resolve-view.d.ts.map +1 -0
- package/build-types/types.d.ts +19 -21
- package/build-types/types.d.ts.map +1 -1
- package/build-types/use-view-config.d.ts.map +1 -1
- package/build-types/use-view.d.ts +5 -0
- package/build-types/use-view.d.ts.map +1 -1
- package/package.json +13 -8
- package/src/load-view.ts +26 -38
- package/src/lock-unlock.ts +0 -3
- package/src/resolve-view.ts +261 -0
- package/src/test/resolve-view.ts +63 -0
- package/src/test/use-view.tsx +966 -0
- package/src/types.ts +20 -24
- package/src/use-view-config.ts +0 -7
- package/src/use-view.ts +50 -92
- package/build/filter-utils.cjs +0 -134
- package/build/filter-utils.cjs.map +0 -7
- package/build-module/filter-utils.mjs +0 -108
- package/build-module/filter-utils.mjs.map +0 -7
- package/build-types/filter-utils.d.ts +0 -31
- package/build-types/filter-utils.d.ts.map +0 -1
- package/src/filter-utils.ts +0 -180
- package/src/test/filter-utils.ts +0 -522
package/src/filter-utils.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WordPress dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { View, Filter } from '@wordpress/dataviews';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Internal dependencies
|
|
8
|
-
*/
|
|
9
|
-
import type { ActiveViewOverrides } from './types';
|
|
10
|
-
|
|
11
|
-
const SCALAR_VALUES = [
|
|
12
|
-
'titleField',
|
|
13
|
-
'mediaField',
|
|
14
|
-
'descriptionField',
|
|
15
|
-
'showTitle',
|
|
16
|
-
'showMedia',
|
|
17
|
-
'showDescription',
|
|
18
|
-
'showLevels',
|
|
19
|
-
'infiniteScrollEnabled',
|
|
20
|
-
] as const;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Merges activeViewOverrides into a view.
|
|
24
|
-
* Filters: Active filters take precedence; same-field filters are replaced.
|
|
25
|
-
* Sort: Active sort is applied only if current sort matches the default.
|
|
26
|
-
*
|
|
27
|
-
* @param view The view to merge overrides into.
|
|
28
|
-
* @param activeViewOverrides The tab-specific overrides to apply.
|
|
29
|
-
* @param defaultView The default view configuration.
|
|
30
|
-
* @return A new view with merged overrides, or the original view if no overrides.
|
|
31
|
-
*/
|
|
32
|
-
export function mergeActiveViewOverrides(
|
|
33
|
-
view: View,
|
|
34
|
-
activeViewOverrides?: ActiveViewOverrides,
|
|
35
|
-
defaultView?: View
|
|
36
|
-
): View {
|
|
37
|
-
if ( ! activeViewOverrides ) {
|
|
38
|
-
return view;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let result = view;
|
|
42
|
-
|
|
43
|
-
// Merge scalar overrides — always win over persisted values
|
|
44
|
-
for ( const key of SCALAR_VALUES ) {
|
|
45
|
-
if ( key in activeViewOverrides ) {
|
|
46
|
-
result = { ...result, [ key ]: activeViewOverrides[ key ] };
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Merge filters
|
|
51
|
-
if (
|
|
52
|
-
activeViewOverrides.filters &&
|
|
53
|
-
activeViewOverrides.filters.length > 0
|
|
54
|
-
) {
|
|
55
|
-
const activeFields = new Set(
|
|
56
|
-
activeViewOverrides.filters.map( ( f ) => f.field )
|
|
57
|
-
);
|
|
58
|
-
const preserved = ( view.filters ?? [] ).filter(
|
|
59
|
-
( f: Filter ) => ! activeFields.has( f.field )
|
|
60
|
-
);
|
|
61
|
-
result = {
|
|
62
|
-
...result,
|
|
63
|
-
filters: [ ...preserved, ...activeViewOverrides.filters ],
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Merge sort - only apply if the current sort matches the default
|
|
68
|
-
if ( activeViewOverrides.sort ) {
|
|
69
|
-
const isDefaultSort =
|
|
70
|
-
defaultView &&
|
|
71
|
-
view.sort?.field === defaultView.sort?.field &&
|
|
72
|
-
view.sort?.direction === defaultView.sort?.direction;
|
|
73
|
-
|
|
74
|
-
if ( isDefaultSort ) {
|
|
75
|
-
result = {
|
|
76
|
-
...result,
|
|
77
|
-
sort: activeViewOverrides.sort,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Merge layout — shallow merge, override keys always win
|
|
83
|
-
if ( activeViewOverrides.layout ) {
|
|
84
|
-
result = {
|
|
85
|
-
...result,
|
|
86
|
-
layout: {
|
|
87
|
-
...( result as any ).layout,
|
|
88
|
-
...activeViewOverrides.layout,
|
|
89
|
-
},
|
|
90
|
-
} as View;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Merge groupBy — full replacement, override always wins
|
|
94
|
-
if ( activeViewOverrides.groupBy ) {
|
|
95
|
-
result = {
|
|
96
|
-
...result,
|
|
97
|
-
groupBy: activeViewOverrides.groupBy,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Strips overrides before persisting.
|
|
106
|
-
* Filters: Removes filters on fields managed by activeViewOverrides.
|
|
107
|
-
* Sort: If sort matches the override, restores the default sort.
|
|
108
|
-
*
|
|
109
|
-
* @param view The view to strip overrides from.
|
|
110
|
-
* @param activeViewOverrides The tab-specific override definitions.
|
|
111
|
-
* @param defaultView The default view configuration.
|
|
112
|
-
* @return A new view with overrides stripped, or the original view if no overrides.
|
|
113
|
-
*/
|
|
114
|
-
export function stripActiveViewOverrides(
|
|
115
|
-
view: View,
|
|
116
|
-
activeViewOverrides?: ActiveViewOverrides,
|
|
117
|
-
defaultView?: View
|
|
118
|
-
): View {
|
|
119
|
-
if ( ! activeViewOverrides ) {
|
|
120
|
-
return view;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
let result = view;
|
|
124
|
-
|
|
125
|
-
// Strip scalar keys managed by overrides
|
|
126
|
-
for ( const key of SCALAR_VALUES ) {
|
|
127
|
-
if ( key in activeViewOverrides ) {
|
|
128
|
-
const { [ key ]: _, ...rest } = result;
|
|
129
|
-
result = rest as View;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Strip managed filters
|
|
134
|
-
if (
|
|
135
|
-
activeViewOverrides.filters &&
|
|
136
|
-
activeViewOverrides.filters.length > 0
|
|
137
|
-
) {
|
|
138
|
-
const activeFields = new Set(
|
|
139
|
-
activeViewOverrides.filters.map( ( f ) => f.field )
|
|
140
|
-
);
|
|
141
|
-
result = {
|
|
142
|
-
...result,
|
|
143
|
-
filters: ( view.filters ?? [] ).filter(
|
|
144
|
-
( f: Filter ) => ! activeFields.has( f.field )
|
|
145
|
-
),
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Strip sort if it matches the override (restore to default)
|
|
150
|
-
if (
|
|
151
|
-
activeViewOverrides.sort &&
|
|
152
|
-
view.sort?.field === activeViewOverrides.sort.field &&
|
|
153
|
-
view.sort?.direction === activeViewOverrides.sort.direction
|
|
154
|
-
) {
|
|
155
|
-
result = {
|
|
156
|
-
...result,
|
|
157
|
-
sort: defaultView?.sort,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Strip layout keys managed by overrides
|
|
162
|
-
if ( activeViewOverrides.layout && 'layout' in result && result.layout ) {
|
|
163
|
-
const layout = { ...result.layout } as Record< string, unknown >;
|
|
164
|
-
for ( const key of Object.keys( activeViewOverrides.layout ) ) {
|
|
165
|
-
delete layout[ key ];
|
|
166
|
-
}
|
|
167
|
-
result = {
|
|
168
|
-
...result,
|
|
169
|
-
layout: Object.keys( layout ).length > 0 ? layout : undefined,
|
|
170
|
-
} as View;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Strip groupBy managed by overrides
|
|
174
|
-
if ( activeViewOverrides.groupBy && 'groupBy' in result ) {
|
|
175
|
-
const { groupBy: _, ...rest } = result;
|
|
176
|
-
result = rest as View;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return result;
|
|
180
|
-
}
|
package/src/test/filter-utils.ts
DELETED
|
@@ -1,522 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WordPress dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { View } from '@wordpress/dataviews';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Internal dependencies
|
|
8
|
-
*/
|
|
9
|
-
import {
|
|
10
|
-
mergeActiveViewOverrides,
|
|
11
|
-
stripActiveViewOverrides,
|
|
12
|
-
} from '../filter-utils';
|
|
13
|
-
|
|
14
|
-
const baseView: View = {
|
|
15
|
-
type: 'table',
|
|
16
|
-
filters: [ { field: 'author', operator: 'isAny', value: [ 'admin' ] } ],
|
|
17
|
-
sort: { field: 'date', direction: 'desc' },
|
|
18
|
-
page: 1,
|
|
19
|
-
perPage: 25,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const defaultView: View = {
|
|
23
|
-
type: 'table',
|
|
24
|
-
sort: { field: 'date', direction: 'desc' },
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
describe( 'mergeActiveViewOverrides', () => {
|
|
28
|
-
it( 'should return the view unchanged when no overrides are provided', () => {
|
|
29
|
-
expect( mergeActiveViewOverrides( baseView ) ).toBe( baseView );
|
|
30
|
-
expect( mergeActiveViewOverrides( baseView, undefined ) ).toBe(
|
|
31
|
-
baseView
|
|
32
|
-
);
|
|
33
|
-
} );
|
|
34
|
-
|
|
35
|
-
describe( 'scalar overrides', () => {
|
|
36
|
-
it( 'should merge titleField override', () => {
|
|
37
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
38
|
-
titleField: 'name',
|
|
39
|
-
} );
|
|
40
|
-
expect( result.titleField ).toBe( 'name' );
|
|
41
|
-
} );
|
|
42
|
-
|
|
43
|
-
it( 'should merge mediaField override', () => {
|
|
44
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
45
|
-
mediaField: 'thumbnail',
|
|
46
|
-
} );
|
|
47
|
-
expect( result.mediaField ).toBe( 'thumbnail' );
|
|
48
|
-
} );
|
|
49
|
-
|
|
50
|
-
it( 'should merge descriptionField override', () => {
|
|
51
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
52
|
-
descriptionField: 'excerpt',
|
|
53
|
-
} );
|
|
54
|
-
expect( result.descriptionField ).toBe( 'excerpt' );
|
|
55
|
-
} );
|
|
56
|
-
|
|
57
|
-
it( 'should merge showTitle override', () => {
|
|
58
|
-
const result = mergeActiveViewOverrides(
|
|
59
|
-
{ ...baseView, showTitle: true },
|
|
60
|
-
{ showTitle: false }
|
|
61
|
-
);
|
|
62
|
-
expect( result.showTitle ).toBe( false );
|
|
63
|
-
} );
|
|
64
|
-
|
|
65
|
-
it( 'should merge showMedia override', () => {
|
|
66
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
67
|
-
showMedia: true,
|
|
68
|
-
} );
|
|
69
|
-
expect( result.showMedia ).toBe( true );
|
|
70
|
-
} );
|
|
71
|
-
|
|
72
|
-
it( 'should merge showDescription override', () => {
|
|
73
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
74
|
-
showDescription: false,
|
|
75
|
-
} );
|
|
76
|
-
expect( result.showDescription ).toBe( false );
|
|
77
|
-
} );
|
|
78
|
-
|
|
79
|
-
it( 'should merge showLevels override', () => {
|
|
80
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
81
|
-
showLevels: true,
|
|
82
|
-
} );
|
|
83
|
-
expect( result.showLevels ).toBe( true );
|
|
84
|
-
} );
|
|
85
|
-
|
|
86
|
-
it( 'should merge infiniteScrollEnabled override', () => {
|
|
87
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
88
|
-
infiniteScrollEnabled: true,
|
|
89
|
-
} );
|
|
90
|
-
expect( result.infiniteScrollEnabled ).toBe( true );
|
|
91
|
-
} );
|
|
92
|
-
|
|
93
|
-
it( 'should override existing scalar value on the view', () => {
|
|
94
|
-
const view: View = { ...baseView, titleField: 'old-title' };
|
|
95
|
-
const result = mergeActiveViewOverrides( view, {
|
|
96
|
-
titleField: 'new-title',
|
|
97
|
-
} );
|
|
98
|
-
expect( result.titleField ).toBe( 'new-title' );
|
|
99
|
-
} );
|
|
100
|
-
|
|
101
|
-
it( 'should merge multiple scalar overrides at once', () => {
|
|
102
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
103
|
-
titleField: 'name',
|
|
104
|
-
showTitle: true,
|
|
105
|
-
showMedia: false,
|
|
106
|
-
infiniteScrollEnabled: true,
|
|
107
|
-
} );
|
|
108
|
-
expect( result.titleField ).toBe( 'name' );
|
|
109
|
-
expect( result.showTitle ).toBe( true );
|
|
110
|
-
expect( result.showMedia ).toBe( false );
|
|
111
|
-
expect( result.infiniteScrollEnabled ).toBe( true );
|
|
112
|
-
} );
|
|
113
|
-
} );
|
|
114
|
-
|
|
115
|
-
describe( 'filter overrides', () => {
|
|
116
|
-
it( 'should add override filters', () => {
|
|
117
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
118
|
-
filters: [
|
|
119
|
-
{ field: 'status', operator: 'isAny', value: 'publish' },
|
|
120
|
-
],
|
|
121
|
-
} );
|
|
122
|
-
expect( result.filters ).toHaveLength( 2 );
|
|
123
|
-
expect( result.filters ).toEqual(
|
|
124
|
-
expect.arrayContaining( [
|
|
125
|
-
{ field: 'author', operator: 'isAny', value: [ 'admin' ] },
|
|
126
|
-
{
|
|
127
|
-
field: 'status',
|
|
128
|
-
operator: 'isAny',
|
|
129
|
-
value: 'publish',
|
|
130
|
-
},
|
|
131
|
-
] )
|
|
132
|
-
);
|
|
133
|
-
} );
|
|
134
|
-
|
|
135
|
-
it( 'should replace same-field filters', () => {
|
|
136
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
137
|
-
filters: [
|
|
138
|
-
{
|
|
139
|
-
field: 'author',
|
|
140
|
-
operator: 'isAny',
|
|
141
|
-
value: [ 'editor' ],
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
} );
|
|
145
|
-
expect( result.filters ).toHaveLength( 1 );
|
|
146
|
-
expect( result.filters![ 0 ] ).toEqual( {
|
|
147
|
-
field: 'author',
|
|
148
|
-
operator: 'isAny',
|
|
149
|
-
value: [ 'editor' ],
|
|
150
|
-
} );
|
|
151
|
-
} );
|
|
152
|
-
|
|
153
|
-
it( 'should handle empty override filters array', () => {
|
|
154
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
155
|
-
filters: [],
|
|
156
|
-
} );
|
|
157
|
-
// Empty filters array is treated as no override.
|
|
158
|
-
expect( result.filters ).toEqual( baseView.filters );
|
|
159
|
-
} );
|
|
160
|
-
} );
|
|
161
|
-
|
|
162
|
-
describe( 'sort overrides', () => {
|
|
163
|
-
it( 'should apply sort override when current sort matches default', () => {
|
|
164
|
-
const result = mergeActiveViewOverrides(
|
|
165
|
-
baseView,
|
|
166
|
-
{ sort: { field: 'title', direction: 'asc' } },
|
|
167
|
-
defaultView
|
|
168
|
-
);
|
|
169
|
-
expect( result.sort ).toEqual( {
|
|
170
|
-
field: 'title',
|
|
171
|
-
direction: 'asc',
|
|
172
|
-
} );
|
|
173
|
-
} );
|
|
174
|
-
|
|
175
|
-
it( 'should not apply sort override when user has changed sort', () => {
|
|
176
|
-
const userView: View = {
|
|
177
|
-
...baseView,
|
|
178
|
-
sort: { field: 'title', direction: 'asc' },
|
|
179
|
-
};
|
|
180
|
-
const result = mergeActiveViewOverrides(
|
|
181
|
-
userView,
|
|
182
|
-
{ sort: { field: 'modified', direction: 'desc' } },
|
|
183
|
-
defaultView
|
|
184
|
-
);
|
|
185
|
-
expect( result.sort ).toEqual( {
|
|
186
|
-
field: 'title',
|
|
187
|
-
direction: 'asc',
|
|
188
|
-
} );
|
|
189
|
-
} );
|
|
190
|
-
|
|
191
|
-
it( 'should not apply sort override when no default view is provided', () => {
|
|
192
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
193
|
-
sort: { field: 'title', direction: 'asc' },
|
|
194
|
-
} );
|
|
195
|
-
expect( result.sort ).toEqual( baseView.sort );
|
|
196
|
-
} );
|
|
197
|
-
} );
|
|
198
|
-
|
|
199
|
-
describe( 'layout overrides', () => {
|
|
200
|
-
it( 'should merge layout override into existing layout', () => {
|
|
201
|
-
const view: View = {
|
|
202
|
-
...baseView,
|
|
203
|
-
layout: { density: 'compact' },
|
|
204
|
-
};
|
|
205
|
-
const result = mergeActiveViewOverrides( view, {
|
|
206
|
-
layout: { styles: { author: { align: 'end' } } },
|
|
207
|
-
} );
|
|
208
|
-
expect( result.layout ).toEqual( {
|
|
209
|
-
density: 'compact',
|
|
210
|
-
styles: { author: { align: 'end' } },
|
|
211
|
-
} );
|
|
212
|
-
} );
|
|
213
|
-
|
|
214
|
-
it( 'should set layout when view has no existing layout', () => {
|
|
215
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
216
|
-
layout: { styles: { title: { width: '50%' } } },
|
|
217
|
-
} );
|
|
218
|
-
expect( result.layout ).toEqual( {
|
|
219
|
-
styles: { title: { width: '50%' } },
|
|
220
|
-
} );
|
|
221
|
-
} );
|
|
222
|
-
|
|
223
|
-
it( 'should override matching layout keys', () => {
|
|
224
|
-
const view: View = {
|
|
225
|
-
...baseView,
|
|
226
|
-
layout: {
|
|
227
|
-
density: 'compact',
|
|
228
|
-
styles: { old: { width: '10%' } },
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
const result = mergeActiveViewOverrides( view, {
|
|
232
|
-
layout: { styles: { new: { width: '20%' } } },
|
|
233
|
-
} );
|
|
234
|
-
// Shallow merge: styles key is replaced entirely.
|
|
235
|
-
expect( result.layout ).toEqual( {
|
|
236
|
-
density: 'compact',
|
|
237
|
-
styles: { new: { width: '20%' } },
|
|
238
|
-
} );
|
|
239
|
-
} );
|
|
240
|
-
} );
|
|
241
|
-
|
|
242
|
-
describe( 'groupBy overrides', () => {
|
|
243
|
-
it( 'should replace groupBy with override', () => {
|
|
244
|
-
const view: View = {
|
|
245
|
-
...baseView,
|
|
246
|
-
groupBy: {
|
|
247
|
-
field: 'status',
|
|
248
|
-
direction: 'asc',
|
|
249
|
-
showLabel: false,
|
|
250
|
-
},
|
|
251
|
-
};
|
|
252
|
-
const result = mergeActiveViewOverrides( view, {
|
|
253
|
-
groupBy: { field: 'category', direction: 'desc' },
|
|
254
|
-
} );
|
|
255
|
-
expect( result.groupBy ).toEqual( {
|
|
256
|
-
field: 'category',
|
|
257
|
-
direction: 'desc',
|
|
258
|
-
} );
|
|
259
|
-
} );
|
|
260
|
-
|
|
261
|
-
it( 'should set groupBy when view has none', () => {
|
|
262
|
-
const result = mergeActiveViewOverrides( baseView, {
|
|
263
|
-
groupBy: { field: 'category', direction: 'desc' },
|
|
264
|
-
} );
|
|
265
|
-
expect( result.groupBy ).toEqual( {
|
|
266
|
-
field: 'category',
|
|
267
|
-
direction: 'desc',
|
|
268
|
-
} );
|
|
269
|
-
} );
|
|
270
|
-
} );
|
|
271
|
-
|
|
272
|
-
it( 'should not mutate the original view', () => {
|
|
273
|
-
const original = { ...baseView };
|
|
274
|
-
mergeActiveViewOverrides( original, {
|
|
275
|
-
titleField: 'name',
|
|
276
|
-
filters: [
|
|
277
|
-
{ field: 'status', operator: 'isAny', value: 'publish' },
|
|
278
|
-
],
|
|
279
|
-
layout: { styles: {} },
|
|
280
|
-
} );
|
|
281
|
-
expect( original ).toEqual( baseView );
|
|
282
|
-
} );
|
|
283
|
-
} );
|
|
284
|
-
|
|
285
|
-
describe( 'stripActiveViewOverrides', () => {
|
|
286
|
-
it( 'should return the view unchanged when no overrides are provided', () => {
|
|
287
|
-
expect( stripActiveViewOverrides( baseView ) ).toBe( baseView );
|
|
288
|
-
expect( stripActiveViewOverrides( baseView, undefined ) ).toBe(
|
|
289
|
-
baseView
|
|
290
|
-
);
|
|
291
|
-
} );
|
|
292
|
-
|
|
293
|
-
describe( 'scalar stripping', () => {
|
|
294
|
-
it( 'should strip a scalar key managed by overrides', () => {
|
|
295
|
-
const view: View = { ...baseView, titleField: 'name' };
|
|
296
|
-
const result = stripActiveViewOverrides( view, {
|
|
297
|
-
titleField: 'name',
|
|
298
|
-
} );
|
|
299
|
-
expect( result ).not.toHaveProperty( 'titleField' );
|
|
300
|
-
} );
|
|
301
|
-
|
|
302
|
-
it( 'should strip multiple scalar keys', () => {
|
|
303
|
-
const view: View = {
|
|
304
|
-
...baseView,
|
|
305
|
-
titleField: 'name',
|
|
306
|
-
showTitle: true,
|
|
307
|
-
mediaField: 'thumb',
|
|
308
|
-
};
|
|
309
|
-
const result = stripActiveViewOverrides( view, {
|
|
310
|
-
titleField: 'name',
|
|
311
|
-
showTitle: true,
|
|
312
|
-
mediaField: 'thumb',
|
|
313
|
-
} );
|
|
314
|
-
expect( result ).not.toHaveProperty( 'titleField' );
|
|
315
|
-
expect( result ).not.toHaveProperty( 'showTitle' );
|
|
316
|
-
expect( result ).not.toHaveProperty( 'mediaField' );
|
|
317
|
-
} );
|
|
318
|
-
|
|
319
|
-
it( 'should preserve non-overridden scalar keys', () => {
|
|
320
|
-
const view: View = {
|
|
321
|
-
...baseView,
|
|
322
|
-
titleField: 'name',
|
|
323
|
-
descriptionField: 'excerpt',
|
|
324
|
-
};
|
|
325
|
-
const result = stripActiveViewOverrides( view, {
|
|
326
|
-
titleField: 'name',
|
|
327
|
-
} );
|
|
328
|
-
expect( result ).not.toHaveProperty( 'titleField' );
|
|
329
|
-
expect( result.descriptionField ).toBe( 'excerpt' );
|
|
330
|
-
} );
|
|
331
|
-
} );
|
|
332
|
-
|
|
333
|
-
describe( 'filter stripping', () => {
|
|
334
|
-
it( 'should remove filters on managed fields', () => {
|
|
335
|
-
const view: View = {
|
|
336
|
-
...baseView,
|
|
337
|
-
filters: [
|
|
338
|
-
{
|
|
339
|
-
field: 'status',
|
|
340
|
-
operator: 'isAny',
|
|
341
|
-
value: 'publish',
|
|
342
|
-
},
|
|
343
|
-
{
|
|
344
|
-
field: 'author',
|
|
345
|
-
operator: 'isAny',
|
|
346
|
-
value: [ 'admin' ],
|
|
347
|
-
},
|
|
348
|
-
],
|
|
349
|
-
};
|
|
350
|
-
const result = stripActiveViewOverrides( view, {
|
|
351
|
-
filters: [
|
|
352
|
-
{
|
|
353
|
-
field: 'status',
|
|
354
|
-
operator: 'isAny',
|
|
355
|
-
value: 'publish',
|
|
356
|
-
},
|
|
357
|
-
],
|
|
358
|
-
} );
|
|
359
|
-
expect( result.filters ).toHaveLength( 1 );
|
|
360
|
-
expect( result.filters?.[ 0 ].field ).toBe( 'author' );
|
|
361
|
-
} );
|
|
362
|
-
|
|
363
|
-
it( 'should handle empty override filters', () => {
|
|
364
|
-
const result = stripActiveViewOverrides( baseView, {
|
|
365
|
-
filters: [],
|
|
366
|
-
} );
|
|
367
|
-
expect( result.filters ).toEqual( baseView.filters );
|
|
368
|
-
} );
|
|
369
|
-
} );
|
|
370
|
-
|
|
371
|
-
describe( 'sort stripping', () => {
|
|
372
|
-
it( 'should restore default sort when current matches override', () => {
|
|
373
|
-
const view: View = {
|
|
374
|
-
...baseView,
|
|
375
|
-
sort: { field: 'title', direction: 'asc' },
|
|
376
|
-
};
|
|
377
|
-
const result = stripActiveViewOverrides(
|
|
378
|
-
view,
|
|
379
|
-
{ sort: { field: 'title', direction: 'asc' } },
|
|
380
|
-
defaultView
|
|
381
|
-
);
|
|
382
|
-
expect( result.sort ).toEqual( defaultView.sort );
|
|
383
|
-
} );
|
|
384
|
-
|
|
385
|
-
it( 'should not change sort when it does not match override', () => {
|
|
386
|
-
const view: View = {
|
|
387
|
-
...baseView,
|
|
388
|
-
sort: { field: 'author', direction: 'asc' },
|
|
389
|
-
};
|
|
390
|
-
const result = stripActiveViewOverrides(
|
|
391
|
-
view,
|
|
392
|
-
{ sort: { field: 'title', direction: 'asc' } },
|
|
393
|
-
defaultView
|
|
394
|
-
);
|
|
395
|
-
expect( result.sort ).toEqual( {
|
|
396
|
-
field: 'author',
|
|
397
|
-
direction: 'asc',
|
|
398
|
-
} );
|
|
399
|
-
} );
|
|
400
|
-
} );
|
|
401
|
-
|
|
402
|
-
describe( 'layout stripping', () => {
|
|
403
|
-
it( 'should strip layout keys managed by overrides', () => {
|
|
404
|
-
const view: View = {
|
|
405
|
-
...baseView,
|
|
406
|
-
layout: {
|
|
407
|
-
density: 'compact',
|
|
408
|
-
styles: { author: { align: 'end' } },
|
|
409
|
-
},
|
|
410
|
-
};
|
|
411
|
-
const result = stripActiveViewOverrides( view, {
|
|
412
|
-
layout: { styles: { author: { align: 'end' } } },
|
|
413
|
-
} );
|
|
414
|
-
expect( result.layout ).toEqual( { density: 'compact' } );
|
|
415
|
-
} );
|
|
416
|
-
|
|
417
|
-
it( 'should set layout to undefined when all keys are stripped', () => {
|
|
418
|
-
const view: View = {
|
|
419
|
-
...baseView,
|
|
420
|
-
layout: {
|
|
421
|
-
styles: { author: { align: 'end' } },
|
|
422
|
-
},
|
|
423
|
-
};
|
|
424
|
-
const result = stripActiveViewOverrides( view, {
|
|
425
|
-
layout: { styles: { author: { align: 'end' } } },
|
|
426
|
-
} );
|
|
427
|
-
expect( result.layout ).toBeUndefined();
|
|
428
|
-
} );
|
|
429
|
-
|
|
430
|
-
it( 'should not touch layout when view has no layout', () => {
|
|
431
|
-
const result = stripActiveViewOverrides( baseView, {
|
|
432
|
-
layout: { styles: {} },
|
|
433
|
-
} );
|
|
434
|
-
expect( result ).not.toHaveProperty( 'layout' );
|
|
435
|
-
} );
|
|
436
|
-
} );
|
|
437
|
-
|
|
438
|
-
describe( 'groupBy stripping', () => {
|
|
439
|
-
it( 'should remove groupBy when managed by overrides', () => {
|
|
440
|
-
const view: View = {
|
|
441
|
-
...baseView,
|
|
442
|
-
groupBy: {
|
|
443
|
-
field: 'status',
|
|
444
|
-
direction: 'asc',
|
|
445
|
-
showLabel: true,
|
|
446
|
-
},
|
|
447
|
-
};
|
|
448
|
-
const result = stripActiveViewOverrides( view, {
|
|
449
|
-
groupBy: { field: 'category', direction: 'desc' },
|
|
450
|
-
} );
|
|
451
|
-
expect( result ).not.toHaveProperty( 'groupBy' );
|
|
452
|
-
} );
|
|
453
|
-
|
|
454
|
-
it( 'should not touch groupBy when view has no groupBy', () => {
|
|
455
|
-
const result = stripActiveViewOverrides( baseView, {
|
|
456
|
-
groupBy: { field: 'category', direction: 'asc' },
|
|
457
|
-
} );
|
|
458
|
-
expect( result ).not.toHaveProperty( 'groupBy' );
|
|
459
|
-
} );
|
|
460
|
-
} );
|
|
461
|
-
|
|
462
|
-
it( 'should not mutate the original view', () => {
|
|
463
|
-
const view: View = {
|
|
464
|
-
...baseView,
|
|
465
|
-
titleField: 'name',
|
|
466
|
-
layout: { density: 'compact', styles: { a: { width: '1px' } } },
|
|
467
|
-
};
|
|
468
|
-
const original = { ...view, layout: { ...view.layout } };
|
|
469
|
-
stripActiveViewOverrides( view, {
|
|
470
|
-
titleField: 'name',
|
|
471
|
-
layout: { styles: {} },
|
|
472
|
-
} );
|
|
473
|
-
expect( view ).toEqual( original );
|
|
474
|
-
} );
|
|
475
|
-
} );
|
|
476
|
-
|
|
477
|
-
describe( 'merge + strip round-trip', () => {
|
|
478
|
-
it( 'should strip what merge added for scalar overrides', () => {
|
|
479
|
-
const overrides = {
|
|
480
|
-
titleField: 'name' as const,
|
|
481
|
-
showMedia: true as const,
|
|
482
|
-
};
|
|
483
|
-
const merged = mergeActiveViewOverrides( baseView, overrides );
|
|
484
|
-
const stripped = stripActiveViewOverrides( merged, overrides );
|
|
485
|
-
expect( stripped ).not.toHaveProperty( 'titleField' );
|
|
486
|
-
expect( stripped ).not.toHaveProperty( 'showMedia' );
|
|
487
|
-
// Original fields remain.
|
|
488
|
-
expect( stripped.type ).toBe( 'table' );
|
|
489
|
-
expect( stripped.sort ).toEqual( baseView.sort );
|
|
490
|
-
} );
|
|
491
|
-
|
|
492
|
-
it( 'should strip what merge added for layout overrides', () => {
|
|
493
|
-
const overrides = {
|
|
494
|
-
layout: { styles: { author: { align: 'end' } } },
|
|
495
|
-
};
|
|
496
|
-
const view: View = {
|
|
497
|
-
...baseView,
|
|
498
|
-
layout: { density: 'compact' },
|
|
499
|
-
};
|
|
500
|
-
const merged = mergeActiveViewOverrides( view, overrides );
|
|
501
|
-
const stripped = stripActiveViewOverrides( merged, overrides );
|
|
502
|
-
expect( stripped.layout ).toEqual( { density: 'compact' } );
|
|
503
|
-
} );
|
|
504
|
-
|
|
505
|
-
it( 'should strip what merge added for filter overrides', () => {
|
|
506
|
-
const overrides = {
|
|
507
|
-
filters: [
|
|
508
|
-
{
|
|
509
|
-
field: 'status' as const,
|
|
510
|
-
operator: 'isAny' as const,
|
|
511
|
-
value: 'publish',
|
|
512
|
-
},
|
|
513
|
-
],
|
|
514
|
-
};
|
|
515
|
-
const merged = mergeActiveViewOverrides( baseView, overrides );
|
|
516
|
-
const stripped = stripActiveViewOverrides( merged, overrides );
|
|
517
|
-
// Only the original author filter should remain.
|
|
518
|
-
expect( stripped.filters ).toEqual( [
|
|
519
|
-
{ field: 'author', operator: 'isAny', value: [ 'admin' ] },
|
|
520
|
-
] );
|
|
521
|
-
} );
|
|
522
|
-
} );
|