Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -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 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-resource'; |
| 22 | import 'angular-cookies'; |
| 23 | import {IXosResourceService} from './model.rest'; |
| 24 | import {xosDataSources} from '../index'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 25 | |
| 26 | let service: IXosResourceService; |
| 27 | let resource: ng.resource.IResourceClass<any>; |
| 28 | let httpBackend: ng.IHttpBackendService; |
| 29 | let $resource; |
| 30 | let $scope; |
| 31 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 32 | const MockAppCfg = { |
| 33 | apiEndpoint: 'http://xos-test:3000/api', |
| 34 | websocketClient: 'http://xos-test:3000' |
| 35 | }; |
| 36 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 37 | describe('The ModelRest service', () => { |
| 38 | |
| 39 | beforeEach(angular.mock.module(xosDataSources)); |
| 40 | |
| 41 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 42 | |
| 43 | angular.module(xosDataSources) |
| 44 | .constant('AppConfig', MockAppCfg); |
| 45 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 46 | angular.mock.module(xosDataSources); |
| 47 | }); |
| 48 | |
| 49 | |
| 50 | beforeEach(angular.mock.inject(( |
| 51 | ModelRest: IXosResourceService, |
| 52 | $httpBackend: ng.IHttpBackendService, |
| 53 | _$resource_: ng.resource.IResourceService, |
| 54 | _$rootScope_: ng.IRootScopeService |
| 55 | ) => { |
| 56 | service = ModelRest; |
| 57 | httpBackend = $httpBackend; |
| 58 | $resource = _$resource_; |
| 59 | $scope = _$rootScope_; |
| 60 | })); |
| 61 | |
| 62 | it('should return a resource based on the URL', () => { |
| 63 | resource = service.getResource('/core/test'); |
| 64 | expect(resource.constructor).toEqual($resource.constructor); |
| 65 | }); |
| 66 | |
| 67 | it('should have a query method', (done) => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 68 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 69 | .respond([ |
| 70 | {status: 'ok'} |
| 71 | ]); |
| 72 | resource = service.getResource('/core/test'); |
| 73 | resource.query().$promise |
| 74 | .then((res) => { |
| 75 | expect(res[0].status).toEqual('ok'); |
| 76 | done(); |
| 77 | }) |
| 78 | .catch(e => { |
| 79 | done(e); |
| 80 | }); |
| 81 | $scope.$apply(); |
| 82 | httpBackend.flush(); |
| 83 | }); |
| 84 | |
| 85 | it('should have a get method', (done) => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 86 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test/1`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 87 | .respond([ |
| 88 | {status: 'ok'} |
| 89 | ]); |
| 90 | resource = service.getResource('/core/test'); |
| 91 | resource.get({id: 1}).$promise |
| 92 | .then((res) => { |
| 93 | expect(res[0].status).toEqual('ok'); |
| 94 | done(); |
| 95 | }) |
| 96 | .catch(e => { |
| 97 | done(e); |
| 98 | }); |
| 99 | $scope.$apply(); |
| 100 | httpBackend.flush(); |
| 101 | }); |
| 102 | }); |