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