Preset.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 { Plugin, Preset } from '@superset-ui/core';
  20. describe('Preset', () => {
  21. it('exists', () => {
  22. expect(Preset).toBeDefined();
  23. });
  24. describe('new Preset()', () => {
  25. it('creates new preset', () => {
  26. const preset = new Preset();
  27. expect(preset).toBeInstanceOf(Preset);
  28. });
  29. });
  30. describe('.register()', () => {
  31. it('register all listed presets then plugins', () => {
  32. const values: number[] = [];
  33. class Plugin1 extends Plugin {
  34. register() {
  35. values.push(1);
  36. return this;
  37. }
  38. }
  39. class Plugin2 extends Plugin {
  40. register() {
  41. values.push(2);
  42. return this;
  43. }
  44. }
  45. class Plugin3 extends Plugin {
  46. register() {
  47. values.push(3);
  48. return this;
  49. }
  50. }
  51. class Plugin4 extends Plugin {
  52. register() {
  53. const { key } = this.config;
  54. values.push(key as number);
  55. return this;
  56. }
  57. }
  58. const preset1 = new Preset({
  59. plugins: [new Plugin1()],
  60. });
  61. const preset2 = new Preset({
  62. plugins: [new Plugin2()],
  63. });
  64. const preset3 = new Preset({
  65. presets: [preset1, preset2],
  66. plugins: [new Plugin3(), new Plugin4().configure({ key: 9 })],
  67. });
  68. preset3.register();
  69. expect(values).toEqual([1, 2, 3, 9]);
  70. });
  71. it('returns itself', () => {
  72. const preset = new Preset();
  73. expect(preset.register()).toBe(preset);
  74. });
  75. });
  76. });