-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
phpunit/experimental/interactivity-api/class-wp-interactivity-initial-state-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
186 changes: 0 additions & 186 deletions
186
phpunit/experimental/interactivity-api/class-wp-interactivity-store-test.php
This file was deleted.
Oops, something went wrong.