each.test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. describe("each", function () {
  4. beforeEach(function () {
  5. domHelper(
  6. "<div data-alpha=\"a\"></div>" +
  7. "<div data-alpha=\"b\"></div>" +
  8. "<div data-alpha=\"c\"></div>"
  9. );
  10. });
  11. it("should iterate over members of an af collection", function () {
  12. var expected = "a,b,c";
  13. var collected = [];
  14. var count = 0;
  15. $("div").each(function (index, item) {
  16. count += 1;
  17. collected.push(item.getAttribute("data-alpha"));
  18. });
  19. var actual = collected.join(",");
  20. actual.should.equal(expected);
  21. count.should.equal(3);
  22. });
  23. it("should iterate over members of an af collection passed to $.each", function () {
  24. var expected = "a,b,c";
  25. var collected = [];
  26. var count = 0;
  27. $.each($("div"), function (index, item) {
  28. count += 1;
  29. // work around for bug #348
  30. if (typeof item === "object") {
  31. collected.push(item.getAttribute("data-alpha"));
  32. }
  33. });
  34. var actual = collected.join(",");
  35. actual.should.equal(expected);
  36. // bug https://github.com/01org/appframework/issues/348
  37. // count.should.equal(3);
  38. });
  39. it("should iterate over an array", function () {
  40. var arr = ["a", "b", "c"];
  41. var actual = "";
  42. $.each(arr, function (index, item) {
  43. actual += item;
  44. });
  45. var expected = "abc";
  46. actual.should.equal(expected);
  47. });
  48. });