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 | 3850d42 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 25 | import {IXosFormHelpersService} from '../../core/form/form-helpers'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 26 | |
| 27 | let service: IXosResourceService; |
| 28 | let resource: ng.resource.IResourceClass<any>; |
| 29 | let httpBackend: ng.IHttpBackendService; |
| 30 | let $resource; |
| 31 | let $scope; |
| 32 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 33 | const MockAppCfg = { |
| 34 | apiEndpoint: 'http://xos-test:3000/api', |
| 35 | websocketClient: 'http://xos-test:3000' |
| 36 | }; |
| 37 | |
Matteo Scandolo | 3850d42 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 38 | const MockFormHelpers: IXosFormHelpersService = { |
| 39 | _getFieldFormat: () => 'date' |
| 40 | }; |
| 41 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 42 | describe('The ModelRest service', () => { |
| 43 | |
| 44 | beforeEach(angular.mock.module(xosDataSources)); |
| 45 | |
| 46 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 47 | |
| 48 | angular.module(xosDataSources) |
Matteo Scandolo | 3850d42 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 49 | .constant('AppConfig', MockAppCfg) |
| 50 | .value('XosFormHelpers', MockFormHelpers); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 51 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 52 | angular.mock.module(xosDataSources); |
| 53 | }); |
| 54 | |
| 55 | |
| 56 | beforeEach(angular.mock.inject(( |
| 57 | ModelRest: IXosResourceService, |
| 58 | $httpBackend: ng.IHttpBackendService, |
| 59 | _$resource_: ng.resource.IResourceService, |
| 60 | _$rootScope_: ng.IRootScopeService |
| 61 | ) => { |
| 62 | service = ModelRest; |
| 63 | httpBackend = $httpBackend; |
| 64 | $resource = _$resource_; |
| 65 | $scope = _$rootScope_; |
| 66 | })); |
| 67 | |
| 68 | it('should return a resource based on the URL', () => { |
| 69 | resource = service.getResource('/core/test'); |
| 70 | expect(resource.constructor).toEqual($resource.constructor); |
| 71 | }); |
| 72 | |
| 73 | it('should have a query method', (done) => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 74 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 75 | .respond([ |
| 76 | {status: 'ok'} |
| 77 | ]); |
| 78 | resource = service.getResource('/core/test'); |
| 79 | resource.query().$promise |
| 80 | .then((res) => { |
| 81 | expect(res[0].status).toEqual('ok'); |
| 82 | done(); |
| 83 | }) |
| 84 | .catch(e => { |
| 85 | done(e); |
| 86 | }); |
| 87 | $scope.$apply(); |
| 88 | httpBackend.flush(); |
| 89 | }); |
| 90 | |
| 91 | it('should have a get method', (done) => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 92 | httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test/1`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 93 | .respond([ |
| 94 | {status: 'ok'} |
| 95 | ]); |
| 96 | resource = service.getResource('/core/test'); |
| 97 | resource.get({id: 1}).$promise |
| 98 | .then((res) => { |
| 99 | expect(res[0].status).toEqual('ok'); |
| 100 | done(); |
| 101 | }) |
| 102 | .catch(e => { |
| 103 | done(e); |
| 104 | }); |
| 105 | $scope.$apply(); |
| 106 | httpBackend.flush(); |
| 107 | }); |
Matteo Scandolo | 3850d42 | 2017-11-02 12:45:37 +0100 | [diff] [blame] | 108 | |
| 109 | describe('when saving a model', () => { |
| 110 | |
| 111 | let item, date; |
| 112 | const timestamp = 1509552402000; |
| 113 | |
| 114 | beforeEach(() => { |
| 115 | httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/core/test`) |
| 116 | .respond((method, url, req) => { |
| 117 | return [200, req]; |
| 118 | }); |
| 119 | resource = service.getResource('/core/test'); |
| 120 | date = new Date(timestamp); |
| 121 | item = new resource({date: date.toString()}); |
| 122 | }); |
| 123 | |
| 124 | xit('should convert dates to timestamps', (done) => { |
| 125 | item.$save() |
| 126 | .then(res => { |
| 127 | expect(res.date).toEqual(timestamp); |
| 128 | done(); |
| 129 | }); |
| 130 | $scope.$apply(); |
| 131 | httpBackend.flush(); |
| 132 | done(); |
| 133 | }); |
| 134 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 135 | }); |