chai-jquery icon indicating copy to clipboard operation
chai-jquery copied to clipboard

Add ability to specify jQuery object

Open knownasilya opened this issue 11 years ago • 9 comments

At the moment chai-jquery assumes jQuery is jQuery on the window object, but this isn't always the case, e.g. Ember has Ember.$. Allow an option to specify the jQuery object.

knownasilya avatar Jan 21 '14 20:01 knownasilya

I'm dealing with a similar issue: multiple jQuery instances. How about replacing this:

if (obj instanceof $) { /* ... */ }

to this:

if ('jquery' in Object(obj)) { /* ... */ }

Based on this SO answer. Duck test instead of instance test.

tadas-s avatar Dec 23 '14 15:12 tadas-s

Should probably be 'jQuery', no?

knownasilya avatar Dec 23 '14 15:12 knownasilya

Nope. It's just a property where jQuery provides version:

> 'jquery' in Object($("body"))
true
> $("body").jquery
"1.11.0"

tadas-s avatar Dec 23 '14 16:12 tadas-s

Ah, make sense. Always forget that's how you get the version, kinda off putting.

knownasilya avatar Dec 23 '14 16:12 knownasilya

Related to this issue, I was thinking of using chai-jquery with zombie.js. Jquery is available from browser.window.$. Now I have to write following after each page load, since browser.window.$ will be new after each page load:

before(function () {
  chai.use(function (chai, utils) {
    return chaiJquery(chai, utils, browser.window.$)
  })
})

What if users could implement a function that is called for every assertion:

chai.use(function (chai, utils) {
  return chaiJquery(chai, utils, function() { return browser.window.$})
})

eeroan avatar Mar 18 '15 10:03 eeroan

Would really love to see this. I'm in a similar situation where the hard coded instanceof check is really problematic. I have multiple versions of jQuery and have to do some crazy juggling to make this work.

brian-mann avatar Apr 20 '15 03:04 brian-mann

I made a pull request https://github.com/chaijs/chai-jquery/pull/66

eeroan avatar Apr 21 '15 14:04 eeroan

This pull request would fix my issue, any reason why it can't move ahead?

brian-mann avatar Apr 27 '15 04:04 brian-mann

+1

jgerigmeyer avatar Jul 20 '15 15:07 jgerigmeyer