replaceClass.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. describe("replaceClass", function () {
  4. beforeEach(function () {
  5. domHelper(
  6. "<div data-role=\"foo1\" class=\"red\"></div>" +
  7. "<div data-role=\"foo2\" class=\"red\"></div>" +
  8. "<div data-role=\"foo3\" class=\"blue\"></div>"
  9. );
  10. });
  11. it("should replace a class on a single element", function () {
  12. var matching = $(".blue");
  13. matching.length.should.equal(1);
  14. matching.replaceClass("blue", "pink");
  15. var elt = document.querySelector("[data-role=\"foo3\"]");
  16. elt.getAttribute("class").should.equal("pink");
  17. });
  18. it("should replace a class on multiple elements", function () {
  19. var matching = $(".red");
  20. matching.length.should.equal(2);
  21. matching.replaceClass("red", "bronze");
  22. var elts = document.querySelectorAll(".bronze");
  23. elts.length.should.equal(2);
  24. for (var i = 0; i < elts.length; i += 1) {
  25. elts[i].getAttribute("class").should.equal("bronze");
  26. }
  27. var untouched = document.querySelector("[data-role=\"foo3\"]");
  28. untouched.getAttribute("class").should.equal("blue");
  29. });
  30. });
  31. describe("replaceClass", function () {
  32. var stubMatrix = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1 };
  33. beforeEach(function () {
  34. $(document.body).append("<div id=\"moo\"></div>");
  35. });
  36. it("should replace a css class", function () {
  37. var el=document.getElementById("moo");
  38. el.className="foobar";
  39. $(el).replaceClass("foobar","bar");
  40. expect($(el).hasClass("bar")).to.be.true;
  41. expect($(el).hasClass("foobar")).to.be.false;
  42. });
  43. it("should do nothing to the classes", function () {
  44. var el=document.getElementById("moo");
  45. el.className="foobar";
  46. $(el).replaceClass();
  47. expect($(el).hasClass("bar")).to.be.false;
  48. expect($(el).hasClass("foobar")).to.be.true;
  49. });
  50. it("Should add the class",function(){
  51. var el=document.getElementById("moo");
  52. el.className="foobar";
  53. $(el).replaceClass("","bar");
  54. expect($(el).hasClass("bar")).to.be.true;
  55. expect($(el).hasClass("foobar")).to.be.true;
  56. });
  57. it("should replace a css class when multiple classes exist", function () {
  58. var el=document.getElementById("moo");
  59. el.className="foobar temp stuff";
  60. $(el).replaceClass("foobar","bar");
  61. expect($(el).hasClass("bar")).to.be.true;
  62. expect($(el).hasClass("temp")).to.be.true;
  63. expect($(el).hasClass("stuff")).to.be.true;
  64. expect($(el).hasClass("foobar")).to.be.false;
  65. });
  66. it("should replace multiple classess", function () {
  67. var el=document.getElementById("moo");
  68. el.className="foobar temp";
  69. $(el).replaceClass("foobar temp","bar has");
  70. expect($(el).hasClass("bar")).to.be.true;
  71. expect($(el).hasClass("has")).to.be.true;
  72. expect($(el).hasClass("foobar")).to.be.false;
  73. expect($(el).hasClass("temp")).to.be.false;
  74. });
  75. });