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 | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 9 | |
| 10 | let service: IStoreHelpersService; |
| 11 | let subject: BehaviorSubject<any>; |
| 12 | let resource: ng.resource.IResourceClass<any>; |
| 13 | let $resource: ng.resource.IResourceService; |
| 14 | |
| 15 | describe('The StoreHelpers service', () => { |
| 16 | |
| 17 | beforeEach(() => { |
| 18 | angular |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 19 | .module('test', ['ngResource', 'toastr']) |
| 20 | .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 21 | .service('ModelRest', ModelRest) // NOTE evaluate mock |
| 22 | .service('StoreHelpers', StoreHelpers); |
| 23 | |
| 24 | angular.mock.module('test'); |
| 25 | }); |
| 26 | |
| 27 | beforeEach(angular.mock.inject(( |
| 28 | StoreHelpers: IStoreHelpersService, |
| 29 | _$resource_: ng.resource.IResourceService |
| 30 | ) => { |
| 31 | $resource = _$resource_; |
| 32 | resource = $resource('/test'); |
| 33 | service = StoreHelpers; |
| 34 | })); |
| 35 | |
| 36 | it('should have an update collection method', () => { |
| 37 | expect(service.updateCollection).toBeDefined(); |
| 38 | }); |
| 39 | |
| 40 | describe('when updating a collection', () => { |
| 41 | |
| 42 | beforeEach(() => { |
| 43 | subject = new BehaviorSubject([ |
| 44 | new resource({id: 1, name: 'test'}) |
| 45 | ]); |
| 46 | }); |
| 47 | |
| 48 | it('should remove a model if it has been deleted', () => { |
| 49 | const event: IWSEvent = { |
| 50 | model: 'Test', |
| 51 | msg: { |
| 52 | object: { |
| 53 | id: 1, |
| 54 | name: 'test' |
| 55 | }, |
| 56 | changed_fields: ['deleted'] |
| 57 | } |
| 58 | }; |
| 59 | service.updateCollection(event, subject); |
| 60 | expect(subject.value.length).toBe(0); |
| 61 | }); |
| 62 | |
| 63 | it('should update a model if it has been updated', () => { |
| 64 | const event: IWSEvent = { |
| 65 | model: 'Test', |
| 66 | msg: { |
| 67 | object: { |
| 68 | id: 1, |
| 69 | name: 'test-updated' |
| 70 | }, |
| 71 | changed_fields: ['name'] |
| 72 | } |
| 73 | }; |
| 74 | service.updateCollection(event, subject); |
| 75 | expect(subject.value.length).toBe(1); |
| 76 | expect(subject.value[0].name).toBe('test-updated'); |
| 77 | }); |
| 78 | |
| 79 | it('should add a model if it has been created', () => { |
| 80 | const event: IWSEvent = { |
| 81 | model: 'Test', |
| 82 | msg: { |
| 83 | object: { |
| 84 | id: 2, |
| 85 | name: 'another-test' |
| 86 | }, |
| 87 | changed_fields: ['created'] |
| 88 | } |
| 89 | }; |
| 90 | service.updateCollection(event, subject); |
| 91 | expect(subject.value.length).toBe(2); |
| 92 | expect(subject.value[0].name).toBe('test'); |
| 93 | expect(subject.value[1].name).toBe('another-test'); |
| 94 | }); |
| 95 | |
| 96 | describe('when adding a model', () => { |
| 97 | |
| 98 | beforeEach(() => { |
| 99 | const event: IWSEvent = { |
| 100 | model: 'Test', |
| 101 | msg: { |
| 102 | object: { |
| 103 | id: 2, |
| 104 | name: 'another-test' |
| 105 | }, |
| 106 | changed_fields: ['created'] |
| 107 | } |
| 108 | }; |
| 109 | service.updateCollection(event, subject); |
| 110 | }); |
| 111 | |
| 112 | it('should create a resource', () => { |
| 113 | expect(subject.value[1].$save).toBeDefined(); |
| 114 | expect(subject.value[1].$delete).toBeDefined(); |
| 115 | }); |
| 116 | |
| 117 | xit('should automatically create the appropriate resource', () => { |
| 118 | // TODO test that the url of the resource is the correct one, |
| 119 | // use httpbackend and mock a call?? any faster way?? |
| 120 | }); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | }); |