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 2017 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 = require('../../shared/is_node');
  25. var _is_node2 = _interopRequireDefault(_is_node);
  26. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  27. describe('dom_utils', function () {
  28. describe('DOMSVGFactory', function () {
  29. var svgFactory = void 0;
  30. beforeAll(function (done) {
  31. svgFactory = new _dom_utils.DOMSVGFactory();
  32. done();
  33. });
  34. afterAll(function () {
  35. svgFactory = null;
  36. });
  37. it('`create` should throw an error if the dimensions are invalid', function () {
  38. expect(function () {
  39. return svgFactory.create(-1, 0);
  40. }).toThrow(new Error('Invalid SVG dimensions'));
  41. expect(function () {
  42. return svgFactory.create(0, -1);
  43. }).toThrow(new Error('Invalid SVG dimensions'));
  44. });
  45. it('`create` should return an SVG element if the dimensions are valid', function () {
  46. if ((0, _is_node2.default)()) {
  47. pending('Document is not supported in Node.js.');
  48. }
  49. var svg = svgFactory.create(20, 40);
  50. expect(svg instanceof SVGSVGElement).toBe(true);
  51. expect(svg.getAttribute('version')).toBe('1.1');
  52. expect(svg.getAttribute('width')).toBe('20px');
  53. expect(svg.getAttribute('height')).toBe('40px');
  54. expect(svg.getAttribute('preserveAspectRatio')).toBe('none');
  55. expect(svg.getAttribute('viewBox')).toBe('0 0 20 40');
  56. });
  57. it('`createElement` should throw an error if the type is not a string', function () {
  58. expect(function () {
  59. return svgFactory.createElement(true);
  60. }).toThrow(new Error('Invalid SVG element type'));
  61. });
  62. it('`createElement` should return an SVG element if the type is valid', function () {
  63. if ((0, _is_node2.default)()) {
  64. pending('Document is not supported in Node.js.');
  65. }
  66. var svg = svgFactory.createElement('svg:rect');
  67. expect(svg instanceof SVGRectElement).toBe(true);
  68. });
  69. });
  70. describe('getFilenameFromUrl', function () {
  71. it('should get the filename from an absolute URL', function () {
  72. var url = 'http://server.org/filename.pdf';
  73. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  74. var expected = 'filename.pdf';
  75. expect(result).toEqual(expected);
  76. });
  77. it('should get the filename from a relative URL', function () {
  78. var url = '../../filename.pdf';
  79. var result = (0, _dom_utils.getFilenameFromUrl)(url);
  80. var expected = 'filename.pdf';
  81. expect(result).toEqual(expected);
  82. });
  83. });
  84. });