LazyFactory.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 LazyFactory from '../../../src/dimension/svg/LazyFactory';
  20. describe('LazyFactory', () => {
  21. describe('createInContainer()', () => {
  22. it('creates node in the specified container', () => {
  23. const factory = new LazyFactory(() => document.createElement('div'));
  24. const div = factory.createInContainer();
  25. const innerDiv = factory.createInContainer(div);
  26. expect(div.parentNode).toEqual(document.body);
  27. expect(innerDiv.parentNode).toEqual(div);
  28. });
  29. it('reuses existing', () => {
  30. const factoryFn = jest.fn(() => document.createElement('div'));
  31. const factory = new LazyFactory(factoryFn);
  32. const div1 = factory.createInContainer();
  33. const div2 = factory.createInContainer();
  34. expect(div1).toBe(div2);
  35. expect(factoryFn).toHaveBeenCalledTimes(1);
  36. });
  37. });
  38. describe('removeFromContainer', () => {
  39. it('removes node from container', () => {
  40. const factory = new LazyFactory(() => document.createElement('div'));
  41. const div = factory.createInContainer();
  42. const innerDiv = factory.createInContainer(div);
  43. expect(div.parentNode).toEqual(document.body);
  44. expect(innerDiv.parentNode).toEqual(div);
  45. factory.removeFromContainer();
  46. factory.removeFromContainer(div);
  47. expect(div.parentNode).toBeNull();
  48. expect(innerDiv.parentNode).toBeNull();
  49. });
  50. it('does not remove if others are using', () => {
  51. const factory = new LazyFactory(() => document.createElement('div'));
  52. const div1 = factory.createInContainer();
  53. factory.createInContainer();
  54. factory.removeFromContainer();
  55. expect(div1.parentNode).toEqual(document.body);
  56. factory.removeFromContainer();
  57. expect(div1.parentNode).toBeNull();
  58. });
  59. it('does not crash if try to remove already removed node', () => {
  60. const factory = new LazyFactory(() => document.createElement('div'));
  61. factory.createInContainer();
  62. factory.removeFromContainer();
  63. expect(() => factory.removeFromContainer()).not.toThrow();
  64. });
  65. });
  66. });