|
| 1 | +describe('ui.grid.edit uiGridEditFileChooser', function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var $compile, $rootScope, $scope, gridUtil, uiGridConstants, uiGridEditConstants, |
| 5 | + compiledElement, fileChooser, fileChooserScope; |
| 6 | + |
| 7 | + beforeEach(function() { |
| 8 | + module('ui.grid.edit'); |
| 9 | + |
| 10 | + inject(function(_$compile_, _$rootScope_, _gridUtil_, _uiGridConstants_, _uiGridEditConstants_) { |
| 11 | + $compile = _$compile_; |
| 12 | + $rootScope = _$rootScope_; |
| 13 | + gridUtil = _gridUtil_; |
| 14 | + uiGridConstants = _uiGridConstants_; |
| 15 | + uiGridEditConstants = _uiGridEditConstants_; |
| 16 | + }); |
| 17 | + |
| 18 | + spyOn(gridUtil, 'logError').and.callFake(angular.noop); |
| 19 | + |
| 20 | + $scope = $rootScope.$new(); |
| 21 | + $scope.col = {colDef: {}}; |
| 22 | + |
| 23 | + fileChooser = angular.element('<input type="text" value="File Chooser" ui-grid-edit-file-chooser />'); |
| 24 | + compiledElement = $compile(fileChooser)($scope); |
| 25 | + $scope.$apply(); |
| 26 | + |
| 27 | + fileChooserScope = compiledElement.isolateScope(); |
| 28 | + }); |
| 29 | + afterEach(function() { |
| 30 | + gridUtil.logError.calls.reset(); |
| 31 | + }); |
| 32 | + describe('on file select', function() { |
| 33 | + beforeEach(function() { |
| 34 | + spyOn($scope, '$emit').and.callThrough(); |
| 35 | + }); |
| 36 | + afterEach(function() { |
| 37 | + $scope.$emit.calls.reset(); |
| 38 | + }); |
| 39 | + function testNegativeScenario() { |
| 40 | + it('should emit a cancel cell edit event', function() { |
| 41 | + expect($scope.$emit).toHaveBeenCalledWith(uiGridEditConstants.events.CANCEL_CELL_EDIT); |
| 42 | + }); |
| 43 | + } |
| 44 | + describe('when no target exists', function() { |
| 45 | + beforeEach(function() { |
| 46 | + var event = new Event('change', {target: null}); |
| 47 | + |
| 48 | + fileChooser[0].dispatchEvent(event); |
| 49 | + }); |
| 50 | + testNegativeScenario(); |
| 51 | + }); |
| 52 | + describe('when no target files exists', function() { |
| 53 | + beforeEach(function() { |
| 54 | + var event = new Event('change', {target: {}}); |
| 55 | + |
| 56 | + fileChooser[0].dispatchEvent(event); |
| 57 | + }); |
| 58 | + testNegativeScenario(); |
| 59 | + }); |
| 60 | + describe('when there are 0 target files', function() { |
| 61 | + beforeEach(function() { |
| 62 | + var event = new Event('change', {target: {files: []}}); |
| 63 | + |
| 64 | + fileChooser[0].dispatchEvent(event); |
| 65 | + }); |
| 66 | + testNegativeScenario(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + describe('on begin cell edit', function() { |
| 70 | + beforeEach(function() { |
| 71 | + spyOn(fileChooser[0], 'focus').and.callFake(angular.noop); |
| 72 | + spyOn(fileChooser[0], 'select').and.callFake(angular.noop); |
| 73 | + $scope.$broadcast(uiGridEditConstants.events.BEGIN_CELL_EDIT); |
| 74 | + }); |
| 75 | + afterEach(function() { |
| 76 | + fileChooser[0].focus.calls.reset(); |
| 77 | + fileChooser[0].select.calls.reset(); |
| 78 | + }); |
| 79 | + it('should focus on the file chooser', function() { |
| 80 | + expect(fileChooser[0].focus).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + it('should select the file chooser', function() { |
| 83 | + expect(fileChooser[0].select).toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + it('should emit an end cell edit event on blur', function() { |
| 86 | + var event = new Event('blur'); |
| 87 | + |
| 88 | + spyOn($scope, '$emit').and.callFake(angular.noop); |
| 89 | + fileChooser.triggerHandler(event); |
| 90 | + |
| 91 | + expect($scope.$emit).toHaveBeenCalledWith(uiGridEditConstants.events.END_CELL_EDIT); |
| 92 | + $scope.$emit.calls.reset(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + describe('on $destroy', function() { |
| 96 | + beforeEach(function() { |
| 97 | + spyOn(fileChooser[0], 'removeEventListener').and.callThrough(); |
| 98 | + $scope.$broadcast('$destroy'); |
| 99 | + }); |
| 100 | + afterEach(function() { |
| 101 | + fileChooser[0].removeEventListener.calls.reset(); |
| 102 | + }); |
| 103 | + it('should remove all event handlers', function() { |
| 104 | + expect(fileChooser[0].removeEventListener).toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments