Matteo Scandolo | 686547a | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 19 | /** |
| 20 | * © OpenCORD |
| 21 | * |
| 22 | * Created by teone on 5/25/16. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | let cookies = { |
| 29 | xosUserPrefs: JSON.stringify({test: true}) |
| 30 | }; |
| 31 | |
| 32 | const cookieMock = { |
| 33 | get: (name) => { |
| 34 | return cookies[name] |
| 35 | }, |
| 36 | put: (name, value) => { |
| 37 | cookies[name] = value |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | describe('The xos.helper module', function(){ |
| 42 | |
| 43 | describe('The XosUserPrefs service', () => { |
| 44 | let service, deffered, rootScope; |
| 45 | |
| 46 | // load the application module |
| 47 | beforeEach(module('xos.helpers', ($provide) => { |
| 48 | $provide.value('$cookies', cookieMock); |
| 49 | })); |
| 50 | |
| 51 | // inject the cartService |
| 52 | beforeEach(inject(function (_XosUserPrefs_) { |
| 53 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 54 | service = _XosUserPrefs_; |
| 55 | spyOn(cookieMock, 'put').and.callThrough(); |
| 56 | })); |
| 57 | |
| 58 | beforeEach(inject(($q, $rootScope) => { |
| 59 | rootScope = $rootScope; |
| 60 | })); |
| 61 | it('should exists and have methods', () => { |
| 62 | expect(service).toBeDefined(); |
| 63 | expect(service.getAll).toBeDefined(); |
| 64 | expect(service.setAll).toBeDefined(); |
| 65 | expect(service.getSynchronizerNotificationStatus).toBeDefined(); |
| 66 | expect(service.setSynchronizerNotificationStatus).toBeDefined(); |
| 67 | expect(service.setUserDetailsCookie).toBeDefined(); |
| 68 | expect(service.getUserDetailsCookie).toBeDefined(); |
| 69 | }); |
| 70 | |
| 71 | describe('the getAll method', () => { |
| 72 | it('should return all the stored prefs', () => { |
| 73 | let prefs = service.getAll(); |
| 74 | expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs)); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | describe('the setAll method', () => { |
| 79 | it('should override all preferences', () => { |
| 80 | service.setAll({test: true, updated: true}); |
| 81 | expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true}); |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | describe('the synchronizers status', () => { |
| 86 | let syncNotification; |
| 87 | beforeEach(() => { |
| 88 | syncNotification = { |
| 89 | synchronizers: { |
| 90 | notification: { |
| 91 | first: true, |
| 92 | second: false |
| 93 | } |
| 94 | }, |
| 95 | userData: { |
| 96 | userId: 1 |
| 97 | } |
| 98 | } |
| 99 | cookies.xosUserPrefs = JSON.stringify(syncNotification); |
| 100 | }); |
| 101 | |
| 102 | describe('the getSynchronizerNotificationStatus method', () => { |
| 103 | it('should return notification status for all synchronizers', () => { |
| 104 | expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification); |
| 105 | }); |
| 106 | |
| 107 | it('should return notification status for a single synchronizers', () => { |
| 108 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first); |
| 109 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe('the setSynchronizerNotificationStatus', () => { |
| 114 | |
| 115 | it('should throw an error if called without synchronizer name', () => { |
| 116 | function wrapper (){ |
| 117 | service.setSynchronizerNotificationStatus(); |
| 118 | } |
| 119 | expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.'); |
| 120 | }); |
| 121 | |
| 122 | it('should update a synchronizer notification status', () => { |
| 123 | service.setSynchronizerNotificationStatus('second', true); |
| 124 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 125 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(true); |
| 126 | |
| 127 | // should persist the change |
| 128 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}'); |
| 129 | }); |
| 130 | |
| 131 | it('should handle empty cookies', () => { |
| 132 | cookies.xosUserPrefs = ''; |
| 133 | service.setSynchronizerNotificationStatus('second', true); |
| 134 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 135 | }); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | describe('the userdetails status', () => { |
| 140 | let userdata, meMock; |
| 141 | const userData = { |
| 142 | current_user_id: 2 |
| 143 | }; |
| 144 | beforeEach(inject(($q, Me) => { |
| 145 | userdata = { |
| 146 | userData: { |
| 147 | current_user_id: 1 |
| 148 | } |
| 149 | } |
| 150 | cookies.xosUserPrefs = JSON.stringify(userdata); |
| 151 | deffered= $q.defer(); |
| 152 | meMock = Me; |
| 153 | spyOn(meMock, 'get').and.callFake(function(){ |
| 154 | return deffered.promise; |
| 155 | }); |
| 156 | })); |
| 157 | describe('Get user data method', ()=>{ |
| 158 | it('it should return the stored data', (done) => { |
| 159 | service.getUserDetailsCookie().$promise.then((data) => { |
| 160 | expect(data).toEqual(userdata.userData); |
| 161 | done(); |
| 162 | }); |
| 163 | rootScope.$apply(); |
| 164 | }); |
| 165 | |
| 166 | it('Should call the Api to return the data', (done) => { |
| 167 | cookies.xosUserPrefs = JSON.stringify(); |
| 168 | service.getUserDetailsCookie().$promise.then((res) => { |
| 169 | expect(res.userId).toEqual(userData.userId); |
| 170 | expect(meMock.get).toHaveBeenCalled(); |
| 171 | done(); |
| 172 | }); |
| 173 | deffered.resolve(userData); |
| 174 | rootScope.$apply(); |
| 175 | }); |
| 176 | |
| 177 | }); |
| 178 | describe('Set user data method', ()=>{ |
| 179 | it('it should save the provided data', (done) => { |
| 180 | service.setUserDetailsCookie(userData).$promise.then(data => { |
| 181 | expect(data).toEqual(userData); |
| 182 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}'); |
| 183 | done(); |
| 184 | }); |
| 185 | rootScope.$apply(); |
| 186 | }); |
| 187 | it('Should call the Api to save the data', (done)=>{ |
| 188 | service.setUserDetailsCookie().$promise.then(data => { |
| 189 | expect(meMock.get).toHaveBeenCalled(); |
| 190 | expect(data).toEqual(userData); |
| 191 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}'); |
| 192 | done(); |
| 193 | }); |
| 194 | deffered.resolve(userData); |
| 195 | rootScope.$apply(); |
| 196 | }); |
| 197 | }); |
| 198 | }); |
| 199 | }); |
| 200 | }); |
| 201 | })(); |