file_utils_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var fileUtils = require('utils/file_utils');
  19. describe('file_utils', function () {
  20. describe('#openInfoInNewTab', function () {
  21. var mock = {
  22. document: {
  23. write: Em.K
  24. },
  25. focus: Em.K
  26. };
  27. beforeEach(function () {
  28. sinon.stub(window, 'open').returns(mock);
  29. sinon.spy(mock.document, 'write');
  30. sinon.spy(mock, 'focus');
  31. fileUtils.openInfoInNewTab('data');
  32. });
  33. afterEach(function () {
  34. window.open.restore();
  35. mock.document.write.restore();
  36. mock.focus.restore();
  37. });
  38. it('opening new window', function () {
  39. expect(window.open.calledOnce).to.be.true;
  40. });
  41. it('no URL for new window', function () {
  42. expect(window.open.firstCall.args).to.eql(['']);
  43. });
  44. it('writing document contents', function () {
  45. expect(mock.document.write.calledOnce).to.be.true;
  46. });
  47. it('document contents', function () {
  48. expect(mock.document.write.firstCall.args).to.eql(['data']);
  49. });
  50. it('focusing on new window', function () {
  51. expect(mock.focus.calledOnce).to.be.true;
  52. });
  53. });
  54. describe('#safariDownload', function () {
  55. var linkEl = {
  56. click: Em.K
  57. };
  58. beforeEach(function () {
  59. sinon.stub(document, 'createElement').returns(linkEl);
  60. sinon.stub(document.body, 'appendChild', Em.K);
  61. sinon.stub(document.body, 'removeChild', Em.K);
  62. sinon.spy(linkEl, 'click');
  63. fileUtils.safariDownload('file data', 'csv', 'file.csv');
  64. });
  65. afterEach(function () {
  66. document.createElement.restore();
  67. document.body.appendChild.restore();
  68. document.body.removeChild.restore();
  69. linkEl.click.restore();
  70. });
  71. it('creating new element', function () {
  72. expect(document.createElement.calledOnce).to.be.true;
  73. });
  74. it('new element is a link', function () {
  75. expect(document.createElement.firstCall.args).to.eql(['a']);
  76. });
  77. it('link URL', function () {
  78. expect(linkEl.href).to.equal('data:attachment/csv;charset=utf-8,file%20data');
  79. });
  80. it('file name', function () {
  81. expect(linkEl.download).to.equal('file.csv');
  82. });
  83. it('appending element to document', function () {
  84. expect(document.body.appendChild.calledOnce).to.be.true;
  85. });
  86. it('link is appended', function () {
  87. expect(document.body.appendChild.firstCall.args).to.eql([linkEl]);
  88. });
  89. it('link is clicked', function () {
  90. expect(linkEl.click.calledOnce).to.be.true;
  91. });
  92. it('removing element from document', function () {
  93. expect(document.body.removeChild.calledOnce).to.be.true;
  94. });
  95. it('link is removed', function () {
  96. expect(document.body.removeChild.firstCall.args).to.eql([linkEl]);
  97. });
  98. });
  99. });