makeSingleton.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 { makeSingleton } from '@superset-ui/core';
  20. describe('makeSingleton()', () => {
  21. class Dog {
  22. name: string;
  23. isSitting?: boolean;
  24. constructor(name?: string) {
  25. this.name = name || 'Pluto';
  26. }
  27. sit() {
  28. this.isSitting = true;
  29. }
  30. }
  31. describe('makeSingleton(BaseClass)', () => {
  32. const getInstance = makeSingleton(Dog);
  33. it('returns a function for getting singleton instance of a given base class', () => {
  34. expect(typeof getInstance).toBe('function');
  35. expect(getInstance()).toBeInstanceOf(Dog);
  36. });
  37. it('returned function returns same instance across all calls', () => {
  38. expect(getInstance()).toBe(getInstance());
  39. });
  40. });
  41. describe('makeSingleton(BaseClass, ...args)', () => {
  42. const getInstance = makeSingleton(Dog, 'Doug');
  43. it('returns a function for getting singleton instance of a given base class constructed with the given arguments', () => {
  44. expect(typeof getInstance).toBe('function');
  45. expect(getInstance()).toBeInstanceOf(Dog);
  46. expect(getInstance().name).toBe('Doug');
  47. });
  48. it('returned function returns same instance across all calls', () => {
  49. expect(getInstance()).toBe(getInstance());
  50. });
  51. });
  52. });