salix/front/core/components/dialog/index.spec.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-10-05 12:28:57 +00:00
describe('Component vnDialog', () => {
let $element;
let $scope;
let controller;
2017-10-05 12:28:57 +00:00
beforeEach(ngModule('vnCore'));
2017-10-05 12:28:57 +00:00
2020-07-23 14:46:16 +00:00
beforeEach(inject(($rootScope, $compile) => {
$scope = $rootScope.$new();
$element = $compile('<vn-dialog><tpl-body>Body</tpl-body></vn-dialog>')($scope);
controller = $element.controller('vnDialog');
controller.emit = jasmine.createSpy('emit');
2017-10-05 12:28:57 +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);
2020-02-26 12:22:52 +00:00
jest.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);
controller.hide();
2019-11-10 10:08:44 +00:00
$scope.$apply();
2019-11-10 10:08:44 +00:00
expect(resolved).toBeTruthy();
});
});
2019-10-30 15:57:14 +00:00
describe('respond()', () => {
it(`should do nothing if dialog is already hidden`, () => {
controller.onResponse = () => {};
2020-02-26 12:22:52 +00:00
jest.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 = () => {};
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'onResponse');
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
it(`should call onResponse() with the response`, () => {
2019-10-30 15:57:14 +00:00
controller.onResponse = () => {};
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'onResponse');
2018-08-30 06:16:50 +00:00
controller.show();
controller.respond('response');
expect(controller.onResponse).toHaveBeenCalledWith({$response: 'response'});
});
it(`should call onAccept() when accept response is given`, () => {
controller.onAccept = () => {};
2020-02-26 12:22:52 +00:00
jest.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`, () => {
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-05 12:28:57 +00:00
});