import_tests.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* global chai */
  2. // Allow for mocking of Angular
  3. import 'angular'
  4. import 'angular-mocks'
  5. /**
  6. * Add chai assertion for comparing CodeMirror Pos objects.
  7. * A deep comparison will fail because CodeMirror inserts additional properties
  8. * that we want to ignore.
  9. */
  10. chai.Assertion.addMethod('equalPos', function(expectedPos) {
  11. const { line: actualLine, ch: actualCh } = this._obj
  12. const { line: expectedLine, ch: expectedCh } = expectedPos
  13. this.assert(
  14. actualLine === expectedLine && actualCh === expectedCh,
  15. `expected #{exp} to equal #{act}`,
  16. `expected #{exp} to not equal #{act}`,
  17. `Pos({ line: ${expectedLine}, ch: ${expectedCh} })`,
  18. `Pos({ line: ${actualLine}, ch: ${actualCh} })`
  19. )
  20. })
  21. // Mock ExposedSettings
  22. window.ExposedSettings = {}
  23. // Mock the file operation I18n names that are stored in the DOM
  24. function mockFileOperationI18nNames(id, text) {
  25. const el = document.createElement('div')
  26. el.id = id
  27. el.innerText = text
  28. el.setAttribute('hidden', true)
  29. document.body.appendChild(el)
  30. }
  31. mockFileOperationI18nNames('file_action_edited_str', 'edited')
  32. mockFileOperationI18nNames('file_action_renamed_str', 'renamed')
  33. mockFileOperationI18nNames('file_action_created_str', 'created')
  34. mockFileOperationI18nNames('file_action_deleted_str', 'deleted')
  35. /*
  36. * Bundle all test files together into a single bundle, and run tests against
  37. * this single bundle.
  38. * We are using karma-webpack to bundle our tests and the 'default' strategy is
  39. * to create a bundle for each test file. This isolates the tests better, but
  40. * causes a problem with Angular. The issue with Angular tests is because we
  41. * load a single global copy of Angular (see karma.conf.js) but
  42. * frontend/js/base.js is included in each bundle, meaning the Angular app is
  43. * initialised for each bundle when it is loaded onto the page when Karma
  44. * starts. This means that only the last bundle will have controllers/directives
  45. * registered against it, ultimately meaning that all other bundles will fail
  46. * because Angular cannot find the controller/directive under test.
  47. */
  48. // Import from the top-level any JS files within a test/frontend
  49. // directory
  50. const context = require.context('../../', true, /test\/frontend\/.*\.js$/)
  51. context.keys().forEach(context)