2017-10-05 12:28:57 +00:00
|
|
|
describe('Component vnDialog', () => {
|
|
|
|
let $element;
|
2019-10-31 14:24:28 +00:00
|
|
|
let $scope;
|
2017-10-17 12:24:40 +00:00
|
|
|
let controller;
|
2017-10-05 12:28:57 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('vnCore'));
|
2017-10-05 12:28:57 +00:00
|
|
|
|
2019-10-28 16:31:33 +00:00
|
|
|
beforeEach(angular.mock.inject(($rootScope, $compile) => {
|
2019-10-31 14:24:28 +00:00
|
|
|
$scope = $rootScope.$new();
|
|
|
|
$element = $compile('<vn-dialog><tpl-body>Body</tpl-body></vn-dialog>')($scope);
|
2019-10-28 16:31:33 +00:00
|
|
|
controller = $element.controller('vnDialog');
|
2019-06-26 11:35:38 +00:00
|
|
|
controller.emit = jasmine.createSpy('emit');
|
2017-10-05 12:28:57 +00:00
|
|
|
}));
|
|
|
|
|
2019-10-31 14:24:28 +00:00
|
|
|
afterEach(() => {
|
|
|
|
$scope.$destroy();
|
|
|
|
$element.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('show()', () => {
|
|
|
|
it(`should call the show handler when response is given`, () => {
|
|
|
|
let called;
|
|
|
|
let showHandler = () => called = true;
|
|
|
|
|
|
|
|
controller.show(showHandler);
|
|
|
|
controller.respond();
|
|
|
|
|
|
|
|
expect(called).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should not hide the dialog when false is returned from response handler`, () => {
|
|
|
|
controller.show(() => false);
|
|
|
|
spyOn(controller, 'hide');
|
|
|
|
controller.respond('answer');
|
|
|
|
|
|
|
|
expect(controller.hide).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hide()', () => {
|
2019-11-10 10:08:44 +00:00
|
|
|
it(`should resolve the promise returned by show`, () => {
|
|
|
|
let resolved = true;
|
|
|
|
controller.show().then(() => resolved = true);
|
2019-10-31 14:24:28 +00:00
|
|
|
controller.hide();
|
2019-11-10 10:08:44 +00:00
|
|
|
$scope.$apply();
|
2019-10-31 14:24:28 +00:00
|
|
|
|
2019-11-10 10:08:44 +00:00
|
|
|
expect(resolved).toBeTruthy();
|
2019-10-31 14:24:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-30 15:57:14 +00:00
|
|
|
describe('respond()', () => {
|
2019-10-31 14:24:28 +00:00
|
|
|
it(`should do nothing if dialog is already hidden`, () => {
|
|
|
|
controller.onResponse = () => {};
|
|
|
|
spyOn(controller, 'onResponse');
|
|
|
|
controller.respond();
|
|
|
|
|
|
|
|
expect(controller.onResponse).not.toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should call onResponse() if it's defined`, () => {
|
2019-10-30 15:57:14 +00:00
|
|
|
controller.onResponse = () => {};
|
|
|
|
spyOn(controller, 'onResponse');
|
2019-10-31 14:24:28 +00:00
|
|
|
|
|
|
|
controller.show();
|
2019-10-30 15:57:14 +00:00
|
|
|
controller.respond();
|
2018-08-30 06:16:50 +00:00
|
|
|
|
2019-10-30 15:57:14 +00:00
|
|
|
expect(controller.onResponse).toHaveBeenCalledWith(jasmine.any(Object));
|
|
|
|
});
|
2017-10-05 12:28:57 +00:00
|
|
|
|
2019-10-31 14:24:28 +00:00
|
|
|
it(`should call onResponse() with the response`, () => {
|
2019-10-30 15:57:14 +00:00
|
|
|
controller.onResponse = () => {};
|
|
|
|
spyOn(controller, 'onResponse');
|
2018-08-30 06:16:50 +00:00
|
|
|
|
2019-10-31 14:24:28 +00:00
|
|
|
controller.show();
|
|
|
|
controller.respond('response');
|
|
|
|
|
|
|
|
expect(controller.onResponse).toHaveBeenCalledWith({$response: 'response'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should call onAccept() when accept response is given`, () => {
|
|
|
|
controller.onAccept = () => {};
|
|
|
|
spyOn(controller, 'onAccept');
|
|
|
|
|
|
|
|
controller.show();
|
|
|
|
controller.respond('accept');
|
|
|
|
|
|
|
|
expect(controller.onAccept).toHaveBeenCalledWith({$response: 'accept'});
|
|
|
|
});
|
|
|
|
|
2019-11-10 10:08:44 +00:00
|
|
|
it(`should resolve the promise returned by show() with response`, () => {
|
2019-10-31 14:24:28 +00:00
|
|
|
let response;
|
|
|
|
controller.show().then(res => response = res);
|
|
|
|
controller.respond('response');
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
|
|
expect(response).toEqual('response');
|
2017-10-05 12:28:57 +00:00
|
|
|
});
|
2017-10-09 12:30:23 +00:00
|
|
|
});
|
2017-10-05 12:28:57 +00:00
|
|
|
});
|