diff --git a/config/webpack.config.js b/config/webpack.config.js index ffb161900933d5d1b1dc616c3020c9e2b5a69a40..ae92419054e23a62c8e7e1b51af3450b52d9776e 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -15,6 +15,9 @@ var DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost'; var DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808; var DEV_SERVER_LIVERELOAD = process.env.DEV_SERVER_LIVERELOAD !== 'false'; var WEBPACK_REPORT = process.env.WEBPACK_REPORT; +var babelLoader = 'babel-loader'; + +if (!IS_PRODUCTION) babelLoader += '?plugins=rewire'; var config = { context: path.join(ROOT_PATH, 'app/assets/javascripts'), @@ -67,7 +70,7 @@ var config = { { test: /\.js$/, exclude: /(node_modules|vendor\/assets)/, - loader: 'babel-loader', + loader: babelLoader, }, { test: /\.vue$/, diff --git a/package.json b/package.json index a17399ddb8f614b897e3d2af9a2e97849ccb97b3..953ea344f37705f3f5194465dd8fc86c5e4029e1 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "babel-core": "^6.22.1", "babel-loader": "^6.2.10", + "babel-plugin-rewire": "^1.1.0", "babel-plugin-transform-define": "^1.2.0", "babel-preset-latest": "^6.24.0", "babel-preset-stage-2": "^6.22.0", diff --git a/spec/javascripts/.eslintrc b/spec/javascripts/.eslintrc index 3d922021978aac8315cbbd33dea2a7dc772a1062..a3a8bb050c95ea9b89034138697280b718a2cbbd 100644 --- a/spec/javascripts/.eslintrc +++ b/spec/javascripts/.eslintrc @@ -27,6 +27,19 @@ "jasmine/no-suite-dupes": [1, "branch"], "jasmine/no-spec-dupes": [1, "branch"], "no-console": 0, - "prefer-arrow-callback": 0 + "prefer-arrow-callback": 0, + "no-underscore-dangle": [ + 2, + { + "allow": [ + "__GetDependency__", + "__Rewire__", + "__ResetDependency__", + "__get__", + "__set__", + "__RewireAPI__" + ] + } + ] } } diff --git a/spec/javascripts/comment_type_toggle_spec.js b/spec/javascripts/comment_type_toggle_spec.js index dfd0810d52e86cd299107087bda40d6339d2d328..2e7343aeb0d7adfbfda5fd6fe3c2c7e623d67f21 100644 --- a/spec/javascripts/comment_type_toggle_spec.js +++ b/spec/javascripts/comment_type_toggle_spec.js @@ -1,5 +1,4 @@ import CommentTypeToggle from '~/comment_type_toggle'; -import * as dropLabSrc from '~/droplab/drop_lab'; import InputSetter from '~/droplab/plugins/input_setter'; describe('CommentTypeToggle', function () { @@ -58,15 +57,16 @@ describe('CommentTypeToggle', function () { this.config = {}; this.droplab = jasmine.createSpyObj('droplab', ['init']); - - spyOn(dropLabSrc, 'default').and.returnValue(this.droplab); + this.DropLab = jasmine.createSpy('DropLab').and.returnValue(this.droplab); spyOn(this.commentTypeToggle, 'setConfig').and.returnValue(this.config); + CommentTypeToggle.__Rewire__('DropLab', this.DropLab); + CommentTypeToggle.prototype.initDroplab.call(this.commentTypeToggle); }); it('should instantiate a DropLab instance', function () { - expect(dropLabSrc.default).toHaveBeenCalled(); + expect(this.DropLab).toHaveBeenCalled(); }); it('should set .droplab', function () { @@ -77,7 +77,7 @@ describe('CommentTypeToggle', function () { expect(this.commentTypeToggle.setConfig).toHaveBeenCalled(); }); - it('should call DropLab.prototype.init', function () { + it('should call droplab.init', function () { expect(this.droplab.init).toHaveBeenCalledWith( this.commentTypeToggle.dropdownTrigger, this.commentTypeToggle.dropdownList, diff --git a/spec/javascripts/droplab/hook_spec.js b/spec/javascripts/droplab/hook_spec.js index 8ebdcdd1404f5760f735693ae55e054076ef15e5..e4625d7e2196fcd8d9b51160d3f94ddb74aac850 100644 --- a/spec/javascripts/droplab/hook_spec.js +++ b/spec/javascripts/droplab/hook_spec.js @@ -1,7 +1,6 @@ /* eslint-disable */ import Hook from '~/droplab/hook'; -import * as dropdownSrc from '~/droplab/drop_down'; describe('Hook', function () { describe('class constructor', function () { @@ -12,7 +11,8 @@ describe('Hook', function () { this.config = {}; this.dropdown = {}; - spyOn(dropdownSrc, 'default').and.returnValue(this.dropdown); + this.DropDown = jasmine.createSpy('DropDown').and.returnValue(this.dropdown); + Hook.__Rewire__('DropDown', this.DropDown); this.hook = new Hook(this.trigger, this.list, this.plugins, this.config); }); @@ -26,7 +26,7 @@ describe('Hook', function () { }); it('should call DropDown constructor', function () { - expect(dropdownSrc.default).toHaveBeenCalledWith(this.list); + expect(this.DropDown).toHaveBeenCalledWith(this.list); }); it('should set .type', function () { diff --git a/yarn.lock b/yarn.lock index e16cd9c36730167a9ef8534fd0e2a393be13c11d..6f8a397aabd7876e9a8e8d138f32af1956fa6ec5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -425,6 +425,10 @@ babel-plugin-istanbul@^4.0.0: istanbul-lib-instrument "^1.4.2" test-exclude "^4.0.0" +babel-plugin-rewire@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/babel-plugin-rewire/-/babel-plugin-rewire-1.1.0.tgz#a6b966d9d8c06c03d95dcda2eec4e2521519549b" + babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"