Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 5/25/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | let service; |
| 10 | let cookies = { |
| 11 | xosUserPrefs: JSON.stringify({test: true}) |
| 12 | }; |
| 13 | |
| 14 | const cookieMock = { |
| 15 | get: (name) => { |
| 16 | return cookies[name] |
| 17 | }, |
| 18 | put: (name, value) => { |
| 19 | cookies[name] = value |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | describe('The xos.helper module', function(){ |
| 24 | |
| 25 | describe('The XosUserPrefs service', () => { |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 26 | let service, toscaBase, deffered, SiteSpy, rootScope; |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 27 | |
| 28 | // load the application module |
| 29 | beforeEach(module('xos.helpers', ($provide) => { |
| 30 | $provide.value('$cookies', cookieMock); |
| 31 | })); |
| 32 | |
| 33 | // inject the cartService |
| 34 | beforeEach(inject(function (_XosUserPrefs_) { |
| 35 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 36 | service = _XosUserPrefs_; |
| 37 | spyOn(cookieMock, 'put').and.callThrough(); |
| 38 | })); |
| 39 | |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 40 | beforeEach(inject(($q, $rootScope) => { |
| 41 | rootScope = $rootScope; |
| 42 | })); |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 43 | it('should exists and have methods', () => { |
| 44 | expect(service).toBeDefined(); |
| 45 | expect(service.getAll).toBeDefined(); |
| 46 | expect(service.setAll).toBeDefined(); |
| 47 | expect(service.getSynchronizerNotificationStatus).toBeDefined(); |
| 48 | expect(service.setSynchronizerNotificationStatus).toBeDefined(); |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 49 | expect(service.setUserDetailsCookie).toBeDefined(); |
| 50 | expect(service.getUserDetailsCookie).toBeDefined(); |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | describe('the getAll method', () => { |
| 54 | it('should return all the stored prefs', () => { |
| 55 | let prefs = service.getAll(); |
| 56 | expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs)); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | describe('the setAll method', () => { |
| 61 | it('should override all preferences', () => { |
| 62 | service.setAll({test: true, updated: true}); |
| 63 | expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true}); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe('the synchronizers status', () => { |
| 68 | let syncNotification; |
| 69 | beforeEach(() => { |
| 70 | syncNotification = { |
| 71 | synchronizers: { |
| 72 | notification: { |
| 73 | first: true, |
| 74 | second: false |
| 75 | } |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 76 | }, |
| 77 | userData: { |
| 78 | userId:1 |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | cookies.xosUserPrefs = JSON.stringify(syncNotification); |
| 82 | }); |
| 83 | |
| 84 | describe('the getSynchronizerNotificationStatus method', () => { |
| 85 | it('should return notification status for all synchronizers', () => { |
| 86 | expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification); |
| 87 | }); |
| 88 | |
| 89 | it('should return notification status for a single synchronizers', () => { |
| 90 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first); |
| 91 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe('the setSynchronizerNotificationStatus', () => { |
| 96 | |
| 97 | it('should throw an error if called without synchronizer name', () => { |
| 98 | function wrapper (){ |
| 99 | service.setSynchronizerNotificationStatus(); |
| 100 | } |
| 101 | expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.'); |
| 102 | }); |
| 103 | |
| 104 | it('should update a synchronizer notification status', () => { |
| 105 | service.setSynchronizerNotificationStatus('second', true); |
| 106 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 107 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(true); |
| 108 | |
| 109 | // should persist the change |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 110 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}'); |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 111 | }); |
| 112 | |
| 113 | it('should handle empty cookies', () => { |
| 114 | cookies.xosUserPrefs = ''; |
| 115 | service.setSynchronizerNotificationStatus('second', true); |
| 116 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 117 | }); |
| 118 | }); |
| 119 | }); |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 120 | |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 121 | describe('the userdetails status', () => { |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 122 | let userdata, meMock; |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 123 | const userData = { |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 124 | current_user_id: 2 |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 125 | }; |
| 126 | beforeEach(inject(($q,Me) => { |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 127 | userdata = { |
| 128 | userData: { |
| 129 | current_user_id:1 |
| 130 | } |
| 131 | } |
| 132 | cookies.xosUserPrefs = JSON.stringify(userdata); |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 133 | deffered= $q.defer(); |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 134 | meMock = Me; |
| 135 | spyOn(meMock, 'get').and.callFake(function(){ |
| 136 | return deffered.promise; |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 137 | }); |
| 138 | })); |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 139 | describe('Get user data method' , ()=>{ |
| 140 | it('it should return the stored data', (done) => { |
| 141 | service.getUserDetailsCookie().$promise.then((data) => { |
| 142 | expect(data).toEqual(userdata.userData); |
| 143 | done(); |
| 144 | }); |
| 145 | rootScope.$apply(); |
| 146 | }); |
| 147 | |
| 148 | it('Should call the Api to return the data', (done) => { |
| 149 | cookies.xosUserPrefs = JSON.stringify(); |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 150 | service.getUserDetailsCookie().$promise.then((res) => { |
| 151 | expect(res.userId).toEqual(userData.userId); |
Arpit Agarwal | e3041d0 | 2016-07-07 16:55:45 -0700 | [diff] [blame] | 152 | expect(meMock.get).toHaveBeenCalled(); |
| 153 | done(); |
| 154 | }); |
| 155 | deffered.resolve(userData); |
| 156 | rootScope.$apply(); |
| 157 | }); |
| 158 | |
| 159 | }); |
| 160 | describe('Set user data method' , ()=>{ |
| 161 | it('it should save the provided data' , (done) => { |
| 162 | service.setUserDetailsCookie(userData).$promise.then(data => { |
| 163 | expect(data).toEqual(userData); |
| 164 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}'); |
| 165 | done(); |
| 166 | }); |
| 167 | rootScope.$apply(); |
| 168 | }); |
| 169 | it('Should call the Api to save the data',(done)=>{ |
| 170 | service.setUserDetailsCookie().$promise.then(data => { |
| 171 | expect(meMock.get).toHaveBeenCalled(); |
| 172 | expect(data).toEqual(userData); |
| 173 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}'); |
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 174 | done(); |
| 175 | }); |
| 176 | deffered.resolve(userData); |
| 177 | rootScope.$apply(); |
| 178 | }); |
| 179 | }); |
| 180 | }); |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 181 | }); |
| 182 | }); |
| 183 | })(); |