Skip to content

Commit

Permalink
Add a matcher to check that element does not have data attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Krister Kari committed Jun 7, 2015
1 parent 4c85e8d commit 2533898
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/data.js
Expand Up @@ -85,6 +85,7 @@
element.removeAttribute("data-placeholder-has-events");
element.removeAttribute("data-placeholder-active");
element.removeAttribute("data-placeholder-maxlength");
element.removeAttribute("data-placeholder-type");
}

placekeeper.data = {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/hide-on-input.spec.js
Expand Up @@ -225,6 +225,10 @@ describe("hide on input mode", function() {
placekeeper.disable();
});

it("should have remove all data-attributes from element", function() {
expect(element).toHaveNoDataAttributes();
});

it("should have called utils.removeEventListener for keydown handler", function() {
expect(placekeeper.utils.removeEventListener)
.toHaveBeenCalledWith(element, "keydown", placekeeper.events.handlers.keydown);
Expand Down
16 changes: 4 additions & 12 deletions test/unit/public.spec.js
Expand Up @@ -54,12 +54,8 @@ describe("public methods", function() {
expect(element.style.display).toEqual("");
});

it("should not have data-placeholder-has-events attribute", function() {
expect(element.getAttribute("data-placeholder-has-events")).toEqual(null);
});

it("should not have data-placeholder-value attribute", function() {
expect(element.getAttribute("data-placeholder-value")).toEqual(null);
it("should have remove all data-attributes from element", function() {
expect(element).toHaveNoDataAttributes();
});

// In IE7 element is `null`
Expand Down Expand Up @@ -113,12 +109,8 @@ describe("public methods", function() {
placekeeper.disable();
});

it("should not have data-placeholder-has-events attribute", function() {
expect(element.getAttribute("data-placeholder-has-events")).toEqual(null);
});

it("should not have data-placeholder-value attribute", function() {
expect(element.getAttribute("data-placeholder-value")).toEqual(null);
it("should have remove all data-attributes from element", function() {
expect(element).toHaveNoDataAttributes();
});

it("should have called utils.removeEventListener for focus handler", function() {
Expand Down
17 changes: 17 additions & 0 deletions test/utils/matchers.js
@@ -1,5 +1,22 @@
beforeEach(function() {
jasmine.addMatchers({
toHaveNoDataAttributes: function() {
return {
compare: function(actual) {
var dataAttrs = [];
for (var i = 0; i < actual.attributes.length; i++) {
if ((/data\-/).test(actual.attributes[i].nodeName)) {
dataAttrs.push(actual.attributes[i].nodeName);
}
}
var pass = dataAttrs.length === 0;
return {
message: "Expected element to have 0 data-attributes, but got " + dataAttrs,
pass: pass
};
}
};
},
toHaveClass: function() {
return {
compare: function(actual, expected) {
Expand Down

0 comments on commit 2533898

Please sign in to comment.