Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

browser_handler.spec.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. var browser_handler = require('../bin/template/cordova/browser_handler');
  18. var shell = require('shelljs');
  19. var fs = require('fs');
  20. var path = require('path');
  21. describe('Asset install tests', function () {
  22. var fsstatMock;
  23. var asset = { itemType: 'asset',
  24. src: path.join('someSrc', 'ServiceWorker.js'),
  25. target: 'ServiceWorker.js' };
  26. var assetWithPath = { itemType: 'asset',
  27. src: path.join('someSrc', 'reformat.js'),
  28. target: path.join('js', 'deepdown', 'reformat.js') };
  29. var assetWithPath2 = { itemType: 'asset',
  30. src: path.join('someSrc', 'reformat.js'),
  31. target: path.join('js', 'deepdown', 'reformat2.js') };
  32. var plugin_dir = 'pluginDir';
  33. var wwwDest = 'dest';
  34. it('if src is a directory, should be called with cp, -Rf', function () {
  35. var cp = spyOn(shell, 'cp').and.returnValue('-Rf');
  36. fsstatMock = {
  37. isDirectory: function () {
  38. return true;
  39. }
  40. };
  41. spyOn(fs, 'statSync').and.returnValue(fsstatMock);
  42. browser_handler.asset.install(asset, plugin_dir, wwwDest);
  43. expect(cp).toHaveBeenCalledWith('-Rf', jasmine.any(String), path.join('dest', asset.target));
  44. });
  45. it('if src is not a directory and asset has no path, should be called with cp, -f', function () {
  46. var cp = spyOn(shell, 'cp').and.returnValue('-f');
  47. var mkdir = spyOn(shell, 'mkdir');
  48. spyOn(fs, 'existsSync').and.returnValue(true);
  49. fsstatMock = {
  50. isDirectory: function () {
  51. return false;
  52. }
  53. };
  54. spyOn(fs, 'statSync').and.returnValue(fsstatMock);
  55. browser_handler.asset.install(asset, plugin_dir, wwwDest);
  56. expect(mkdir).not.toHaveBeenCalled();
  57. expect(cp).toHaveBeenCalledWith('-f', path.join('pluginDir', asset.src), path.join('dest', asset.target));
  58. });
  59. it('if src is not a directory and asset has a path, should be called with cp, -f', function () {
  60. /*
  61. Test that a dest directory gets created if it does not exist
  62. */
  63. var cp = spyOn(shell, 'cp').and.returnValue('-f');
  64. var mkdir = spyOn(shell, 'mkdir');
  65. fsstatMock = {
  66. isDirectory: function () {
  67. return false;
  68. }
  69. };
  70. spyOn(fs, 'statSync').and.returnValue(fsstatMock);
  71. browser_handler.asset.install(assetWithPath, plugin_dir, wwwDest);
  72. expect(mkdir).toHaveBeenCalledWith('-p', path.join('dest', 'js', 'deepdown'));
  73. expect(cp).toHaveBeenCalledWith('-f', path.join('pluginDir', assetWithPath.src),
  74. path.join('dest', assetWithPath.target));
  75. /*
  76. Now test that a second call to the same dest folder skips mkdir because the first asset call should have created it.
  77. */
  78. spyOn(fs, 'existsSync').and.returnValue(true);
  79. browser_handler.asset.install(assetWithPath2, plugin_dir, wwwDest);
  80. expect(mkdir.calls.count()).toBe(1); // not called again
  81. });
  82. });