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

add function to detect and render helmet for use in snapshots #2104

Merged
merged 20 commits into from
Sep 13, 2019

Conversation

jroebu14
Copy link
Contributor

@jroebu14 jroebu14 commented Sep 12, 2019

Overall change: Adds functionality to shouldMatchSnapshot to include html and head tags if react-helmet was used inside of component. This enables us to see things like meta tags and scripts in snapshots.

Code changes:

  • Adds new function called renderWithHelmet that is a wrapper around the @testing-library/react render method.

An example using the Chartbeat component in Simorgh

const CanonicalChartbeatBeacon = ({ chartbeatConfig, chartbeatSource }) => {
  useEffect(() => {
    return () => {
      if (typeof window !== 'undefined' && window.pSUPERFLY) {
        /*
          This function is always called to update config values on page changes
          https://chartbeat.zendesk.com/hc/en-us/articles/210271287-Handling-virtual-page-changes
        */
        window.pSUPERFLY.virtualPage(chartbeatConfig);
      }
    };
  }, [chartbeatConfig]);

  return (
    <>
      <Helmet>
        <script async type="text/javascript">
          {`
        (function(){
          var _sf_async_config = window._sf_async_config = (window._sf_async_config || {});
          var config = ${JSON.stringify(chartbeatConfig)};
          for (var key in config) {
            _sf_async_config[key] = config[key];
          }
        })();
      `}
        </script>
        <script async type="text/javascript" src={chartbeatSource} />
      </Helmet>
      <h1>Hello I should be in the body</h1>
    </>
  );
};

would then render

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CanonicalChartbeatAnalytics should return the helmet wrapper with the script snippet 1`] = `
<html>
  <head>
    <script
      async="true"
      type="text/javascript"
    >
      
        (function(){
          var _sf_async_config = window._sf_async_config = (window._sf_async_config || {});
          var config = {"domain":"test-domain","type":"article","sections":"section1 section2","chartbeatUID":1111,"virtualReferrer":null,"useCanonical":true,"title":"This is an article","uid":123};
          for (var key in config) {
            _sf_async_config[key] = config[key];
          }
        })();
      
    </script>
    <script
      async="true"
      src="//chartbeat.js"
      type="text/javascript"
    />
  </head>
  <body>
    <div>
      <h1>
        Hello I should be in the body
      </h1>
    </div>
  </body>
</html>
`;


  • I have assigned myself to this PR and the corresponding issues
  • Automated (jest and/or cypress) tests added (for new features) or updated (for existing features)
  • This PR requires manual testing

@jroebu14 jroebu14 requested a review from dr3 September 12, 2019 11:45
@jroebu14 jroebu14 self-assigned this Sep 12, 2019
@jroebu14 jroebu14 added ws-media The World Service media stream shared-components labels Sep 12, 2019
@jroebu14 jroebu14 requested a review from sareh September 12, 2019 11:48
@jroebu14 jroebu14 marked this pull request as ready for review September 12, 2019 11:48
Copy link
Contributor

@dr3 dr3 left a comment

Choose a reason for hiding this comment

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

LGTM, just some simple snapshot tests would be nice IMO as its more complicated

packages/utilities/psammead-test-helpers/src/index.js Outdated Show resolved Hide resolved
export const shouldMatchSnapshot = (title, component) => {
it(title, () => {
it(title, async () => {
// select the first child to remove the pointless wrapping div from snapshots
Copy link
Contributor

Choose a reason for hiding this comment

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

This happens now :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you worried about the async it callback? It won't affect the current test helper API.

Copy link
Contributor

@dr3 dr3 Sep 13, 2019

Choose a reason for hiding this comment

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

Soz i meant select the first child to remove the pointless wrapping div from snapshots happens now. We no longer remove the pointless wrapping div

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still do when rendering components without helmet. I'll change the comments and var names.

src="test.js"
/>
</head>
<body>
Copy link
Contributor

Choose a reason for hiding this comment

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

Any easy way to remove the body when not needed?

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'll look into it. Was worried about adding more complexity to an already complex looking function for not much value but it should be straightforward

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 tried removing the body but that stopped other tests from rendering things in the body. I guess it breaks the renderer because it can't find the body element's div to put stuff in. I also tried this with removing just the div and it had the same effect :(

@pjlee11
Copy link
Contributor

pjlee11 commented Sep 12, 2019

Jenkins is failing because this updates packages/utilities/moment-timezone-include/package-lock.json but doesn't bump the package version and changelog

Copy link
Contributor

@dr3 dr3 left a comment

Choose a reason for hiding this comment

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

Soz for my confusion, looks great :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
shared-components ws-media The World Service media stream
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants