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

Inserter Menu: Add unit tests #2018

Merged
merged 12 commits into from
Jul 28, 2017
Merged

Inserter Menu: Add unit tests #2018

merged 12 commits into from
Jul 28, 2017

Conversation

youknowriad
Copy link
Contributor

Follow up to #1661

Several things could be tested for the inserter menu, this PR bootstraps a basic unit test for this component:

  • Ensures that each tab shows the correct blocks
  • Ensures that an already used block with useOnce: true is disabled.

@youknowriad youknowriad added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Jul 26, 2017
@youknowriad youknowriad self-assigned this Jul 26, 2017
@youknowriad youknowriad requested review from nylen and aduth July 26, 2017 09:31
aduth
aduth previously requested changes Jul 26, 2017
);
const visibleBlocks = wrapper.find( '.editor-inserter__block' );
expect( visibleBlocks.length ).toBe( 1 );
expect( visibleBlocks.text() ).toBe( '<Dashicon />Advanced Text' );
Copy link
Member

Choose a reason for hiding this comment

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

This fails after rebase because the Dashicon was replaced with BlockIcon in #1870. Also the way this string is formed seems potentially fragile (what if future serializers omit the space before the closing slash?). Maybe: find, contains, childAt, or not testing content at all.

expect( visibleBlocks.childAt( 0 ).name() ).toBe( 'BlockIcon' );
expect( visibleBlocks.childAt( 1 ).text() ).toBe( 'Advanced Text' );

/>
);
const visibleBlocks = wrapper.find( '.editor-inserter__block' );
expect( visibleBlocks.length ).toBe( 1 );
Copy link
Member

Choose a reason for hiding this comment

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

I'm finding when I run this test in isolation (adding .only), this line fails because it's expecting 36 as length... sounds like the default set of registered blocks? Is this bleeding over from another test? Should we unregister all blocks in a beforeAll block here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to use the .only for some reason but I made the changes that should fix it.

@nylen nylen dismissed aduth’s stale review July 27, 2017 12:48

This is fixed.

/>
);

expect( enzymeToJson( wrapper ) ).toMatchSnapshot();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't like these assertions personally, they break on each small change to the component. I'd rather add more targeted tests

@nylen
Copy link
Member

nylen commented Jul 27, 2017

Thanks for working on this.

I added a test for searching and took the opportunity to try out Jest snapshots.

It will make it easier to write tests, but may cause them to be updated more frequently. They're easy to update with npm run test-unit -- [file] --updateSnapshot, but we will need to be sure to review the changes to them.

Further reading on the tradeoffs here:

For the InserterMenu in particular, it's quite large at 438 lines long, so we may want to break it up into sub-components. This should help simplify the snapshots.

@youknowriad
Copy link
Contributor Author

I don't think snapshots are good for testing Markup or components output, Components are updated frequently. I'd rather use the snapshots for things like "serializing","parsing" where we're not supposed to change the output so often.

Another downside is the maintenance of the test, with the snapshot we don't know exactly what part of the markup is being tested exactly. When the test breaks we don't know if it's really a bug or we just have to update the generated markup. The snapshot is not tied to its assertion.

@nylen
Copy link
Member

nylen commented Jul 27, 2017

I think it's a question of which way is ultimately more effort:

  • Write targeted tests. This is more difficult at first, and the actual result has been that we haven't been doing very much of it.
  • Use snapshots. They are easy to create, and need to be updated when a component changes, but this is also easy. Probably the most time-consuming part will be reviewing the changes to make sure they are as intended.

Still, maybe snapshots would be a better idea later on in the life of the project when it is more stable, and we should revisit then.

@nylen nylen changed the title Inserter Menu: Bootstraping a unit test for the inserter menu Inserter Menu: Add unit tests Jul 27, 2017
@nylen nylen force-pushed the test/inserter-menu branch from 940de1a to 3fe2630 Compare July 27, 2017 13:50
@codecov-io
Copy link

codecov-io commented Jul 27, 2017

Codecov Report

Merging #2018 into master will increase coverage by 1.44%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2018      +/-   ##
==========================================
+ Coverage   18.82%   20.27%   +1.44%     
==========================================
  Files         129      130       +1     
  Lines        4197     4198       +1     
  Branches      716      715       -1     
==========================================
+ Hits          790      851      +61     
+ Misses       2868     2819      -49     
+ Partials      539      528      -11
Impacted Files Coverage Δ
editor/inserter/menu.js 43.69% <ø> (+43.69%) ⬆️
blocks/library/more/index.js 30% <0%> (ø)
blocks/api/serializer.js 100% <0%> (+3.03%) ⬆️
blocks/library/quote/index.js 18.6% <0%> (+4.31%) ⬆️
i18n/index.js 77.77% <0%> (+11.11%) ⬆️
blocks/api/categories.js 100% <0%> (+50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b4a30ac...6db6f7d. Read the comment docs.

@nylen
Copy link
Member

nylen commented Jul 27, 2017

☝️ This is pretty cool IMO, and more useful and less noisy than the previous service (it will only leave one comment, then edit it afterwards). Thoughts on whether we want to leave it enabled?

@youknowriad
Copy link
Contributor Author

@nylen I agree, this is great 👍, you see exactly what you improved or not

@nylen
Copy link
Member

nylen commented Jul 27, 2017

Okay. I do think the tests are nicer this way too, it's just a bit annoying to write. Let's merge this 👍

@youknowriad youknowriad merged commit 73d1410 into master Jul 28, 2017
@youknowriad youknowriad deleted the test/inserter-menu branch July 28, 2017 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[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