Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import {IXosConfirm, XosConfirm} from './confirm.service'; |
| 22 | |
| 23 | let service: IXosConfirm; |
| 24 | let modal; |
| 25 | let modalInstance; |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 26 | let q; |
| 27 | let scope; |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 28 | |
| 29 | describe('The XosConfirm service', () => { |
| 30 | |
| 31 | beforeEach(() => { |
| 32 | angular.module('XosConfirmTest', ['ui.bootstrap.modal']) |
| 33 | .service('XosConfirm', XosConfirm); |
| 34 | angular.mock.module('XosConfirmTest'); |
| 35 | |
| 36 | angular.mock.inject(( |
| 37 | XosConfirm: IXosConfirm, |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 38 | $uibModal: any, |
| 39 | $q: ng.IQService, |
| 40 | $rootScope: ng.IScope |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 41 | ) => { |
| 42 | service = XosConfirm; |
| 43 | modal = $uibModal; |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 44 | q = $q; |
| 45 | scope = $rootScope; |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 46 | }); |
| 47 | }); |
| 48 | |
| 49 | describe('the open method', () => { |
| 50 | |
| 51 | let test1 = { |
| 52 | header: 'Test Header', |
| 53 | text: 'Test body', |
| 54 | actions: [{ |
| 55 | label: 'Action', |
| 56 | cb: () => { |
| 57 | return; |
| 58 | }, |
| 59 | class: 'btn-success' |
| 60 | }] |
| 61 | }; |
| 62 | |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 63 | it('should open the modal', () => { |
| 64 | spyOn(modal, 'open').and.returnValue('fake'); |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 65 | modalInstance = service.open(test1); |
| 66 | expect(modal.open).toHaveBeenCalled(); |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 67 | expect(modalInstance).toEqual('fake'); |
| 68 | expect(service.modalInstance).toEqual('fake'); |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 69 | }); |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 70 | |
Max Chu | f926119 | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 71 | it('should close the modal', (done) => { |
| 72 | const p = q.defer(); |
| 73 | const cb = jasmine.createSpy('cb').and.returnValue(p.promise); |
| 74 | service.modalInstance = { |
| 75 | close: jasmine.createSpy('close') |
| 76 | }; |
| 77 | |
| 78 | service.close(cb); |
| 79 | expect(cb).toHaveBeenCalled(); |
| 80 | p.resolve(); |
| 81 | scope.$apply(); |
| 82 | expect(service.modalInstance.close).toHaveBeenCalled(); |
| 83 | done(); |
| 84 | }); |
| 85 | |
| 86 | |
| 87 | |
| 88 | }); |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 89 | |
| 90 | }); |