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