dom_utils_spec.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _dom_utils = require("../../display/dom_utils");
  24. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. describe('dom_utils', function () {
  27. describe('DOMSVGFactory', function () {
  28. var svgFactory;
  29. beforeAll(function (done) {
  30. svgFactory = new _dom_utils.DOMSVGFactory();
  31. done();
  32. });
  33. afterAll(function () {
  34. svgFactory = null;
  35. });
  36. it('`create` should throw an error if the dimensions are invalid', function () {
  37. expect(function () {
  38. return svgFactory.create(-1, 0);
  39. }).toThrow(new Error('Invalid SVG dimensions'));
  40. expect(function () {
  41. return svgFactory.create(0, -1);
  42. }).toThrow(new Error('Invalid SVG dimensions'));
  43. });
  44. it('`create` should return an SVG element if the dimensions are valid', function () {
  45. if ((0, _is_node.default)()) {
  46. pending('Document is not supported in Node.js.');
  47. }
  48. var svg = svgFactory.create(20, 40);
  49. expect(svg instanceof SVGSVGElement).toBe(true);
  50. expect(svg.getAttribute('version')).toBe('1.1');
  51. expect(svg.getAttribute('width')).toBe('20px');
  52. expect(svg.getAttribute('height')).toBe('40px');
  53. expect(svg.getAttribute('preserveAspectRatio')).toBe('none');
  54. expect(svg.getAttribute('viewBox')).toBe('0 0 20 40');
  55. });
  56. it('`createElement` should throw an error if the type is not a string', function () {
  57. expect(function () {
  58. return svgFactory.createElement(true);
  59. }).toThrow(new Error('Invalid SVG element type'));
  60. });
  61. it('`createElement` should return an SVG element if the type is valid', function () {
  62. if ((0, _is_node.default)()) {
  63. pending('Document is not supported in Node.js.');
  64. }
  65. var svg = svgFactory.createElement('svg:rect');
  66. expect(svg instanceof SVGRectElement).toBe(true);
  67. });
  68. });
  69. describe('getFilenameFromUrl', function () {
  70. it('should get the filename from an absolute URL', function () {
  71. var url = 'http://server.org/filename.pdf';
  72. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  73. var expected = 'filename.pdf';
  74. expect(result).toEqual(expected);
  75. });
  76. it('should get the filename from a relative URL', function () {
  77. var url = '../../filename.pdf';
  78. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  79. var expected = 'filename.pdf';
  80. expect(result).toEqual(expected);
  81. });
  82. });
  83. });