react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Mounting Helmet Tests not working in v5

Open tomatau opened this issue 9 years ago • 6 comments

Running tests for document.head updates using enzyme's mount function for version 4 and all works fine. Started a migration to v5 but my mounted tests have stopped working.

Setup document:

import { jsdom } from 'jsdom'
import { keys } from 'ramda'

global.document = jsdom(
  '<html><head></head><body><div id="application-root"></div></body></html>'
)
global.window = document.defaultView
keys(document.defaultView).forEach(property => {
  if (typeof global[property] === 'undefined') {
    global[property] = document.defaultView[property]
  }
})

global.navigator = { userAgent: 'node.js' }

My main application render:

const Main = () =>
        <DocumentMeta
          defaultTitle='Breko Hub'
          titleTemplate='%s | Breko Hub'>
          <html lang='en' />
          <meta charSet='utf-8' />
        </DocumentMeta>

Running the tests

const wrapper = mount(Main, { attachTo: document.getElementById('application-root') })

setImmediate(() => {
  // This works:
  expect(document.title).to.eql('Breko Hub')

  // This doesn't work anymore in v5, worked fine in v4
  const meta = document.querySelector('meta')
  // meta === null
})

I have server side tests ensuring that the server renders the appropriate attributes and they work fine using Helmet.canUseDOM = false. But the client-side tests which actually assume the DOM is usable, aren't working anymore. The document.head remains empty.

const wrapper = mount(Main, { attachTo: document.getElementById('application-root') })

setImmediate(() => {
  (document.head || document.querySelector('head')).innerHTML
  // returns an empty string
})

tomatau avatar Mar 28 '17 08:03 tomatau

Helmet 5 batches DOM updates via requestIdeCallback. It is now possible to query the DOM before Helmet performs its update.

Take a look at our tests to see how we've dealt with this change.

doctyper avatar Mar 28 '17 16:03 doctyper

@doctyper I did look at your tests and saw you run them in Karma. requestIdleCallback doesn't exist in a JSDOM, a setImmediate works though.

tomatau avatar Mar 28 '17 16:03 tomatau

You can use our polyfill:

import {requestIdleCallback} from "react-helmet/src/HelmetUtils";

doctyper avatar Mar 28 '17 16:03 doctyper

Thanks, should be fine, it's just a setTimeout and the api of requestIdleCallback which I won't be using directly. I'm happy with a setImmediate and less coupling ;) Thanks for the replies and I like the new API!

tomatau avatar Mar 28 '17 16:03 tomatau

@doctyper Thank you for that! Though, I was only able to import it from lib:

import { requestIdleCallback } from 'react-helmet/lib/HelmetUtils';

coreyleelarson avatar May 24 '17 16:05 coreyleelarson

I looks like requestIdleCallback was replaces with requestAnimationFrame from v5.2.0

you will need to import it instead

import { requestAnimationFrame } from 'react-helmet/lib/HelmetUtils'; 
.....
.........
describe('Something', () => {
  it('set title', done => {
    ReactDOM.render(
      <Helmet defaultTitle={'Fallback'} title={'Test Title'} />,
      container
    );

    requestAnimationFrame(() => {
      expect(document.title).toEqual('Test Title');
      done();
    });
  });
});

joselcc avatar May 24 '19 02:05 joselcc