prop.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. describe("prop", function () {
  4. var collection;
  5. var emptyCollection;
  6. beforeEach(function () {
  7. domHelper(
  8. "<div id=\"foo\" bar=\"xomba\"></div>" +
  9. "<div id=\"foo2\" bar=\"xomba\"></div>"
  10. );
  11. collection = $("[bar]");
  12. collection.length.should.equal(2);
  13. emptyCollection = $("bingo");
  14. emptyCollection.length.should.equal(0);
  15. });
  16. it("should set multiple properties on members if first arg is an object", function () {
  17. collection.prop({ "bingo": "masters", "break": "out" });
  18. $(collection[0]).prop("bingo").should.equal("masters");
  19. $(collection[1]).prop("bingo").should.equal("masters");
  20. $(collection[0]).prop("break").should.equal("out");
  21. $(collection[1]).prop("break").should.equal("out");
  22. });
  23. it("should set the property of a single member to the specified value", function () {
  24. collection = $("#foo");
  25. collection.length.should.equal(1);
  26. collection.prop("bleep", "booster");
  27. collection.prop("bleep").should.equal("booster");
  28. // check that the other div doesn't have the property
  29. expect($("#foo2").prop("bleep")).to.be.undefined;
  30. });
  31. it("should set the property of each member to the specified value", function () {
  32. collection.prop("bar", "hoopla");
  33. $(collection[0]).prop("bar").should.equal("hoopla");
  34. $(collection[1]).prop("bar").should.equal("hoopla");
  35. });
  36. it("should remove the property from each member if the value is null", function () {
  37. collection.prop("bar", "hoopla");
  38. // remove the property
  39. collection.prop("bar", null);
  40. expect($(collection[0]).prop("bar")).to.be.undefined;
  41. expect($(collection[1]).prop("bar")).to.be.undefined;
  42. });
  43. it("should remove the property from the member and from the cache if value is null", function () {
  44. collection.prop("bar", { "i": "theobject" });
  45. // remove the property
  46. collection.prop("bar", null);
  47. expect($(collection[0]).prop("bar")).to.be.undefined;
  48. expect($(collection[1]).prop("bar")).to.be.undefined;
  49. });
  50. it("should return the property value of the first member", function () {
  51. // properties are separate from attributes, but we set one
  52. // with the same name as an attribute to maximise the possibility
  53. // of overlap/confusion
  54. collection.prop("bar", "hoopla");
  55. collection.prop("bar").should.equal("hoopla");
  56. });
  57. it("should return undefined for an unset property", function () {
  58. expect(collection.prop("definitelynotsetproperty")).to.be.undefined;
  59. });
  60. it("should cache the value when it is an object, array or function", function () {
  61. // object
  62. var value = { a: 1, b: 2 };
  63. collection.prop("underTest", value);
  64. $(collection[0]).prop("underTest").should.equal(value);
  65. $(collection[1]).prop("underTest").should.equal(value);
  66. // array
  67. value = [1, 2, 3];
  68. collection.prop("underTest", value);
  69. $(collection[0]).prop("underTest").should.equal(value);
  70. $(collection[1]).prop("underTest").should.equal(value);
  71. // function
  72. value = function () {
  73. return "hurray!";
  74. };
  75. collection.prop("underTest", value);
  76. $(collection[0]).prop("underTest").should.equal(value);
  77. $(collection[1]).prop("underTest").should.equal(value);
  78. });
  79. it("should reset cached value when set again so cache doesn't go stale", function () {
  80. // set an object so the cache is used
  81. var value = { x: 10, y: 20 };
  82. collection.prop("splendidObject", value);
  83. $(collection[0]).prop("splendidObject").should.equal(value);
  84. $(collection[1]).prop("splendidObject").should.equal(value);
  85. // reset the property value to a different object
  86. var value2 = { z: 28, p: 38 };
  87. collection.prop("splendidObject", value2);
  88. $(collection[0]).prop("splendidObject").should.equal(value2);
  89. $(collection[1]).prop("splendidObject").should.equal(value2);
  90. });
  91. it("should return undefined for an empty collection", function () {
  92. expect(emptyCollection.prop("master")).to.be.undefined;
  93. });
  94. it("should return the empty collection if setting a value on an empty collection", function () {
  95. expect(emptyCollection.prop("master", "hoho")).to.equal(emptyCollection);
  96. });
  97. });