Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-ui-router'; |
| 4 | import {StoreHelpers, IStoreHelpersService} from './store.helpers'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 5 | import {ModelRest} from '../rest/model.rest'; |
| 6 | import {BehaviorSubject} from 'rxjs'; |
| 7 | import {IWSEvent} from '../websocket/global'; |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 8 | import {ConfigHelpers} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 9 | import {AuthService} from '../rest/auth.rest'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 10 | |
| 11 | let service: IStoreHelpersService; |
| 12 | let subject: BehaviorSubject<any>; |
| 13 | let resource: ng.resource.IResourceClass<any>; |
| 14 | let $resource: ng.resource.IResourceService; |
| 15 | |
| 16 | describe('The StoreHelpers service', () => { |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | angular |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 20 | .module('test', ['ngResource', 'toastr', 'ngCookies']) |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 21 | .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 22 | .service('ModelRest', ModelRest) // NOTE evaluate mock |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 23 | .service('StoreHelpers', StoreHelpers) |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 24 | .service('AuthService', AuthService) |
| 25 | .value('AppConfig', {}); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 26 | |
| 27 | angular.mock.module('test'); |
| 28 | }); |
| 29 | |
| 30 | beforeEach(angular.mock.inject(( |
| 31 | StoreHelpers: IStoreHelpersService, |
| 32 | _$resource_: ng.resource.IResourceService |
| 33 | ) => { |
| 34 | $resource = _$resource_; |
| 35 | resource = $resource('/test'); |
| 36 | service = StoreHelpers; |
| 37 | })); |
| 38 | |
| 39 | it('should have an update collection method', () => { |
| 40 | expect(service.updateCollection).toBeDefined(); |
| 41 | }); |
| 42 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 43 | |
| 44 | it('should convert a core model name in an URL', () => { |
| 45 | expect(service.urlFromCoreModel('Slice')).toBe('/core/slices'); |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 46 | expect(service.urlFromCoreModel('Xos')).toBe('/core/xoses'); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 47 | |
| 48 | // handling exceptions |
| 49 | expect(service.urlFromCoreModel('SiteRole')).toBe('/core/site_roles'); |
| 50 | expect(service.urlFromCoreModel('SliceRole')).toBe('/core/slice_roles'); |
| 51 | expect(service.urlFromCoreModel('SlicePrivilege')).toBe('/core/slice_privileges'); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 52 | }); |
| 53 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 54 | describe('when updating a collection', () => { |
| 55 | |
| 56 | beforeEach(() => { |
| 57 | subject = new BehaviorSubject([ |
| 58 | new resource({id: 1, name: 'test'}) |
| 59 | ]); |
| 60 | }); |
| 61 | |
| 62 | it('should remove a model if it has been deleted', () => { |
| 63 | const event: IWSEvent = { |
| 64 | model: 'Test', |
| 65 | msg: { |
| 66 | object: { |
| 67 | id: 1, |
| 68 | name: 'test' |
| 69 | }, |
| 70 | changed_fields: ['deleted'] |
| 71 | } |
| 72 | }; |
| 73 | service.updateCollection(event, subject); |
| 74 | expect(subject.value.length).toBe(0); |
| 75 | }); |
| 76 | |
| 77 | it('should update a model if it has been updated', () => { |
| 78 | const event: IWSEvent = { |
| 79 | model: 'Test', |
| 80 | msg: { |
| 81 | object: { |
| 82 | id: 1, |
| 83 | name: 'test-updated' |
| 84 | }, |
| 85 | changed_fields: ['name'] |
| 86 | } |
| 87 | }; |
| 88 | service.updateCollection(event, subject); |
| 89 | expect(subject.value.length).toBe(1); |
| 90 | expect(subject.value[0].name).toBe('test-updated'); |
| 91 | }); |
| 92 | |
| 93 | it('should add a model if it has been created', () => { |
| 94 | const event: IWSEvent = { |
| 95 | model: 'Test', |
| 96 | msg: { |
| 97 | object: { |
| 98 | id: 2, |
| 99 | name: 'another-test' |
| 100 | }, |
| 101 | changed_fields: ['created'] |
| 102 | } |
| 103 | }; |
| 104 | service.updateCollection(event, subject); |
| 105 | expect(subject.value.length).toBe(2); |
| 106 | expect(subject.value[0].name).toBe('test'); |
| 107 | expect(subject.value[1].name).toBe('another-test'); |
| 108 | }); |
| 109 | |
| 110 | describe('when adding a model', () => { |
| 111 | |
| 112 | beforeEach(() => { |
| 113 | const event: IWSEvent = { |
| 114 | model: 'Test', |
| 115 | msg: { |
| 116 | object: { |
| 117 | id: 2, |
| 118 | name: 'another-test' |
| 119 | }, |
| 120 | changed_fields: ['created'] |
| 121 | } |
| 122 | }; |
| 123 | service.updateCollection(event, subject); |
| 124 | }); |
| 125 | |
| 126 | it('should create a resource', () => { |
| 127 | expect(subject.value[1].$save).toBeDefined(); |
| 128 | expect(subject.value[1].$delete).toBeDefined(); |
| 129 | }); |
| 130 | |
| 131 | xit('should automatically create the appropriate resource', () => { |
| 132 | // TODO test that the url of the resource is the correct one, |
| 133 | // use httpbackend and mock a call?? any faster way?? |
| 134 | }); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | }); |