This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy directives unit tests from #118
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* wp-bind directive test. | ||
*/ | ||
|
||
require_once __DIR__ . '/../../src/directives/wp-bind.php'; | ||
require_once __DIR__ . '/../../src/directives/wp-context.php'; | ||
|
||
require_once __DIR__ . '/../../src/html/index.php'; | ||
|
||
/** | ||
* Tests for the wp-bind directive. | ||
* | ||
* @group directives | ||
* @covers process_wp_bind | ||
*/ | ||
class Tests_Directives_WpBind extends WP_UnitTestCase { | ||
public function test_directive() { | ||
$markup = '<img wp-bind:src="context.myblock.imageSource" />'; | ||
$tags = new WP_HTML_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
$context_before = new WP_Directive_Context( array( 'myblock' => array( 'imageSource' => './wordpress.png' ) ) ); | ||
$context = $context_before; | ||
process_wp_bind( $tags, $context ); | ||
|
||
$this->assertSame( | ||
'<img src="./wordpress.png" wp-bind:src="context.myblock.imageSource" />', | ||
$tags->get_updated_html() | ||
); | ||
// $this->assertSame( './wordpress.png', $tags->get_attribute( 'src' ) ); // FIXME: Broken; will be fixed by https://github.com/WordPress/gutenberg/pull/46598. | ||
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-bind directive changed context' ); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
/** | ||
* wp-context directive test. | ||
*/ | ||
|
||
require_once __DIR__ . '/../../src/directives/wp-context.php'; | ||
|
||
require_once __DIR__ . '/../../src/html/index.php'; | ||
|
||
/** | ||
* Tests for the wp-context directive. | ||
* | ||
* @group directives | ||
* @covers process_wp_context | ||
*/ | ||
class Tests_Directives_WpContext extends WP_UnitTestCase { | ||
public function test_directive_updates_context() { | ||
$context = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ) ) ); | ||
|
||
$markup = <<<EOF | ||
<wp-context data='{ "myblock": { "open": true } }'> | ||
<wp-context data='{ "myblock": { "open": false } }'> | ||
<span>blah</span> | ||
</wp-context> | ||
<div class="context-true">Should be true again!</div> | ||
</wp-context> | ||
EOF; | ||
$tags = new WP_HTML_Tag_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
process_wp_context( $tags, $context ); | ||
|
||
$this->assertSame( array( 'myblock' => array( 'open' => true ) ), $context->get_context() ); | ||
// TODO: Verify that markup is unchanged? | ||
$tags->next_tag(); | ||
process_wp_context( $tags, $context ); | ||
|
||
$this->assertSame( array( 'myblock' => array( 'open' => false ) ), $context->get_context() ); | ||
|
||
$tags->next_tag(); // <span> | ||
process_wp_context( $tags, $context ); | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); // </span> | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); // </wp-context> for wp-context | ||
|
||
process_wp_context( $tags, $context ); | ||
|
||
$tags->next_tag(); | ||
$this->assertSame( 'context-true', $tags->get_attribute(( 'class' ) ) ); | ||
process_wp_context( $tags, $context ); | ||
|
||
$this->assertSame( array( 'myblock' => array( 'open' => true ) ), $context->get_context() ); | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); // </div> | ||
process_wp_context( $tags, $context ); | ||
|
||
$tags->next_tag( array( 'tag_closers' => 'visit' ) ); // </wp-context> | ||
process_wp_context( $tags, $context ); | ||
|
||
$this->assertSame( array( 'myblock' => array( 'open' => false ) ), $context->get_context() ); | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
/** | ||
* wp-show directive test. | ||
*/ | ||
|
||
require_once __DIR__ . '/../../src/directives/wp-show.php'; | ||
require_once __DIR__ . '/../../src/directives/wp-context.php'; | ||
|
||
/** | ||
* Tests for the wp-show directive. | ||
* | ||
* @group directives | ||
* @covers process_wp_show | ||
*/ | ||
class Tests_Directives_WpShow extends WP_UnitTestCase { | ||
public function test_directive_leaves_content_unchanged_if_when_is_true() { | ||
$markup = <<<EOF | ||
<wp-show when="context.myblock.open"> | ||
<div>I should be shown!</div> | ||
</wp-show> | ||
EOF; | ||
$tags = new WP_HTML_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => true ) ) ); | ||
$context = $context_before; | ||
process_wp_show( $tags, $context ); | ||
|
||
// TODO: | ||
// $content = trim ( $tags->get_content_inside_balanced_tags() ); | ||
// $this->assertSame( '<div>I should be shown!</div>', $content ); | ||
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); | ||
} | ||
|
||
public function test_directive_wraps_content_in_template_if_when_is_false() { | ||
$markup = <<<EOF | ||
<wp-show when="context.myblock.open"> | ||
<div>I should be shown!</div> | ||
</wp-show> | ||
EOF; | ||
$tags = new WP_HTML_Processor( $markup ); | ||
$tags->next_tag(); | ||
|
||
$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ) ) ); | ||
$context = $context_before; | ||
process_wp_show( $tags, $context ); | ||
|
||
// TODO: | ||
// $content = trim( $tags->get_content_inside_balanced_tags() ); | ||
// $this->assertSame( '<template><div>I should be shown!</div></template>', $content ); | ||
$this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); | ||
} | ||
} |