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'); |
| 46 | }); |
| 47 | |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 48 | describe('when updating a collection', () => { |
| 49 | |
| 50 | beforeEach(() => { |
| 51 | subject = new BehaviorSubject([ |
| 52 | new resource({id: 1, name: 'test'}) |
| 53 | ]); |
| 54 | }); |
| 55 | |
| 56 | it('should remove a model if it has been deleted', () => { |
| 57 | const event: IWSEvent = { |
| 58 | model: 'Test', |
| 59 | msg: { |
| 60 | object: { |
| 61 | id: 1, |
| 62 | name: 'test' |
| 63 | }, |
| 64 | changed_fields: ['deleted'] |
| 65 | } |
| 66 | }; |
| 67 | service.updateCollection(event, subject); |
| 68 | expect(subject.value.length).toBe(0); |
| 69 | }); |
| 70 | |
| 71 | it('should update a model if it has been updated', () => { |
| 72 | const event: IWSEvent = { |
| 73 | model: 'Test', |
| 74 | msg: { |
| 75 | object: { |
| 76 | id: 1, |
| 77 | name: 'test-updated' |
| 78 | }, |
| 79 | changed_fields: ['name'] |
| 80 | } |
| 81 | }; |
| 82 | service.updateCollection(event, subject); |
| 83 | expect(subject.value.length).toBe(1); |
| 84 | expect(subject.value[0].name).toBe('test-updated'); |
| 85 | }); |
| 86 | |
| 87 | it('should add a model if it has been created', () => { |
| 88 | const event: IWSEvent = { |
| 89 | model: 'Test', |
| 90 | msg: { |
| 91 | object: { |
| 92 | id: 2, |
| 93 | name: 'another-test' |
| 94 | }, |
| 95 | changed_fields: ['created'] |
| 96 | } |
| 97 | }; |
| 98 | service.updateCollection(event, subject); |
| 99 | expect(subject.value.length).toBe(2); |
| 100 | expect(subject.value[0].name).toBe('test'); |
| 101 | expect(subject.value[1].name).toBe('another-test'); |
| 102 | }); |
| 103 | |
| 104 | describe('when adding a model', () => { |
| 105 | |
| 106 | beforeEach(() => { |
| 107 | const event: IWSEvent = { |
| 108 | model: 'Test', |
| 109 | msg: { |
| 110 | object: { |
| 111 | id: 2, |
| 112 | name: 'another-test' |
| 113 | }, |
| 114 | changed_fields: ['created'] |
| 115 | } |
| 116 | }; |
| 117 | service.updateCollection(event, subject); |
| 118 | }); |
| 119 | |
| 120 | it('should create a resource', () => { |
| 121 | expect(subject.value[1].$save).toBeDefined(); |
| 122 | expect(subject.value[1].$delete).toBeDefined(); |
| 123 | }); |
| 124 | |
| 125 | xit('should automatically create the appropriate resource', () => { |
| 126 | // TODO test that the url of the resource is the correct one, |
| 127 | // use httpbackend and mock a call?? any faster way?? |
| 128 | }); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | }); |