Skip to content

[RNMobile] Tested the React Native <RichText/> component to ensure various expectations regarding font size.#35528

Merged
enejb merged 2 commits intoWordPress:trunkfrom
ttahmouch:feature/test-richtext
Oct 12, 2021
Merged

[RNMobile] Tested the React Native <RichText/> component to ensure various expectations regarding font size.#35528
enejb merged 2 commits intoWordPress:trunkfrom
ttahmouch:feature/test-richtext

Conversation

@ttahmouch
Copy link
Copy Markdown
Contributor

@ttahmouch ttahmouch commented Oct 11, 2021

Description

Previously, the <RichText/> component lacked test coverage in React Native altogether.

This is my humble attempt at adding coverage for styling rich text displayed with font size derived from various sources, i.e., fontSize and style.fontSize component props, and global state selected from __experimentalGlobalStylesBaseStyles.typography.fontSize.

How has this been tested?

The tests were written as "black-box integration" tests. That's basically just my way of saying that I deliberately tried to abstain from mocking or stubbing implementation details of the <RichText/> component itself and only mocked state provided to the component from the outer edges of the system, i.e., component props, global state selected from Redux. That means that getPxFromCssUnit and Dimensions.get( 'window' ) are "real."

These tests are supposed to closely resemble "functional" testing in that they "display" the <RichText/> component in various staged scenarios and attempt to assert the results from the "users' experience." I would have preferred to use the actual text that the user would see displayed in order to assert against, but for some reason our text provided via component props is modified to be represented as HTML fragments and is wrapped in <div> which could lead to nondeterminism. I chose to use accessibilityLabel instead even though it's not ideal.

$ npm run native test -- ../rich-text/src/test/index.native.js
...
 PASS  ../rich-text/src/test/index.native.js (6.04 s)
  <RichText/>
    Font Size
      ✓ should display rich text at the DEFAULT font size. (10 ms)
      ✓ should display rich text at the PROVIDED font size computed from the LOCAL `fontSize` CSS. (7 ms)
      ✓ should display rich text at the PROVIDED font size computed from the LOCAL `style.fontSize` CSS. (1 ms)
      ✓ should display rich text at the PROVIDED font size computed from the selected GLOBAL
		`__experimentalGlobalStylesBaseStyles.typography.fontSize` CSS. (2 ms)
      ✓ should display rich text at the font size computed from the LOCAL `fontSize` CSS with HIGHEST PRIORITY
		when CSS is provided ambiguously from ALL possible sources. (2 ms)
      ✓ should display rich text at the font size computed from the LOCAL `style.fontSize` CSS with
		NEXT PRIORITY when CSS is provided ambiguously from MULTIPLE possible sources EXCLUDING `fontSize`. (1 ms)
      ✓ should display rich text at the font size computed from CSS relative to the VIEWPORT WIDTH. (1 ms)
      ✓ should display rich text at the font size computed from CSS relative to the VIEWPORT HEIGHT. (2 ms)

Test Suites: 1 passed, 1 total
Tests:       8 passed, 8 total
Snapshots:   0 total
Time:        6.489 s

Screenshots

The changes do not affect the actual UX implementation details. This just adds test coverage to existing implementation details.

Types of changes

Test Coverage.

ESLint

No issues found with the contents of this PR. All issues found are regarding existing skipped tests.

$ npm run lint-js:fix
...
/Users/ttahmouch/Desktop/src/gutenberg/packages/e2e-tests/specs/*
  113:2  warning  Skipped test  jest/no-disabled-tests
✖ 18 problems (0 errors, 18 warnings)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

ttahmouch added 2 commits October 11, 2021 15:31
…s expectations regarding font size.

Previously, the `<RichText/>` component lacked test coverage in React Native altogether.

This is my humble attempt at adding coverage for styling rich text displayed with font size derived from various sources, i.e., `fontSize` and `style.fontSize` component props, and global state selected from `__experimentalGlobalStylesBaseStyles.typography.fontSize`.
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 11, 2021
@github-actions
Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @ttahmouch! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

Copy link
Copy Markdown
Contributor

@mkevins mkevins left a comment

Choose a reason for hiding this comment

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

This looks great Tony! Thanks for adding these. I like the comments in each test for the functional test approach.

Code looks good, and I tested these via the steps described.

Comment on lines +69 to +80
// Arrange
const expectedFontSize = 32;
// Act
const { getByA11yLabel } = render(
<RichText
accessibilityLabel={ 'editor' }
fontSize={ 'min(2em, 3em)' }
/>
);
// Assert
const actualFontSize = getByA11yLabel( 'editor' ).props.fontSize;
expect( actualFontSize ).toBe( expectedFontSize );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I appreciate the comments here. 😄 👍 (as in the other tests)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback. I greatly appreciate you taking the time to review and run the tests, @mkevins . :)

I don't have a way of adding other assignees to review. @enejb and I paired on these tests. I'm hoping he'll also have a chance to take a look when he has some time today.

@enejb enejb merged commit c8148c2 into WordPress:trunk Oct 12, 2021
@enejb
Copy link
Copy Markdown
Contributor

enejb commented Oct 12, 2021

Nice work @ttahmouch !

@github-actions github-actions bot added this to the Gutenberg 11.8 milestone Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants