helpers.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. module.exports = {
  18. /**
  19. * Examples:
  20. * <code>
  21. * var actual = [{a:1, b: [1, 2], c: 3}],
  22. * expected = [{a: 1, b: [1, 2]}];
  23. * nestedExpect(expected, actual); // valid
  24. * </code>
  25. *
  26. * <code>
  27. * var actual = [{a:1, b: [1, 2]}],
  28. * expected = [{a: 1, b: [1, 2], c: 3}];
  29. * nestedExpect(expected, actual); // invalid valid (actual[0] doesn't contains key 'c)
  30. * </code>
  31. * @param {object[]} expected
  32. * @param {object[]} actual
  33. * @method nestedExpect
  34. */
  35. nestedExpect: function (expected, actual) {
  36. expected.forEach(function (group, i) {
  37. Em.keys(group).forEach(function (key) {
  38. if ('array' === Em.typeOf(actual[i][key])) {
  39. expect(group[key]).to.eql(actual[i][key].toArray());
  40. }
  41. else {
  42. expect(group[key]).to.equal(actual[i][key]);
  43. }
  44. });
  45. });
  46. },
  47. /**
  48. * Get arguments for one <code>App.ajax.send</code> call according to the criteria
  49. * Example:
  50. * <pre>
  51. * sinon.stub(App.ajax, 'send', Em.K);
  52. * App.ajax.send({
  53. * name: 'n1',
  54. * sender: {},
  55. * data: {
  56. * f1: 'v1',
  57. * f2: 'v2'
  58. * }
  59. * });
  60. * App.ajax.send({
  61. * name: 'n2',
  62. * sender: {}
  63. * });
  64. * var args = findAjaxRequest('name', 'n1');
  65. * console.log(args); // [{name: 'n1', sender: {}, data: {f1: 'v1', f2: 'v2'}}]
  66. * App.ajax.send.restore();
  67. * </pre>
  68. *
  69. * @param {string} property field to find
  70. * @param {*} value value to find
  71. * @returns {array|null}
  72. */
  73. findAjaxRequest: function(property, value) {
  74. if (Em.typeOf(App.ajax.send.args) !== 'array') {
  75. return null;
  76. }
  77. return App.ajax.send.args.find(function (request) {
  78. return Em.get(request[0], property) === value;
  79. });
  80. },
  81. /**
  82. * Get arguments for several <code>App.ajax.send</code> calls according to the criteria
  83. * Example:
  84. * <pre>
  85. * sinon.stub(App.ajax, 'send', Em.K);
  86. * App.ajax.send({
  87. * name: 'n1',
  88. * sender: {},
  89. * data: {
  90. * f1: 'v1',
  91. * f2: 'v2'
  92. * }
  93. * });
  94. * App.ajax.send({
  95. * name: 'n2',
  96. * sender: {}
  97. * });
  98. * App.ajax.send({
  99. * name: 'n2',
  100. * sender: {},
  101. * data: {
  102. * d1: 1234
  103. * }
  104. * });
  105. * var args = filterAjaxRequests('name', 'n2');
  106. * console.log(args); // [[{name: 'n1', sender: {}}], [{name: 'n2', sender: {}, data: {d1: 1234}}]]
  107. * App.ajax.send.restore();
  108. * </pre>
  109. *
  110. * @param {string} property field to filter
  111. * @param {*} value value to filter
  112. * @returns {array}
  113. */
  114. filterAjaxRequests: function (property, value) {
  115. if (Em.typeOf(App.ajax.send.args) !== 'array') {
  116. return [];
  117. }
  118. return App.ajax.send.args.filter(function (request) {
  119. return Em.get(request[0], property) === value;
  120. });
  121. }
  122. };