|
| 1 | +describe('ui.grid.importer uiGridImporterMenuItem', function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var $compile, $templateCache, $rootScope, $scope, gridUtil, uiGridImporterService, |
| 5 | + fileChooser, uiGridImporterMenuItem; |
| 6 | + |
| 7 | + function compileImporterMenuItem(elem) { |
| 8 | + uiGridImporterMenuItem = angular.element(elem); |
| 9 | + $compile(uiGridImporterMenuItem)($scope); |
| 10 | + $scope.$apply(); |
| 11 | + |
| 12 | + fileChooser = uiGridImporterMenuItem[0].querySelectorAll('.ui-grid-importer-file-chooser'); |
| 13 | + } |
| 14 | + |
| 15 | + beforeEach(function() { |
| 16 | + module('ui.grid'); |
| 17 | + module('ui.grid.importer'); |
| 18 | + |
| 19 | + inject(function(_$compile_, _$rootScope_, _$templateCache_, _gridUtil_, _uiGridImporterService_) { |
| 20 | + $compile = _$compile_; |
| 21 | + $rootScope = _$rootScope_; |
| 22 | + $templateCache = _$templateCache_; |
| 23 | + gridUtil = _gridUtil_; |
| 24 | + uiGridImporterService = _uiGridImporterService_; |
| 25 | + }); |
| 26 | + |
| 27 | + spyOn(gridUtil, 'logError').and.callFake(angular.noop); |
| 28 | + spyOn(uiGridImporterService, 'importThisFile').and.callFake(angular.noop); |
| 29 | + |
| 30 | + $scope = $rootScope.$new(); |
| 31 | + |
| 32 | + compileImporterMenuItem('<div ui-grid-importer-menu-item></div>'); |
| 33 | + }); |
| 34 | + afterEach(function() { |
| 35 | + gridUtil.logError.calls.reset(); |
| 36 | + uiGridImporterService.importThisFile.calls.reset(); |
| 37 | + }); |
| 38 | + it('should compile the menu item', function() { |
| 39 | + expect(uiGridImporterMenuItem.hasClass('ui-grid-menu-item')).toBe(true); |
| 40 | + }); |
| 41 | + it('should do nothing when a change event is fired and there are no files selected', function() { |
| 42 | + var event = new Event('change'); |
| 43 | + |
| 44 | + fileChooser[0].dispatchEvent(event); |
| 45 | + |
| 46 | + expect(gridUtil.logError).not.toHaveBeenCalled(); |
| 47 | + expect(uiGridImporterService.importThisFile).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + it('should log an error if more than one file choosers are present on the menu item', function() { |
| 50 | + $templateCache.put('ui-grid/importerMenuItem', '<div class="ui-grid-menu-item"><span class="ui-grid-importer-file-chooser"></span>' + |
| 51 | + '<span class="ui-grid-importer-file-chooser"></span></div>'); |
| 52 | + compileImporterMenuItem('<div ui-grid-importer-menu-item></div>'); |
| 53 | + |
| 54 | + expect(gridUtil.logError).toHaveBeenCalledWith('Found > 1 or < 1 file choosers within the menu item, error, cannot continue'); |
| 55 | + }); |
| 56 | + describe('on $destroy', function() { |
| 57 | + beforeEach(function() { |
| 58 | + spyOn(fileChooser[0], 'removeEventListener').and.callThrough(); |
| 59 | + $scope.$broadcast('$destroy'); |
| 60 | + }); |
| 61 | + afterEach(function() { |
| 62 | + fileChooser[0].removeEventListener.calls.reset(); |
| 63 | + }); |
| 64 | + it('should remove all event handlers', function() { |
| 65 | + expect(fileChooser[0].removeEventListener).toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments