Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tests for Format API code #11562

Merged
merged 4 commits into from
Nov 8, 2018

Conversation

Pixelrobin
Copy link
Contributor

@Pixelrobin Pixelrobin commented Nov 7, 2018

Description

Closes #11110.

Addressed #11110 by adding various tests. I used tests from other parts of the code as a reference. I am new to Jest and Gutenberg development though.

Types of changes

Adds tests to several functions in the Format API, which should hopefully not break anything?

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable labels Nov 7, 2018
@gziolo gziolo added this to the 4.3 milestone Nov 7, 2018
@gziolo gziolo requested review from gziolo and ellatrix November 7, 2018 07:44
@ellatrix
Copy link
Member

ellatrix commented Nov 7, 2018

Thanks for the tests @Pixelrobin!


describe( 'getFormatType', () => {
// Initialize format store.
require( '../store' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be wrapped in a beforeAll?

Copy link
Contributor Author

@Pixelrobin Pixelrobin Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my bad. I think I did it like this all the way though. I'll fix it.

const obj = { type: 'obj' };
const em = { type: 'em' };

const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a special characters file with these constants that you can import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I noticed that. I was working off an older version that didn't have that implemented yet. I assume updating the branch would require me to take care of the merge conflict myself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a rebase you could use the constants. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually do the following on my branch:

git fetch
git rebase origin/master

you fix conflicts and:

git add .
git rebase --continue

};
const expected = {
formats: [ , , [ { ...obj, object: true } ], [ em ], , , , , , , ],
text: 'on' + OBJECT_REPLACEMENT_CHARACTER + 'o three',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: you could use template literals here:

`on${ OBJECT_REPLACEMENT_CHARACTER }o three`

It reads a bit easier to me.

@ellatrix
Copy link
Member

ellatrix commented Nov 7, 2018

Looks good! Needs a rebase. There were some tests added for registerFormatType which will duplicate some tests here.


describe( 'getFormatTypes', () => {
// Initialize format store.
require( '../store' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing with beforeAll.

} );

it( 'should return all registered formats', () => {
const formatType1 = { edit: noop, title: 'format title' };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like more descriptive names for variables.
How about:

  • testFormat
  • testFormatWithSettings
    to align with the names used as first param in registerFormatType?

const obj = { type: 'obj' };
const em = { type: 'em' };

const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually do the following on my branch:

git fetch
git rebase origin/master

you fix conflicts and:

git add .
git rebase --continue

import { getFormatTypes, getFormatType } from '../selectors';

describe( 'selectors', () => {
const defaultState = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good practice to wrap state with deepFreeze to ensure that selectors never mutate state.

const defaultFormatSettings = { edit: noop, title: 'format title' };

// Initialize format store.
require( '../store' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeAll again :)
I also noticed that comment isn't 100% accurate. It is rich text store.

In general, we need to come up with a nicer way of using stores with tests.

const defaultFormatSettings = { edit: noop, title: 'format title' };

// Initialize format store.
require( '../store' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeAll :)


it( 'should unregister existing formats', () => {
registerFormatType( 'core/test-format', defaultFormatSettings );
expect( getFormatTypes() ).toEqual( [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might read better when getFormatType is used here as core/test-format' is the only thing we care about here..

@gziolo
Copy link
Member

gziolo commented Nov 7, 2018

This was my first Gutenberg PR and first time using Jest, so I learned a lot 😄, thanks for that opportunity. I hope it's good to go!

You learn really fast, awesome work! :)
We need rebase and a few tweaks and we should be good to go.

There were some tests added for registerFormatType which will duplicate some tests here.

Yes, this is the only tricky part. I left some hint how I usually do it. Just make sure we don't have duplicated tests in that file. A few of tests you added are missing in the test suite added by @iseulde yesterday.

@youknowriad youknowriad modified the milestones: 4.3, 4.4 Nov 7, 2018
@youknowriad
Copy link
Contributor

Moved to 4.4 as it's not a blocker for 4.3. Let's merge it if it's ready though.

@Pixelrobin Pixelrobin force-pushed the add/missing-format-api-tests branch from 2973ece to fd7a191 Compare November 7, 2018 19:49
@Pixelrobin
Copy link
Contributor Author

Pixelrobin commented Nov 7, 2018

Ok, I think I applied all the feedback, thanks!

  • Used beforeAll on initializing rich-text store
  • Used deepFreeze on state objects.
  • Imported OBJECT_REPLACEMENT_CHARACTER
  • Fixed some variable naming
  • Used getFormatType instead of getFormatTypes in test/unregister-format-type.js. I do think it should be noted that the original way was how it's done in the block API tests.
  • test/register-format-type.js has changed quite a bit as I mashed the two versions together.
  • Changes to registerFormatType object requirements led to some other changes to format registration in certain tests.
  • Fixed minor documentation bug in insert-object.js, where it incorrectly labeled an object property as a string.

Rebasing added the gutenberg-mobile submodule to the branch for some reason, so I removed it in another commit. So this should definitely be squashed :D

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work, thanks for addressing all feedback 💯

@gziolo gziolo modified the milestones: 4.4, 4.3 Nov 8, 2018
@gziolo gziolo merged commit a9b0742 into WordPress:master Nov 8, 2018
@gziolo
Copy link
Member

gziolo commented Nov 8, 2018

@Pixelrobin congrats on your first code contribution! 🎉

title: 'format title',
keywords: [ 'one', 'two', 'three' ],
formatTestSetting: 'settingTestValue',
tagName: 'test',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iseulde - should we add validation for tagName property? Values like testandtest 2` made me think about it.

daniloercoli added a commit that referenced this pull request Nov 8, 2018
…rnmobile/fix-merge-content-not-refreshed-UI

* 'master' of https://github.com/WordPress/gutenberg:
  Add missing tests for Format API code (#11562)
  chore: Improve undo/redo no-op (#11428)
  Fix: Contrast checker: Consider fontSize large when size >=  24px instead of >= 18px (#9268)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants