featureFlag.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import * as uiCore from '@superset-ui/core';
  20. test('initializes feature flags', () => {
  21. Object.defineProperty(window, 'featureFlags', {
  22. value: undefined,
  23. });
  24. uiCore.initFeatureFlags();
  25. expect(window.featureFlags).toEqual({});
  26. });
  27. test('initializes feature flags with predefined values', () => {
  28. Object.defineProperty(window, 'featureFlags', {
  29. value: undefined,
  30. });
  31. const featureFlags = {
  32. DRILL_BY: false,
  33. };
  34. uiCore.initFeatureFlags(featureFlags);
  35. expect(window.featureFlags).toEqual(featureFlags);
  36. });
  37. test('does nothing if feature flags are already initialized', () => {
  38. const featureFlags = { DRILL_BY: false };
  39. Object.defineProperty(window, 'featureFlags', {
  40. value: featureFlags,
  41. });
  42. uiCore.initFeatureFlags({ DRILL_BY: true });
  43. expect(window.featureFlags).toEqual(featureFlags);
  44. });
  45. test('returns false and raises console error if feature flags have not been initialized', () => {
  46. const logging = jest.spyOn(uiCore.logging, 'error');
  47. Object.defineProperty(window, 'featureFlags', {
  48. value: undefined,
  49. });
  50. expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.DrillBy)).toEqual(false);
  51. expect(uiCore.logging.error).toHaveBeenCalled();
  52. expect(logging).toHaveBeenCalledWith('Failed to query feature flag DRILL_BY');
  53. });
  54. test('returns false for unset feature flag', () => {
  55. Object.defineProperty(window, 'featureFlags', {
  56. value: {},
  57. });
  58. expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.DrillBy)).toEqual(false);
  59. });
  60. test('returns true for set feature flag', () => {
  61. Object.defineProperty(window, 'featureFlags', {
  62. value: {
  63. DRILL_BY: true,
  64. },
  65. });
  66. expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.DrillBy)).toEqual(true);
  67. });