attr.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. describe("attr", function () {
  4. beforeEach(function () {
  5. domHelper(
  6. "<div id=\"single1\" class=\"red\" data-spong=\"bang\"></div>" +
  7. "<div id=\"single2\" class=\"red\" data-spong=\"bloing\"></div>"
  8. );
  9. });
  10. it("should set an attribute on a collection of elements", function () {
  11. var divs = $("div");
  12. divs.attr("data-foo", "bar");
  13. var rawDivs = document.querySelectorAll("div");
  14. for (var i = 0; i < rawDivs.length; i += 1) {
  15. rawDivs[i].getAttribute("data-foo").should.equal("bar");
  16. }
  17. });
  18. it("should set attributes from an object on a collection of elements", function () {
  19. var divs = $("div");
  20. divs.attr({
  21. "data-foo": "bar",
  22. "data-ex": "why",
  23. "data-alpha": "zed"
  24. });
  25. var rawDivs = document.querySelectorAll("div");
  26. for (var i = 0; i < rawDivs.length; i += 1) {
  27. rawDivs[i].getAttribute("data-foo").should.equal("bar");
  28. rawDivs[i].getAttribute("data-ex").should.equal("why");
  29. rawDivs[i].getAttribute("data-alpha").should.equal("zed");
  30. }
  31. });
  32. it("should set a (pseudo) attribute to a function, object or array", function () {
  33. // function
  34. var fn = function () {
  35. return "bar";
  36. };
  37. $(".red").attr("data-foo", fn);
  38. $(document.getElementById("single1")).attr("data-foo").should.equal(fn);
  39. $(document.getElementById("single2")).attr("data-foo").should.equal(fn);
  40. // object
  41. var obj = {
  42. a: 1,
  43. b: 2
  44. };
  45. $(".red").attr("data-foo", obj);
  46. $(document.getElementById("single1")).attr("data-foo").should.equal(obj);
  47. $(document.getElementById("single2")).attr("data-foo").should.equal(obj);
  48. // array
  49. var arr = ["a", "b", "c"];
  50. $(".red").attr("data-foo", arr);
  51. $(document.getElementById("single1")).attr("data-foo").should.equal(arr);
  52. $(document.getElementById("single2")).attr("data-foo").should.equal(arr);
  53. });
  54. it("should remove an attribute if value is set to null", function () {
  55. $(".red").attr("data-greet", "hello");
  56. $(".red").attr("data-greet", null);
  57. var elt1 = document.getElementById("single1");
  58. var value1 = elt1.getAttribute("data-greet");
  59. (value1 === null).should.be.true
  60. var elt2 = document.getElementById("single2");
  61. var value2 = elt2.getAttribute("data-greet");
  62. (value2 === null).should.be.true
  63. });
  64. it("should remove a (pseudo) attribute if value is set to null", function () {
  65. $(".red").attr("data-greet", {a: 1, b: 2});
  66. $(".red").attr("data-greet", null);
  67. var elt1 = document.getElementById("single1");
  68. var value1 = elt1.getAttribute("data-greet");
  69. (value1 === null).should.be.true
  70. var elt2 = document.getElementById("single2");
  71. var value2 = elt2.getAttribute("data-greet");
  72. (value2 === null).should.be.true
  73. });
  74. it("should get an attribute's value from a single element", function () {
  75. var actual = $("#single1").attr("data-spong");
  76. actual.should.equal("bang");
  77. });
  78. it("should get the first element's attribute value if called on an af collection", function () {
  79. var actual = $(".red").attr("data-spong");
  80. actual.should.equal("bang");
  81. });
  82. });