or.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 objectUtils = require('utils/object_utils');
  19. var helpers = App.TestAliases.helpers;
  20. function getFalsyCombination(dependentKeys) {
  21. var hash = {};
  22. dependentKeys.forEach(function (key) {
  23. if (key.startsWith('!')) {
  24. hash[key.substr(1)] = true;
  25. }
  26. else {
  27. hash[key] = false;
  28. }
  29. });
  30. return hash;
  31. }
  32. /**
  33. *
  34. * @param {Em.Object} context
  35. * @param {string} propertyName
  36. * @param {string[]} dependentKeys
  37. */
  38. App.TestAliases.testAsComputedOr = function (context, propertyName, dependentKeys) {
  39. var realKeys = dependentKeys.map(function (key) {
  40. return key.startsWith('!') ? key.substr(1) : key;
  41. });
  42. var falsyCombination = getFalsyCombination(dependentKeys);
  43. var binaryCombos = helpers.getBinaryCombos(realKeys);
  44. describe('#' + propertyName + ' as Em.computed.or', function () {
  45. afterEach(function () {
  46. helpers.smartRestoreGet(context);
  47. });
  48. it('has valid dependent keys', function () {
  49. expect(Em.meta(context).descs[propertyName]._dependentKeys).to.eql(realKeys);
  50. });
  51. binaryCombos.forEach(function (combo) {
  52. var expectedResult = !objectUtils.deepEqual(falsyCombination, combo);
  53. it('`' + expectedResult + '` for ' + JSON.stringify(combo), function() {
  54. helpers.smartStubGet(context, combo)
  55. .propertyDidChange(context, propertyName);
  56. var value = helpers.smartGet(context, propertyName);
  57. expect(value).to.equal(expectedResult);
  58. });
  59. });
  60. });
  61. };