truncate.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var helpers = App.TestAliases.helpers;
  19. var strTemplate = '0123456789';
  20. /**
  21. * 10 -> '0123456789'
  22. * 5 -> '01234'
  23. * 15 -> '012345678901234'
  24. *
  25. * @param length
  26. * @returns {string}
  27. */
  28. function getStr(length) {
  29. var ret = '';
  30. var n = Math.floor(length / 10);
  31. var m = length % 10;
  32. for (var i = 0; i < n; i++) {
  33. ret += strTemplate;
  34. }
  35. ret += strTemplate.substr(0, m);
  36. return ret;
  37. }
  38. /**
  39. *
  40. * @param {Em.Object} context
  41. * @param {string} propertyName
  42. * @param {string} dependentKey
  43. * @param {number} maxLength
  44. * @param {number} reduceTo
  45. * @param {string} [replacer]
  46. */
  47. App.TestAliases.testAsComputedTruncate = function (context, propertyName, dependentKey, maxLength, reduceTo, replacer) {
  48. var _replacer = arguments.length > 5 ? replacer : '...';
  49. describe('#' + propertyName + ' as Em.computed.truncate', function () {
  50. afterEach(function () {
  51. helpers.smartRestoreGet(context);
  52. });
  53. it('has valid dependent keys', function () {
  54. expect(Em.meta(context).descs[propertyName]._dependentKeys).to.eql([dependentKey]);
  55. });
  56. it('should be truncated if `maxLength` > ' + JSON.stringify(dependentKey) + ' length', function () {
  57. var val = getStr(maxLength + 1);
  58. var expectedValue = val.substr(0, reduceTo) + _replacer;
  59. helpers.smartStubGet(context, dependentKey, val)
  60. .propertyDidChange(context, propertyName);
  61. var value = helpers.smartGet(context, propertyName);
  62. expect(value).to.be.equal(expectedValue);
  63. });
  64. it('should not be truncated if `maxLength` = ' + JSON.stringify(dependentKey) + ' length', function () {
  65. var val = getStr(maxLength);
  66. var expectedValue = val;
  67. helpers.smartStubGet(context, dependentKey, val)
  68. .propertyDidChange(context, propertyName);
  69. var value = helpers.smartGet(context, propertyName);
  70. expect(value).to.be.equal(expectedValue);
  71. });
  72. it('should not be truncated if `maxLength` < ' + JSON.stringify(dependentKey) + ' length', function () {
  73. var val = getStr(maxLength - 1);
  74. var expectedValue = val;
  75. helpers.smartStubGet(context, dependentKey, val)
  76. .propertyDidChange(context, propertyName);
  77. var value = helpers.smartGet(context, propertyName);
  78. expect(value).to.be.equal(expectedValue);
  79. });
  80. it('should be "" if ' + JSON.stringify(dependentKey) + ' value is empty', function () {
  81. var val = null;
  82. var expectedValue = '';
  83. helpers.smartStubGet(context, dependentKey, val)
  84. .propertyDidChange(context, propertyName);
  85. var value = helpers.smartGet(context, propertyName);
  86. expect(value).to.be.equal(expectedValue);
  87. });
  88. });
  89. };