Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

Assertion match fails with data attribute #366

Closed
eskimoblood opened this issue Aug 19, 2013 · 6 comments
Closed

Assertion match fails with data attribute #366

eskimoblood opened this issue Aug 19, 2013 · 6 comments
Assignees
Milestone

Comments

@eskimoblood
Copy link

When I use the match assertion to check for a data attribute the assertion fails:

 expect(meElement[0]).toMatch({id: 'id1', 'type':'checkbox', 'data-path':'foo.bar'}); 

while this assertion passes

expect(myElement.data('path')).toEqual('foo.bar');
@augustl
Copy link
Member

augustl commented Aug 27, 2013

Thanks! @cjohansen is the assertion guy, but afaik the assertion system has no special handling of DOM elements, it just checks if theElement[prop] is identical to expectedObj[prop]. And:

var el = document.createElement("div");
el.setAttribute("data-foo", "foo");
el.getAttribute("data-foo") // "foo"
el["data-foo"] // undefined

It makes sense to me that we have special handling of DOM elements, so that we can easily assert things like data-xxx props.

@cjohansen
Copy link
Member

Meh, not so sure about this situation. We actually do handle DOM elements specially when there are two of them. But for this case, I would perhaps suggest a custom assertion. How can we solve this in a general matter in samsam/referee?

@augustl
Copy link
Member

augustl commented Aug 27, 2013

We could have a separate assertion for DOM element attributes.

assert.attributes(domNode, {"className": /\bmy-class\b/, "data-id": "520"})

But why not let assert.match be polymorphic, and trigger DOM node attribute match when actual is an element and expected is an object?

Alternatively, we can wrap elements and use assert.match(zing(element), {"data-foo": "foo"}). zing could turn the DOM element into a plain object.

@ghost ghost assigned cjohansen Sep 5, 2013
@dwittner
Copy link
Member

What do you think about this solution:

if (matcher && typeof matcher === "object") {
    var prop;
    for (prop in matcher) {
        var value = object[prop] || object.getAttribute ? object.getAttribute(prop) : undefined; 
        if (!match(value, matcher[prop])) {
            return false;
        }
    }
    return true;
}

@cjohansen
Copy link
Member

I like it. It's a nice and pragmatic approach to the problem, making assert.match behave like expected. 👍

@dwittner
Copy link
Member

Implemented by d6d507e925.

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

No branches or pull requests

4 participants