shared-modules.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 { defineSharedModule, defineSharedModules, reset } from '../../src';
  20. describe('shared modules', () => {
  21. afterEach(() => {
  22. reset();
  23. });
  24. it('assigns to window', async () => {
  25. const fakeModule = { foo: 'bar' };
  26. const fetchModule = jest.fn().mockResolvedValue(fakeModule);
  27. await defineSharedModule('test-module', fetchModule);
  28. expect((window as any)['__superset__/test-module']).toStrictEqual(
  29. fakeModule,
  30. );
  31. });
  32. it('resolves to the same reference every time', async () => {
  33. const fakeModule = { foo: 'bar' };
  34. const fetchModule = jest.fn().mockResolvedValue(fakeModule);
  35. const result1 = await defineSharedModule('test-module', fetchModule);
  36. const result2 = await defineSharedModule('test-module', fetchModule);
  37. expect(result1).toStrictEqual(fakeModule);
  38. expect(result2).toStrictEqual(fakeModule);
  39. });
  40. it('does not redefine unnecessarily', async () => {
  41. const fakeModule = { foo: 'bar' };
  42. const fetchModule = jest.fn().mockResolvedValue(fakeModule);
  43. const duplicateFetchModule = jest.fn().mockResolvedValue(fakeModule);
  44. const result1 = await defineSharedModule('test-module', fetchModule);
  45. const result2 = await defineSharedModule(
  46. 'test-module',
  47. duplicateFetchModule,
  48. );
  49. expect(result1).toStrictEqual(fakeModule);
  50. expect(result2).toStrictEqual(fakeModule);
  51. expect(duplicateFetchModule).not.toHaveBeenCalled();
  52. });
  53. it('deduplicates in-progress definitions', async () => {
  54. const fakeModule = { foo: 'bar' };
  55. // get a promise that actually takes a moment;
  56. const fetchModule = jest
  57. .fn()
  58. .mockImplementation(() =>
  59. Promise.resolve(setImmediate).then(() => fakeModule),
  60. );
  61. const promise1 = defineSharedModule('test-module', fetchModule);
  62. const promise2 = defineSharedModule('test-module', fetchModule);
  63. const [result1, result2] = await Promise.all([promise1, promise2]);
  64. expect(fetchModule).toHaveBeenCalledTimes(1);
  65. expect(result1).toStrictEqual(result2);
  66. });
  67. it('shares a map of modules', async () => {
  68. const foo = { foo: 'bar' };
  69. const fizz = { fizz: 'buzz' };
  70. await defineSharedModules({
  71. 'module-foo': async () => foo,
  72. 'module-fizz': async () => fizz,
  73. });
  74. expect((window as any)['__superset__/module-foo']).toEqual(foo);
  75. expect((window as any)['__superset__/module-fizz']).toEqual(fizz);
  76. });
  77. });