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

Commit

Permalink
Add unit test for nested directives
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jun 1, 2023
1 parent 9430be8 commit 1dc96a2
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions phpunit/directives/attributes/wp-show.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,72 @@ public function test_directive_wraps_void_tag_in_template_if_when_is_false() {
$this->assertSame( $updated_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' );
}

public function test_nested_directives_within_wp_show_with_truthy_value() {
$markup = <<<END
<div data-wp-show="context.myBlock.open">
I should be shown!
<div data-wp-show="context.myOtherBlock.open">I should be shown!</div>
<div data-wp-show="!context.myOtherBlock.open">I should not be shown!</div>
</div>
END;

$context_before = new WP_Directive_Context(
array(
'myBlock' => array( 'open' => true ),
'myOtherBlock' => array( 'open' => true ),
)
);
$context = clone $context_before;

$tags = new WP_Directive_Processor( $markup );

while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
process_wp_show( $tags, $context );
}

$updated_markup = <<<END
<div data-wp-show="context.myBlock.open">
I should be shown!
<div data-wp-show="context.myOtherBlock.open">I should be shown!</div>
<template data-wp-show="!context.myOtherBlock.open"><div >I should not be shown!</div></template>
</div>
END;
$this->assertSame( $updated_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' );
}

public function test_nested_directives_within_wp_show_with_falsy_value() {
$markup = <<<END
<div data-wp-show="context.myBlock.open">
I should be shown!
<div data-wp-show="context.myOtherBlock.open">I should be shown!</div>
<div data-wp-show="!context.myOtherBlock.open">I should not be shown!</div>
</div>
END;

$context_before = new WP_Directive_Context(
array(
'myBlock' => array( 'open' => false ),
'myOtherBlock' => array( 'open' => true ),
)
);
$context = clone $context_before;

$tags = new WP_Directive_Processor( $markup );

while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
process_wp_show( $tags, $context );
}

$updated_markup = <<<END
<template data-wp-show="context.myBlock.open"><div >
I should be shown!
<div data-wp-show="context.myOtherBlock.open">I should be shown!</div>
<template data-wp-show="!context.myOtherBlock.open"><div >I should not be shown!</div></template>
</div></template>
END;
$this->assertSame( $updated_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' );
}
}

0 comments on commit 1dc96a2

Please sign in to comment.