javascript.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ===============
  2. JavaScript code
  3. ===============
  4. While most of Django core is Python, the ``admin`` and ``gis`` contrib apps
  5. contain JavaScript code.
  6. Please follow these coding standards when writing JavaScript code for inclusion
  7. in Django.
  8. Code style
  9. ==========
  10. * Please conform to the indentation style dictated in the ``.editorconfig``
  11. file. We recommend using a text editor with `EditorConfig`_ support to avoid
  12. indentation and whitespace issues. Most of the JavaScript files use 4 spaces
  13. for indentation, but there are some exceptions.
  14. * When naming variables, use ``camelCase`` instead of ``underscore_case``.
  15. Different JavaScript files sometimes use a different code style. Please try to
  16. conform to the code style of each file.
  17. * Use the `ESLint`_ code linter to check your code for bugs and style errors.
  18. ESLint will be run when you run the JavaScript tests. We also recommended
  19. installing a ESLint plugin in your text editor.
  20. * Where possible, write code that will work even if the page structure is later
  21. changed with JavaScript. For instance, when binding a click handler, use
  22. ``$('body').on('click', selector, func)`` instead of
  23. ``$(selector).click(func)``. This makes it easier for projects to extend
  24. Django's default behavior with JavaScript.
  25. .. _javascript-patches:
  26. JavaScript patches
  27. ==================
  28. Django's admin system leverages the jQuery framework to increase the
  29. capabilities of the admin interface. In conjunction, there is an emphasis on
  30. admin JavaScript performance and minimizing overall admin media file size.
  31. .. _javascript-tests:
  32. JavaScript tests
  33. ================
  34. Django's JavaScript tests can be run in a browser or from the command line.
  35. The tests are located in a top level :source:`js_tests` directory.
  36. Writing tests
  37. -------------
  38. Django's JavaScript tests use `QUnit`_. Here is an example test module:
  39. .. code-block:: javascript
  40. QUnit.module('magicTricks', {
  41. beforeEach: function() {
  42. const $ = django.jQuery;
  43. $('#qunit-fixture').append('<button class="button"></button>');
  44. }
  45. });
  46. QUnit.test('removeOnClick removes button on click', function(assert) {
  47. const $ = django.jQuery;
  48. removeOnClick('.button');
  49. assert.equal($('.button').length, 1);
  50. $('.button').click();
  51. assert.equal($('.button').length, 0);
  52. });
  53. QUnit.test('copyOnClick adds button on click', function(assert) {
  54. const $ = django.jQuery;
  55. copyOnClick('.button');
  56. assert.equal($('.button').length, 1);
  57. $('.button').click();
  58. assert.equal($('.button').length, 2);
  59. });
  60. Please consult the ``QUnit`` documentation for information on the types of
  61. `assertions supported by QUnit <https://api.qunitjs.com/assert/>`_.
  62. Running tests
  63. -------------
  64. The JavaScript tests may be run from a web browser or from the command line.
  65. Testing from a web browser
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. To run the tests from a web browser, open up :source:`js_tests/tests.html` in your
  68. browser.
  69. To measure code coverage when running the tests, you need to view that file
  70. over HTTP. To view code coverage:
  71. * Execute ``python -m http.server`` from the root directory (not from inside
  72. ``js_tests``).
  73. * Open http://localhost:8000/js_tests/tests.html in your web browser.
  74. Testing from the command line
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. To run the tests from the command line, you need to have `Node.js`_ installed.
  77. After installing ``Node.js``, install the JavaScript test dependencies by
  78. running the following from the root of your Django checkout:
  79. .. console::
  80. $ npm install
  81. Then run the tests with:
  82. .. console::
  83. $ npm test
  84. .. _EditorConfig: https://editorconfig.org/
  85. .. _Java: https://www.java.com
  86. .. _eslint: https://eslint.org/
  87. .. _node.js: https://nodejs.org/
  88. .. _qunit: https://qunitjs.com/