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

Use Iterable.isIterable for Immutable check #36

Merged
merged 1 commit into from
Mar 14, 2016

Conversation

jakelazaroff
Copy link
Contributor

Right now the check for whether an object is Immutable uses instanceof Collection, which returns false if different modules are using different copies of Immutable.

This PR replaces that with Immutable.Iterable.isIterable as advised here: http://stackoverflow.com/questions/31907470/how-to-check-if-object-is-immutable

@astorije
Copy link
Owner

Hey @jakelazaroff, thanks for your contribution! Good catch too!
Before I can merge this, could you think of a test case where the current solution fails and yours passes please? :-)

@jakelazaroff
Copy link
Contributor Author

No problem 😁 I'll update the PR in a few minutes.

@@ -68,6 +71,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given an empty collection', function () {
fail(function () { expect(new List()).to.not.be.empty; });
});

if (typeEnv === 'Node.js') {
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 couldn't figure out how to load a different copy of Immutable for the Phantom tests, but I don't think it should really be a problem anyway since in a browser everything will just use whatever's on the window object. I also don't love wrapping its inside a conditional — let me know if there's a way you'd rather I write this!

@jakelazaroff
Copy link
Contributor Author

@astorije Updated the PR but there's some work to be done still. After adding the tests I realized there were more instanceofs to replace, which for the most part was straightforward except for the keys method: https://github.com/astorije/chai-immutable/blob/master/chai-immutable.js#L220

It's not exactly clear (to me, at least) how these map to the static methods listed here: https://facebook.github.io/immutable-js/docs/#/Iterable/isIterable I'm guessing instanceof KeyedCollection could be replaced with isKeyed and instanceof IndexedCollection could be replaced with isIndexed, but I'm not sure about instanceof SetCollection (isAssociative, maybe?)

@jakelazaroff
Copy link
Contributor Author

Also: trying to figure out now how to replace these assertions with the Iterable static methods: https://github.com/astorije/chai-immutable/blob/master/chai-immutable.js#L312

@jakelazaroff
Copy link
Contributor Author

@astorije Should be fixed! I replaced the new Assertion checks with this: https://github.com/astorije/chai-immutable/pull/36/files#diff-27f88d01c83c19b108b361c0975ed547R27

For the keys method, I compared the instanceof checks with this table:

screen shot 2015-12-15 at 1 22 17 am

Looks like anything that returns true for isIndexed you can call toJS on and it'll get the keys, so that check now catches List and Stack. The rest just fall through to a general isIterable check before they get to Object.keys.

Let me know if there's anything you'd like me to add/change 😄

@jakelazaroff
Copy link
Contributor Author

Anything I can do to help get this merged?

@astorije
Copy link
Owner

astorije commented Jan 6, 2016

Hi @jakelazaroff, so sorry I have been very busy and I must admit I dropped the ball, shame on me...
I can't take a close look at that right now, but I'll try within a week or two. If I don't, well, I will have dropped the ball again... you know where to ping me!

I don't like the idea of wrapping it in conditions. I'll think of something but at the very least this.skip(); looks better to me (I was not too happy with typeEnv when I added it but I found it a cheap way to display the env). Of course, none of these are ideal, it would be better to actually make the tests relevant on all environments. Again, I'll think of something.
For the rest, well, I need to spend time on this :-)

Thanks again!

@jakelazaroff
Copy link
Contributor Author

No worries! I'll try to figure out a way to get Phantom to load two different copies of Immutable; that should avoid the test-skipping issue entirely.

@@ -5,6 +5,7 @@ if (!chai) {
var chai = require('chai');
var chaiImmutable = require('../chai-immutable');
var Immutable = require('immutable');
var otherImmutable = require('../node_modules/immutable/dist/immutable.min.js');

chai.use(chaiImmutable);
typeEnv = 'Node.js';
Copy link
Owner

Choose a reason for hiding this comment

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

Another way to do that, somewhat cleaner and most likely compatible with PhantomJS, but probably a bit more difficult to achieve, is to deep clone/duplicate Immutable right before var assert = chai.assert; and assign this copy to your otherImmutable (that could be renamed clonedImmutable accordingly).

Then I believe you can remove all those if (typeEnv === 'Node.js') and let the runners test these.

Would you mind exploring this idea before we can merge?

Thanks!

@astorije
Copy link
Owner

@jakelazaroff, I left you a comment inline that might address this problem. Let me know what you think and when you think you'll reach to a solution: I'm sorry to see your PR hanging around and would like to help you having it ready for production!

@astorije
Copy link
Owner

@jakelazaroff, do you have any update on this? :-)

@astorije
Copy link
Owner

@jakelazaroff, ping ping ping? :-)

@jakelazaroff
Copy link
Contributor Author

@astorije Crap, I seriously dropped the ball on this. I'll update by the end of the week. Sorry about the delay!

@jakelazaroff
Copy link
Contributor Author

@astorije Your hunch worked! Should be good to go now.

@@ -68,6 +82,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given an empty collection', function () {
fail(function () { expect(new List()).to.not.be.empty; });
});

it('should work if using different copies of Immtuable', function () {
Copy link
Owner

Choose a reason for hiding this comment

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

Typo at Immtuable.

@astorije
Copy link
Owner

Hi @jakelazaroff, sorry I didn't get back to you earlier!
It starts to look good, almost there! I left you a few comments inline, basically about typos and the fact that tests probably don't reflect the exact issue you opened your PR with.

Also, could you squash your commits? :-)
I'll try to be more responsive next time!

@jakelazaroff
Copy link
Contributor Author

Done! I think this should do it :)

@astorije
Copy link
Owner

Very nice job @jakelazaroff, sorry it took so long!
👍

astorije added a commit that referenced this pull request Mar 14, 2016
Use Iterable.isIterable for Immutable check
@astorije astorije merged commit f77dbc7 into astorije:master Mar 14, 2016
@jakelazaroff jakelazaroff deleted the use-is-iterable branch March 15, 2016 03:58
@jakelazaroff
Copy link
Contributor Author

Awesome, thanks so much!

Side note: if you ever need to use this with karma, I created this adapter for it https://github.com/jakelazaroff/karma-chai-immutable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants