RegistryWithDefaultKey.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 { Registry, RegistryWithDefaultKey } from '@superset-ui/core';
  20. describe('RegistryWithDefaultKey', () => {
  21. let registry: RegistryWithDefaultKey<number>;
  22. beforeEach(() => {
  23. registry = new RegistryWithDefaultKey();
  24. });
  25. it('exists', () => {
  26. expect(RegistryWithDefaultKey).toBeDefined();
  27. });
  28. describe('new RegistryWithDefaultKey(config)', () => {
  29. it('returns a class that extends from Registry', () => {
  30. expect(registry).toBeInstanceOf(Registry);
  31. });
  32. });
  33. describe('.clear()', () => {
  34. it('also resets default key', () => {
  35. registry.setDefaultKey('abc');
  36. registry.clear();
  37. expect(registry.getDefaultKey()).toBeUndefined();
  38. });
  39. it('returns itself', () => {
  40. expect(registry.clear()).toBe(registry);
  41. });
  42. });
  43. describe('.get()', () => {
  44. beforeEach(() => {
  45. registry
  46. .registerValue('abc', 100)
  47. .registerValue('def', 200)
  48. .setDefaultKey('abc');
  49. });
  50. it('.get() returns value from default key', () => {
  51. expect(registry.get()).toEqual(100);
  52. });
  53. it('.get(key) returns value from specified key', () => {
  54. expect(registry.get('def')).toEqual(200);
  55. });
  56. it('returns undefined if no key was given and there is no default key', () => {
  57. registry.clearDefaultKey();
  58. expect(registry.get()).toBeUndefined();
  59. });
  60. });
  61. describe('.getDefaultKey()', () => {
  62. it('returns defaultKey', () => {
  63. registry.setDefaultKey('abc');
  64. expect(registry.getDefaultKey()).toEqual('abc');
  65. });
  66. });
  67. describe('.setDefaultKey(key)', () => {
  68. it('set the default key', () => {
  69. registry.setDefaultKey('abc');
  70. expect(registry.defaultKey).toEqual('abc');
  71. });
  72. it('returns itself', () => {
  73. expect(registry.setDefaultKey('ghi')).toBe(registry);
  74. });
  75. });
  76. describe('.clearDefaultKey()', () => {
  77. it('set the default key to undefined', () => {
  78. registry.clearDefaultKey();
  79. expect(registry.defaultKey).toBeUndefined();
  80. });
  81. it('returns itself', () => {
  82. expect(registry.clearDefaultKey()).toBe(registry);
  83. });
  84. });
  85. describe('config.defaultKey', () => {
  86. describe('when not set', () => {
  87. it(`After creation, default key is undefined`, () => {
  88. expect(registry.defaultKey).toBeUndefined();
  89. });
  90. it('.clear() reset defaultKey to undefined', () => {
  91. registry.setDefaultKey('abc');
  92. registry.clear();
  93. expect(registry.getDefaultKey()).toBeUndefined();
  94. });
  95. });
  96. describe('when config.initialDefaultKey is set', () => {
  97. const registry2 = new RegistryWithDefaultKey({
  98. initialDefaultKey: 'def',
  99. });
  100. it(`After creation, default key is undefined`, () => {
  101. expect(registry2.defaultKey).toEqual('def');
  102. });
  103. it('.clear() reset defaultKey to this config.defaultKey', () => {
  104. registry2.setDefaultKey('abc');
  105. registry2.clear();
  106. expect(registry2.getDefaultKey()).toEqual('def');
  107. });
  108. });
  109. });
  110. describe('config.setFirstItemAsDefault', () => {
  111. describe('when true', () => {
  112. const registry2 = new RegistryWithDefaultKey({
  113. setFirstItemAsDefault: true,
  114. });
  115. beforeEach(() => {
  116. registry2.clear();
  117. });
  118. describe('.registerValue(key, value)', () => {
  119. it('sets the default key to this key if default key is not set', () => {
  120. registry2.registerValue('abc', 100);
  121. expect(registry2.getDefaultKey()).toEqual('abc');
  122. });
  123. it('does not modify the default key if already set', () => {
  124. registry2.setDefaultKey('def').registerValue('abc', 100);
  125. expect(registry2.getDefaultKey()).toEqual('def');
  126. });
  127. it('returns itself', () => {
  128. expect(registry2.registerValue('ghi', 300)).toBe(registry2);
  129. });
  130. });
  131. describe('.registerLoader(key, loader)', () => {
  132. it('sets the default key to this key if default key is not set', () => {
  133. registry2.registerLoader('abc', () => 100);
  134. expect(registry2.getDefaultKey()).toEqual('abc');
  135. });
  136. it('does not modify the default key if already set', () => {
  137. registry2.setDefaultKey('def').registerLoader('abc', () => 100);
  138. expect(registry2.getDefaultKey()).toEqual('def');
  139. });
  140. it('returns itself', () => {
  141. expect(registry2.registerLoader('ghi', () => 300)).toBe(registry2);
  142. });
  143. });
  144. });
  145. describe('when false', () => {
  146. const registry2 = new RegistryWithDefaultKey({
  147. setFirstItemAsDefault: false,
  148. });
  149. beforeEach(() => {
  150. registry2.clear();
  151. });
  152. describe('.registerValue(key, value)', () => {
  153. it('does not modify default key', () => {
  154. registry2.registerValue('abc', 100);
  155. expect(registry2.defaultKey).toBeUndefined();
  156. registry2.setDefaultKey('def');
  157. registry2.registerValue('ghi', 300);
  158. expect(registry2.defaultKey).toEqual('def');
  159. });
  160. it('returns itself', () => {
  161. expect(registry2.registerValue('ghi', 300)).toBe(registry2);
  162. });
  163. });
  164. describe('.registerLoader(key, loader)', () => {
  165. it('does not modify default key', () => {
  166. registry2.registerValue('abc', () => 100);
  167. expect(registry2.defaultKey).toBeUndefined();
  168. registry2.setDefaultKey('def');
  169. registry2.registerValue('ghi', () => 300);
  170. expect(registry2.defaultKey).toEqual('def');
  171. });
  172. it('returns itself', () => {
  173. expect(registry2.registerLoader('ghi', () => 300)).toBe(registry2);
  174. });
  175. });
  176. });
  177. });
  178. });