Skip to content

Commit

Permalink
add unit test for file input component
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemcdermott committed Nov 29, 2017
1 parent cc8b5bc commit 5baa371
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion awx/ui/client/lib/components/input/file.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function AtInputFileController (baseInputController, eventService) {
baseInputController.call(vm, 'input', _scope_, element, form);

scope = _scope_;
[input] = element.find('input');
input = element.find('input')[0]; // eslint-disable-line prefer-destructuring

vm.listeners = vm.setFileListeners(input);

Expand Down
4 changes: 2 additions & 2 deletions awx/ui/client/lib/components/modal/modal.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function AtModalController (eventService, strings) {
vm.strings = strings;

vm.init = (scope, el) => {
[overlay] = el;
[modal] = el.find('.at-Modal-window');
overlay = el[0]; // eslint-disable-line prefer-destructuring
modal = el.find('.at-Modal-window')[0]; // eslint-disable-line prefer-destructuring

vm.modal = scope[scope.ns].modal;
vm.modal.show = vm.show;
Expand Down
61 changes: 61 additions & 0 deletions awx/ui/test/unit/components/file.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe('Components | Input | File', () => {
let $scope;
let element;
let state;
let controller;

const getMockFileEvent = file => ({ target: { files: [file] } });

beforeEach(() => {
angular.mock.module('at.lib.services');
angular.mock.module('at.lib.components');
});

describe('AtInputFileController', () => {
beforeEach(angular.mock.inject(($rootScope, $compile) => {
const component = '<at-input-file id="unit" state="vm.form.unit"></at-input-file>';
const dom = angular.element(`<at-form state="vm.form">${component}</at-form>`);

$scope = $rootScope.$new();
$scope.vm = { form: { disabled: false, unit: {} } };

$compile(dom)($scope);
$scope.$digest();

element = dom.find('#unit');
state = $scope.vm.form.unit;
controller = element.controller('atInputFile');
}));

it('should initialize without a value by default', () => {
expect(state._value).not.toBeDefined();
expect(state._displayValue).not.toBeDefined();
});

it('should update display value with file name when file is read', () => {
const name = 'notavirus.exe';
const reader = { result: 'AAAAAAA' };

controller.check = jasmine.createSpy('check');

controller.readFile(reader, getMockFileEvent({ name }));

$scope.$digest();

expect(state._value).toBeDefined();
expect(state._displayValue).toEqual(name);

expect(controller.check).toHaveBeenCalled();
});

it('should notify handler on file input change event', () => {
controller.handleFileChangeEvent = jasmine.createSpy('handleFileChangeEvent');

element.find('input')[0].dispatchEvent(new Event('change'));

$scope.$digest();

expect(controller.handleFileChangeEvent).toHaveBeenCalled();
});
});
});
1 change: 1 addition & 0 deletions awx/ui/test/unit/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'angular-mocks';

// Import tests
import './file.unit';
import './layout.unit';
import './side-nav.unit';
import './side-nav-item.unit';
Expand Down

0 comments on commit 5baa371

Please sign in to comment.