Skip to content

Commit

Permalink
E2E Tests: Adding a test to check CPT templates initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 2, 2018
1 parent 974245c commit 2cc39d0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
volumes:
- wordpress:/var/www/html
- .:/var/www/html/wp-content/plugins/gutenberg
- ./test/e2e/test-plugins:/var/www/html/wp-content/plugins/gutenberg-test-plugins

cli:
image: wordpress:cli
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/specs/__snapshots__/templates.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Using a CPT with a predefined template Should add a custom post types with a predefined template 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\" /></figure>
<!-- /wp:image -->
<!-- wp:paragraph {\\"placeholder\\":\\"Add a book description\\"} -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"></blockquote>
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns has-2-columns\\">
<!-- wp:image {\\"layout\\":\\"column-1\\"} -->
<figure class=\\"wp-block-image layout-column-1\\"><img alt=\\"\\" /></figure>
<!-- /wp:image -->
<!-- wp:paragraph {\\"placeholder\\":\\"Add a inner paragraph\\",\\"layout\\":\\"column-2\\"} -->
<p class=\\"layout-column-2\\"></p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:columns -->"
`;
30 changes: 30 additions & 0 deletions test/e2e/specs/templates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import '../support/bootstrap';
import { newPost, newDesktopBrowserPage } from '../support/utils';
import { activatePlugin, deactivatePlugin } from '../support/plugins';

describe( 'Using a CPT with a predefined template', () => {
beforeAll( async () => {
await newDesktopBrowserPage();
await activatePlugin( 'gutenberg-test-plugin-templates' );
await newPost( 'book' );
} );

afterAll( async () => {
await newDesktopBrowserPage();
await deactivatePlugin( 'gutenberg-test-plugin-templates' );
} );

it( 'Should add a custom post types with a predefined template', async () => {
//Switch to Code Editor to check HTML output
await page.click( '.edit-post-more-menu [aria-label="More"]' );
const codeEditorButton = ( await page.$x( '//button[contains(text(), \'Code Editor\')]' ) )[ 0 ];
await codeEditorButton.click( 'button' );

// Assert that the post already contains the template defined blocks
const textEditorContent = await page.$eval( '.editor-post-text-editor', ( element ) => element.value );
expect( textEditorContent ).toMatchSnapshot();
} );
} );
2 changes: 1 addition & 1 deletion test/e2e/support/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import puppeteer from 'puppeteer';
jest.setTimeout( 100000 );

beforeAll( async () => {
global.browser = await puppeteer.launch();
global.browser = await puppeteer.launch( { headless: false, slowMo: 80 } );
} );

afterAll( async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export async function visitAdmin( adminPath ) {
}
}

export async function newPost() {
await visitAdmin( 'post-new.php' );
export async function newPost( postType ) {
await visitAdmin( 'post-new.php' + ( postType ? '?post_type=' + postType : '' ) );
}

export async function newDesktopBrowserPage() {
Expand Down
46 changes: 46 additions & 0 deletions test/e2e/test-plugins/templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Plugin Name: Gutenberg Test Plugin, Templates
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-templates
*/

/**
* Registers a book CPT with a template
*/
function register_book_type() {
$args = array(
'public' => true,
'label' => 'Books',
'show_in_rest' => true,
'template' => array(
array( 'core/image' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a book description',
),
),
array( 'core/quote' ),
array(
'core/columns',
array(),
array(
array( 'core/image', array( 'layout' => 'column-1' ) ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a inner paragraph',
'layout' => 'column-2',
),
),
),
),
),
);
register_post_type( 'book', $args );
}

add_action( 'init', 'register_book_type' );

0 comments on commit 2cc39d0

Please sign in to comment.