Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Copy directives unit tests from #118
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jan 2, 2023
1 parent 17f57b3 commit 6f35930
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
34 changes: 34 additions & 0 deletions phpunit/directives/wp-bind.php
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' );
}
}
64 changes: 64 additions & 0 deletions phpunit/directives/wp-context.php
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() );
}
}
53 changes: 53 additions & 0 deletions phpunit/directives/wp-show.php
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' );
}
}

0 comments on commit 6f35930

Please sign in to comment.