cordova.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* global cordova:true */
  2. /*!
  3. * Module dependencies.
  4. */
  5. /**
  6. * cordova.js for node.
  7. *
  8. * Think of this as cordova-node, which would be simliar to cordova-android
  9. * or cordova-browser. The purpose of this module is to enable testing
  10. * of a plugin's JavaScript interface.
  11. *
  12. * When this module is first required, it will insert a global cordova
  13. * instance, which can hijack cordova-specific commands within the pluin's
  14. * implementation.
  15. *
  16. * Remember to require this module before the plugin that you want to test.
  17. *
  18. * Example:
  19. *
  20. * var cordova = require('./helper/cordova'),
  21. * myPlugin = require('../www/myPlugin');
  22. */
  23. module.exports = global.cordova = cordova = {
  24. /**
  25. * cordova.require Mock.
  26. *
  27. * Hijacks all cordova.requires. By default, it returns an empty function.
  28. * You can define your own implementation of each required module before
  29. * or after it has been required.
  30. *
  31. * See `cordova.required` to learn how to add your own module implemtnation.
  32. */
  33. require: function(moduleId) {
  34. // define a default function if it doesn't exist
  35. if (!cordova.required[moduleId]) {
  36. cordova.required[moduleId] = function() {};
  37. }
  38. // create a new module mapping between the module Id and cordova.required.
  39. return new ModuleMap(moduleId);
  40. },
  41. /**
  42. * Cordova module implementations.
  43. *
  44. * A key-value hash, where the key is the module such as 'cordova/exec'
  45. * and the value is the function or object returned.
  46. *
  47. * For example:
  48. *
  49. * var exec = require('cordova/exec');
  50. *
  51. * Will map to:
  52. *
  53. * cordova.required['cordova/exec'];
  54. */
  55. required: {
  56. // populated at runtime
  57. }
  58. };
  59. /**
  60. * Module Mapper.
  61. *
  62. * Returns a function that when executed will lookup the implementation
  63. * in cordova.required[id].
  64. *
  65. * @param {String} moduleId is the module name/path, such as 'cordova/exec'
  66. * @return {Function}.
  67. */
  68. function ModuleMap(moduleId) {
  69. return function() {
  70. // lookup and execute the module's mock implementation, passing
  71. // in any parameters that were provided.
  72. return cordova.required[moduleId].apply(this, arguments);
  73. };
  74. }