helpers.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /*eslint-disable mocha-cleanup/no-assertions-outside-it */
  18. module.exports = {
  19. /**
  20. * Examples:
  21. * <code>
  22. * var actual = [{a:1, b: [1, 2], c: 3}],
  23. * expected = [{a: 1, b: [1, 2]}];
  24. * nestedExpect(expected, actual); // valid
  25. * </code>
  26. *
  27. * <code>
  28. * var actual = [{a:1, b: [1, 2]}],
  29. * expected = [{a: 1, b: [1, 2], c: 3}];
  30. * nestedExpect(expected, actual); // invalid valid (actual[0] doesn't contains key 'c)
  31. * </code>
  32. * @param {object[]} expected
  33. * @param {object[]} actual
  34. * @method nestedExpect
  35. */
  36. nestedExpect: function (expected, actual) {
  37. expected.forEach(function (group, i) {
  38. Em.keys(group).forEach(function (key) {
  39. if ('array' === Em.typeOf(actual[i][key])) {
  40. expect(group[key]).to.eql(actual[i][key].toArray());
  41. }
  42. else {
  43. expect(group[key]).to.equal(actual[i][key]);
  44. }
  45. });
  46. });
  47. },
  48. /**
  49. * Get arguments for one <code>App.ajax.send</code> call according to the criteria
  50. * Example:
  51. * <pre>
  52. * sinon.stub(App.ajax, 'send', Em.K);
  53. * App.ajax.send({
  54. * name: 'n1',
  55. * sender: {},
  56. * data: {
  57. * f1: 'v1',
  58. * f2: 'v2'
  59. * }
  60. * });
  61. * App.ajax.send({
  62. * name: 'n2',
  63. * sender: {}
  64. * });
  65. * var args = findAjaxRequest('name', 'n1');
  66. * console.log(args); // [{name: 'n1', sender: {}, data: {f1: 'v1', f2: 'v2'}}]
  67. * App.ajax.send.restore();
  68. * </pre>
  69. *
  70. * @param {string} property field to find
  71. * @param {*} value value to find
  72. * @returns {array|null}
  73. */
  74. findAjaxRequest: function(property, value) {
  75. if (Em.typeOf(App.ajax.send.args) !== 'array') {
  76. return null;
  77. }
  78. return App.ajax.send.args.find(function (request) {
  79. return Em.get(request[0], property) === value;
  80. });
  81. },
  82. /**
  83. * Get arguments for several <code>App.ajax.send</code> calls according to the criteria
  84. * Example:
  85. * <pre>
  86. * sinon.stub(App.ajax, 'send', Em.K);
  87. * App.ajax.send({
  88. * name: 'n1',
  89. * sender: {},
  90. * data: {
  91. * f1: 'v1',
  92. * f2: 'v2'
  93. * }
  94. * });
  95. * App.ajax.send({
  96. * name: 'n2',
  97. * sender: {}
  98. * });
  99. * App.ajax.send({
  100. * name: 'n2',
  101. * sender: {},
  102. * data: {
  103. * d1: 1234
  104. * }
  105. * });
  106. * var args = filterAjaxRequests('name', 'n2');
  107. * console.log(args); // [[{name: 'n1', sender: {}}], [{name: 'n2', sender: {}, data: {d1: 1234}}]]
  108. * App.ajax.send.restore();
  109. * </pre>
  110. *
  111. * @param {string} property field to filter
  112. * @param {*} value value to filter
  113. * @returns {array}
  114. */
  115. filterAjaxRequests: function (property, value) {
  116. if (Em.typeOf(App.ajax.send.args) !== 'array') {
  117. return [];
  118. }
  119. return App.ajax.send.args.filter(function (request) {
  120. return Em.get(request[0], property) === value;
  121. });
  122. }
  123. };