map.test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. describe("map", function () {
  4. it("should apply a function to each element in a collection", function () {
  5. domHelper("<div id=\"div1\"></div>" +
  6. "<div id=\"div2\"></div>" +
  7. "<div id=\"div3\"></div>");
  8. var expected = "div1,div2,div3";
  9. // test $(selector).map(callback)
  10. var actual1 = $("div").map(function (index, elt) {
  11. return elt.id;
  12. }).get().join(",");
  13. // test $.map(collection, callback)
  14. var count = 0;
  15. var actual2 = $.map($("div"), function (elt, index) {
  16. count += 1;
  17. return elt.id;
  18. }).join(",");
  19. // bug https://github.com/01org/appframework/issues/348
  20. // count.should.equal(3);
  21. actual1.should.equal(expected);
  22. actual2.should.equal(expected);
  23. });
  24. it("should apply a function to elements in an array", function () {
  25. var expected = "a0,b1,c2";
  26. var count = 0;
  27. var actual = $.map(["a", "b", "c"], function (elt, index) {
  28. count += 1;
  29. return elt + index;
  30. }).join(",");
  31. actual.should.equal(expected);
  32. count.should.equal(3);
  33. });
  34. it("should ignore undefined values returned by the callback", function () {
  35. var expected = "a,c";
  36. var actual = $.map(["a", "b", "c"], function (elt) {
  37. if (elt !== "b") {
  38. return elt;
  39. }
  40. else {
  41. return undefined;
  42. }
  43. }).join(",");
  44. actual.should.equal(expected);
  45. });
  46. });