Skip to content

Commit 9a4d447

Browse files
committed
test: update tests for WYSIWYG dynamic values and fix existing test issues
- Jest: use getByRole('button') for Insert/Clear to avoid matching ProseMirror toolbar text - Jest: add editor/wysiwyg to insert-only types; fix Clear button expectations - PHPUnit: add tests for dynamic token rendering inside HTML and wysiwyg store/strip logic - PHPUnit: fix enqueue regex multiline flag for WP 7.1-alpha inline script sourceURL suffix - E2E: fix Tangible Fields plugin basename from tangible-fields/plugin to fields/plugin
1 parent 07ec2ad commit 9a4d447

4 files changed

Lines changed: 81 additions & 14 deletions

File tree

assets/src/components/field/combo-box/async.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const getAsyncProps = props => {
8080
? mapResults(results, props.mapResults)
8181
: results
8282

83+
debounced.current.status = false
8384
return {
8485
items: getOptions(
8586
(formatedResults ?? []).reduce((items, item) => ({ ...items, [item.id]: item.title }), {})

tests/jest/cases/dynamic/render.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe('dynamic values feature - render', () => {
2121
*/
2222
const testTypes = allowedTypes.filter(type => type !== 'conditional-panel')
2323

24+
/**
25+
* Types that only support insert mode (no replace/clear)
26+
*/
27+
const insertOnlyTypes = [ 'text', 'editor', 'wysiwyg' ]
28+
2429
test.each(testTypes)('%p type do not render dynamic values UI if not specified', type => {
2530

2631
const { container } = render(
@@ -31,8 +36,8 @@ describe('dynamic values feature - render', () => {
3136
})
3237
)
3338

34-
expect(within(container).queryByText('Insert')).toBeFalsy()
35-
expect(within(container).queryByText('Clear')).toBeFalsy()
39+
expect(within(container).queryByRole('button', { name: 'Insert' })).toBeFalsy()
40+
expect(within(container).queryByRole('button', { name: 'Clear' })).toBeFalsy()
3641
})
3742

3843
test.each(testTypes)('%p type do not render dynamic values UI if dynamic is false', type => {
@@ -46,8 +51,8 @@ describe('dynamic values feature - render', () => {
4651
})
4752
)
4853

49-
expect(within(container).queryByText('Insert')).toBeFalsy()
50-
expect(within(container).queryByText('Clear')).toBeFalsy()
54+
expect(within(container).queryByRole('button', { name: 'Insert' })).toBeFalsy()
55+
expect(within(container).queryByRole('button', { name: 'Clear' })).toBeFalsy()
5156
})
5257

5358
test.each(testTypes)('%p type does render dynamic values UI if dynamic is true', type => {
@@ -61,13 +66,13 @@ describe('dynamic values feature - render', () => {
6166
})
6267
)
6368

64-
expect(within(container).getByText('Insert')).toBeTruthy()
69+
expect(within(container).getByRole('button', { name: 'Insert' })).toBeTruthy()
6570

66-
// Special case for text, as it uses insert mode by default instead if replace like other types
67-
if( type === 'text' ) {
68-
expect(within(container).queryByText('Clear')).toBeFalsy()
71+
// Insert-only types (text, editor, wysiwyg) do not show a Clear button
72+
if( insertOnlyTypes.includes(type) ) {
73+
expect(within(container).queryByRole('button', { name: 'Clear' })).toBeFalsy()
6974
}
70-
else expect(within(container).getByText('Clear')).toBeTruthy()
75+
else expect(within(container).getByRole('button', { name: 'Clear' })).toBeTruthy()
7176
})
7277

7378
test.each(testTypes)('%p type open dynamic value combobox when clicking on insert button', async type => {
@@ -87,15 +92,15 @@ describe('dynamic values feature - render', () => {
8792

8893
expect(document.querySelector('.tf-dynamic-wrapper-popover')).toBeFalsy()
8994

90-
await user.click(within(container).getByText('Insert'))
95+
await user.click(within(container).getByRole('button', { name: 'Insert' }))
9196

9297
expect(document.querySelector('.tf-dynamic-wrapper-popover')).toBeTruthy()
9398

9499
await user.click(within(container).getByText('Test click outside'))
95100

96101
expect(document.querySelector('.tf-dynamic-wrapper-popover')).toBeFalsy()
97102

98-
await user.click(within(container).getByText('Insert'))
103+
await user.click(within(container).getByRole('button', { name: 'Insert' }))
99104

100105
expect(document.querySelector('.tf-dynamic-wrapper-popover')).toBeTruthy()
101106
})

tests/phpunit/cases/dynamics.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,67 @@ function test_dynamic_value_render_with_context() {
297297
$this->assertEquals($parsed_value, $expected_value, 'parsed was not equal to config');
298298
}
299299

300+
/**
301+
* @depends test_dynamic_value_category_registration
302+
*/
303+
function test_dynamic_value_render_in_html_content() {
304+
305+
$fields = tangible_fields();
306+
$fields->register_dynamic_value([
307+
'name' => 'test-value-html',
308+
'category' => 'test-category',
309+
'callback' => function() {
310+
return 'World';
311+
},
312+
'permission_callback_store' => '__return_true',
313+
'permission_callback_parse' => '__return_true'
314+
]);
315+
316+
// Simulate a wysiwyg field value: token inside HTML
317+
$parsed = $fields->render_value('<p>Hello [[test-value-html]]</p>');
318+
$this->assertEquals('<p>Hello World</p>', $parsed, 'dynamic value was not replaced inside HTML content');
319+
320+
// Multiple tokens in HTML
321+
$parsed = $fields->render_value('<p>[[test-value-html]] [[test-value-html]]</p>');
322+
$this->assertEquals('<p>World World</p>', $parsed, 'multiple dynamic values were not replaced inside HTML content');
323+
324+
// Token inside an attribute value should still be replaced
325+
$parsed = $fields->render_value('<a title="[[test-value-html]]">link</a>');
326+
$this->assertEquals('<a title="World">link</a>', $parsed, 'dynamic value was not replaced inside an HTML attribute');
327+
}
328+
329+
/**
330+
* @depends test_dynamic_value_category_registration
331+
*/
332+
function test_dynamic_value_store_strips_tokens_for_wysiwyg() {
333+
334+
$fields = tangible_fields();
335+
336+
$fields->register_field('wysiwyg-dv-test',
337+
[ 'permission_callback' => '__return_true' ]
338+
+ $fields->_store_callbacks['memory']()
339+
);
340+
341+
// Unauthorized token should be stripped on store
342+
$fields->store_value('wysiwyg-dv-test', '<p>Hello [[unauthorized-dynamic-value]]</p>');
343+
$this->assertEquals(
344+
'<p>Hello </p>',
345+
$fields->fetch_value('wysiwyg-dv-test'),
346+
'unauthorized dynamic value was not stripped from wysiwyg content on store'
347+
);
348+
349+
// Authorized token should be preserved on store
350+
$fields->store_value('wysiwyg-dv-test', '<p>Hello [[test-value-html]]</p>');
351+
$this->assertEquals(
352+
'<p>Hello [[test-value-html]]</p>',
353+
$fields->fetch_value('wysiwyg-dv-test'),
354+
'authorized dynamic value token was incorrectly stripped from wysiwyg content on store'
355+
);
356+
357+
// Cleanup
358+
$fields->store_value('wysiwyg-dv-test', null);
359+
}
360+
300361
/**
301362
* @depends test_dynamic_value_category_registration
302363
*/

tests/phpunit/cases/enqueue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function test_items_enqueue(string $type, array $args) {
5858
}
5959
$this->assertGreaterThan(
6060
0,
61-
preg_match('#var TangibleFieldsConfig = (.+?);#', $data, $matches),
61+
preg_match('#^var TangibleFieldsConfig = (.+);$#m', $data, $matches),
6262
'wp_add_inline_script does not have TangibleFieldsConfig'
6363
);
6464
$data = json_decode($matches[1], true);
@@ -93,13 +93,13 @@ public function test_fields_enqueue_conditions(string $type, array $args) {
9393

9494
if ( version_compare($wp_version, '6.3', '>') ) {
9595
preg_match(
96-
'#var TangibleFieldsConfig = (.+?);#',
96+
'#^var TangibleFieldsConfig = (.+);$#m',
9797
wp_scripts()->get_inline_script_data('tangible-fields', 'before'),
9898
$matches
9999
);
100100
} else {
101101
preg_match(
102-
'#^var TangibleFieldsConfig = (.+?);$#',
102+
'#^var TangibleFieldsConfig = (.+);$#m',
103103
wp_scripts()->print_inline_script('tangible-fields', 'before', false),
104104
$matches
105105
);

0 commit comments

Comments
 (0)