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

[Fix] render: handle Fiber strings and numbers #2221

Merged
merged 4 commits into from
Sep 23, 2019

Conversation

ottosichert
Copy link
Contributor

@ottosichert
Copy link
Contributor Author

Rebased and fixed linter errors

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks!

@ljharb
Copy link
Member

ljharb commented Sep 16, 2019

I've rebased again, including my latest changes as well as your utils spec.

@ottosichert
Copy link
Contributor Author

That was quick, thank you very much! I didn't see your approval while I was adding the utils spec. I added them because the coverage went down.

With your latest push everything is as it should be 👍

@ottosichert
Copy link
Contributor Author

This build will fail: https://travis-ci.org/airbnb/enzyme/jobs/585805429

  1) Utils loadCheerioRoot always returns a Cheerio instance:

     AssertionError: expected { Object (options) } to be an instance of 

      at Context.<anonymous> (packages/enzyme-test-suite/test/Utils-spec.jsx:1081:42)

I will push a fix shortly

@ljharb
Copy link
Member

ljharb commented Sep 16, 2019

cheerio does need to be a dep of the test suite, but that doesn't fix it. I'll wait for your push.

@ottosichert
Copy link
Contributor Author

I used the public property of the Cheerio constructor called cheerio, see:

https://github.com/cheeriojs/cheerio/blob/1.0.0-rc.3/lib/cheerio.js#L101

Cheerio.prototype.cheerio = '[cheerio object]';

It is only available on Cheerio instances:

$ node
> const cheerio = require('cheerio');
undefined
> cheerio.version
'1.0.0-rc.3'
> cheerio.cheerio
undefined
> cheerio('<div>foo</div>').cheerio
'[cheerio object]'
> cheerio.load('')().cheerio
'[cheerio object]'
> 

@ljharb
Copy link
Member

ljharb commented Sep 16, 2019

eesh, i mean, that clearly works but that doesn't seem robust enough to be a valuable test. why was instanceOf(cheerio) failing? it works in the repl.

@ottosichert
Copy link
Contributor Author

The cheerio property has been available since the very first release: https://github.com/cheeriojs/cheerio/blob/0.1.0/src/cheerio.coffee#L66

    cheerio : "0.0.1"   

It was later changed to hold the special value "[cheerio object]" instead of the version number in cheeriojs/cheerio@41337f9

-    cheerio : '0.4.2',
+    cheerio : '[cheerio object]',

As it is also used in dom-serializer https://github.com/cheeriojs/dom-serializer/blob/v0.2.1/index.js#L88 I followed the same approach.

However, I didn't look into why the .instanceOf didn't work as expected, and I can reproduce the same behaviour in node:

> const cheerio = require('cheerio');
undefined
> cheerio.version
'1.0.0-rc.3'
> cheerio.root() instanceof cheerio
true
> cheerio.load('<div>foo</div>', null, false).root() instanceof cheerio
true
> cheerio.load('')('<div>foo</div>') instanceof cheerio
true
> 

I will investigate and submit a new version when I found the issue

@ljharb
Copy link
Member

ljharb commented Sep 16, 2019

Thanks! The issue isn't whether the property works in every version of cheerio - this is knowable - but whether any object can mistakenly pass the test by having an arbitrary string property. In tests of our own code, this really isn't a big concern, just a philosophical one :-) really, it's that i'd prefer to at least understand why no better option is available before accepting a non-ideal one.

@ottosichert
Copy link
Contributor Author

What I found out so far is that there are two different identities for cheerio:

  • require('cheerio') in enzyme/build/Utils
  • import cheerio from 'cheerio' in enzyme-test-suite/test/Utils-spec.jsx

Which means the versions are identical but instanceOf and equality check will fail:

  describe('loadCheerioRoot', () => {
    it('always returns a Cheerio instance', () => {
      const buildCheerio = loadCheerioRoot().constructor;
      expect(buildCheerio.version).to.equal(cheerio.version);
      expect(String(buildCheerio)).to.equal(String(cheerio));
      expect(buildCheerio).to.equal(cheerio); // fails
      [...]
  1) Utils loadCheerioRoot always returns a Cheerio instance:

      AssertionError: expected [Function] to equal [Function]
      + expected - actual

      
      at Context.<anonymous> (packages/enzyme-test-suite/test/Utils-spec.jsx:1084:14)

This seems to be more of a build setup issue, did you run into this problem before?

@ottosichert
Copy link
Contributor Author

Also reproducible in node:

enzyme/packages/enzyme-test-suite$ node
> const cheerio = require('cheerio');
undefined
> const loadCheerioRoot = require('enzyme/build/Utils').loadCheerioRoot;
undefined
> loadCheerioRoot().constructor.version === cheerio.version
true
> String(loadCheerioRoot().constructor) === String(cheerio)
true
> loadCheerioRoot().constructor === cheerio
false
> 

@ljharb
Copy link
Member

ljharb commented Sep 17, 2019

gotcha, now that makes sense :-) i'd expect lerna to hoist them up so they're the same version, but either way this is something I can solve. Will fix shortly.

@ljharb ljharb force-pushed the 2098-fix-cheerio-fiber branch 5 times, most recently from 8305b85 to 4b3cc31 Compare September 18, 2019 05:19
@ljharb
Copy link
Member

ljharb commented Sep 18, 2019

k, i ended up going with your test suggestion ¯\_(ツ)_/¯

@ljharb ljharb force-pushed the 2098-fix-cheerio-fiber branch 3 times, most recently from bb08dfb to 9aa653f Compare September 19, 2019 00:00
 - `enzyme-adapter-utils` `assertDomAvailable` & `elementToTree`
 - `EnzymeAdapter` `matchesElementType`
ljharb and others added 2 commits September 23, 2019 16:04
…Wrapper; add tests

Technically this could be considered a breaking change since it’s a
`class` and these are public methods on it.

*However*, since nobody should be depending on this package directly,
and since enzyme itself doesn’t appear to use it, I’m going to consider
it a patch. In the event that it is a breaking change for someone, I’ll
add back the methods, ideally as no-ops.
 - [enzyme] [fix] `render`: handle Fiber strings and numbers
 - [enzyme] [new] `Utils`: add `loadCheerioRoot`

Fixes enzymejs#2098
@ottosichert
Copy link
Contributor Author

@ljharb Thanks for wrapping this up!

ljharb added a commit that referenced this pull request Dec 20, 2019
New Stuff
 - `render`: handle Fiber strings and numbers (#2221)

Fixes
 - `shallow`: Share child context logic between `shallow` and `dive` (#2296)
 - `mount`: `children`: include text nodes ($2269)
 - `mount`: `invoke`: use adapter’s `wrapInvoke` if present (#2158)

Docs
 - `mount`/`shallow`: `closest`/`parent`: Add missing arguments description (#2264)
 - `mount`/`shallow`: fix pluralization of “exist” (#2262)
 - `shallow`/`mount`: `simulate`: added functional component example to simulate doc (#2248)
 - `mount`: `debug`: add missing verbose option flag (#2184)
 - `mount`/`shallow`: `update`: fix semantics description (#2194)
 - add missing backticks to linked method names (#2170)
 - `invoke`: Add missing backticks to end of codeblock (#2160)
 - `invoke`: Fix typo (#2167)
 - Explicit React CSS selector syntax description (#2178)

Meta Stuff
 - [meta] add `funding` field
 - [meta] Update airbnb.io URLs to use https (#2222)
 - [deps] update `is-boolean-object`, `is-callable`, `is-number-object`, `is-string`, `enzyme-shallow-equal`, `array.prototype.flat`, `function.prototype.name`, `html-element-map`, `is-r
egex`, `object-inspect`, `object-is`, `object.entries`, `object.vales`, `raf`, `string.prototype.trim`
 - [dev deps] update `eslint`, `eslint-plugin-import`, `eslint-plugin-markdown`, `eslint-plugin-react`, `safe-publish-latest`, `eslint-config-airbnb`, `rimraf`, `safe-publish-latest`, `k
arma-firefox-launcher`, `babel-preset-airbnb`, `glob-gitignore`, `semver`, `eslint-plugin-jsx-a11y`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect behaviour when statically rendering components returning strings
2 participants