Skip to content

Commit

Permalink
Update Initial State and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Nov 27, 2023
1 parent 3466362 commit 7e5580c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
/**
* WP_Interactivity_Initial_State class
*
* @package Gutenberg
* @subpackage Interactivity API
*/

if ( class_exists( 'WP_Interactivity_Initial_State' ) ) {
return;
}

/**
* Manages the initial state of the Interactivity API store in the server and
* its serialization so it can be restored in the browser upon hydration.
*
Expand All @@ -19,28 +28,28 @@ class WP_Interactivity_Initial_State {
/**
* Get state from a given namespace.
*
* @param string $namespace Namespace.
* @param string $store_ns Namespace.
*
* @return array The requested state.
*/
public static function get_state( $namespace = null ) {
if ( ! $namespace ) {
public static function get_state( $store_ns = null ) {
if ( ! $store_ns ) {
return self::$initial_state;
}
return self::$initial_state[ $namespace ] ?? array();
return self::$initial_state[ $store_ns ] ?? array();
}

/**
* Merge data into the state with the given namespace.
*
* @param string $namespace Namespace.
* @param string $store_ns Namespace.
* @param array $data State to merge.
*
* @return void
*/
public static function merge_state( $namespace, $data ) {
self::$initial_state[ $namespace ] = array_replace_recursive(
self::get_state( $namespace ),
public static function merge_state( $store_ns, $data ) {
self::$initial_state[ $store_ns ] = array_replace_recursive(
self::get_state( $store_ns ),
$data
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* `WP_Interactivity_Initial_State` class test.
*
* @package Gutenberg
* @subpackage Interactivity API
*/

/**
* Tests for the `WP_Interactivity_Initial_State` class.
*
* @group interactivity-api
* @covers WP_Interactivity_Initial_State
*/
class WP_Interactivity_Initial_State_Test extends WP_UnitTestCase {
public function set_up() {
// Clear the state before each test.
WP_Interactivity_Initial_State::reset();
}

public function test_initial_state_should_be_empty() {
$this->assertEmpty( WP_Interactivity_Initial_State::get_data() );
}

public function test_initial_state_can_be_merged() {
$state = array(
'a' => 1,
'b' => 2,
'nested' => array(
'c' => 3,
),
);
WP_Interactivity_Initial_State::merge_state( 'core', $state );
$this->assertSame( $state, WP_Interactivity_Initial_State::get_state( 'core' ) );
}

public function test_initial_state_can_be_extended() {
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => 1 ) );
WP_Interactivity_Initial_State::merge_state( 'core', array( 'b' => 2 ) );
WP_Interactivity_Initial_State::merge_state( 'custom', array( 'c' => 3 ) );
$this->assertSame(
array(
'core' => array(
'a' => 1,
'b' => 2,
),
'custom' => array(
'c' => 3,
),
),
WP_Interactivity_Initial_State::get_data()
);
}

public function test_initial_state_existing_props_should_be_overwritten() {
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => 1 ) );
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => 'overwritten' ) );
$this->assertSame(
array(
'core' => array(
'a' => 'overwritten',
),
),
WP_Interactivity_Initial_State::get_data()
);
}

public function test_initial_state_existing_indexed_arrays_should_be_replaced() {
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => array( 1, 2 ) ) );
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => array( 3, 4 ) ) );
$this->assertSame(
array(
'core' => array(
'a' => array( 3, 4 ),
),
),
WP_Interactivity_Initial_State::get_data()
);
}

public function test_initial_state_should_be_correctly_rendered() {
WP_Interactivity_Initial_State::merge_state( 'core', array( 'a' => 1 ) );
WP_Interactivity_Initial_State::merge_state( 'core', array( 'b' => 2 ) );
WP_Interactivity_Initial_State::merge_state( 'custom', array( 'c' => 3 ) );

ob_start();
WP_Interactivity_Initial_State::render();
$rendered = ob_get_clean();
$this->assertSame(
'<script id="wp-interactivity-initial-state" type="application/json">{"core":{"a":1,"b":2},"custom":{"c":3}}</script>',
$rendered
);
}

public function test_initial_state_should_also_escape_tags_and_amps() {
WP_Interactivity_Initial_State::merge_state(
'test',
array(
'amps' => 'http://site.test/?foo=1&baz=2&bar=3',
'tags' => 'Do not do this: <!-- <script>',
)
);
ob_start();
WP_Interactivity_Initial_State::render();
$rendered = ob_get_clean();
$this->assertSame(
'<script id="wp-interactivity-initial-state" type="application/json">{"test":{"amps":"http:\/\/site.test\/?foo=1\u0026baz=2\u0026bar=3","tags":"Do not do this: \u003C!-- \u003Cscript\u003E"}}</script>',
$rendered
);
}
}

This file was deleted.

0 comments on commit 7e5580c

Please sign in to comment.