Matteo Scandolo | f0958b1 | 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', () => { |
| 26 | |
| 27 | // load the application module |
| 28 | beforeEach(module('xos.helpers', ($provide) => { |
| 29 | $provide.value('$cookies', cookieMock); |
| 30 | })); |
| 31 | |
| 32 | // inject the cartService |
| 33 | beforeEach(inject(function (_XosUserPrefs_) { |
| 34 | // The injector unwraps the underscores (_) from around the parameter names when matching |
| 35 | service = _XosUserPrefs_; |
| 36 | spyOn(cookieMock, 'put').and.callThrough(); |
| 37 | })); |
| 38 | |
| 39 | it('should exists and have methods', () => { |
| 40 | expect(service).toBeDefined(); |
| 41 | expect(service.getAll).toBeDefined(); |
| 42 | expect(service.setAll).toBeDefined(); |
| 43 | expect(service.getSynchronizerNotificationStatus).toBeDefined(); |
| 44 | expect(service.setSynchronizerNotificationStatus).toBeDefined(); |
| 45 | }); |
| 46 | |
| 47 | describe('the getAll method', () => { |
| 48 | it('should return all the stored prefs', () => { |
| 49 | let prefs = service.getAll(); |
| 50 | expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs)); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | describe('the setAll method', () => { |
| 55 | it('should override all preferences', () => { |
| 56 | service.setAll({test: true, updated: true}); |
| 57 | expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true}); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe('the synchronizers status', () => { |
| 62 | let syncNotification; |
| 63 | beforeEach(() => { |
| 64 | syncNotification = { |
| 65 | synchronizers: { |
| 66 | notification: { |
| 67 | first: true, |
| 68 | second: false |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | cookies.xosUserPrefs = JSON.stringify(syncNotification); |
| 73 | }); |
| 74 | |
| 75 | describe('the getSynchronizerNotificationStatus method', () => { |
| 76 | it('should return notification status for all synchronizers', () => { |
| 77 | expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification); |
| 78 | }); |
| 79 | |
| 80 | it('should return notification status for a single synchronizers', () => { |
| 81 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first); |
| 82 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | describe('the setSynchronizerNotificationStatus', () => { |
| 87 | |
| 88 | it('should throw an error if called without synchronizer name', () => { |
| 89 | function wrapper (){ |
| 90 | service.setSynchronizerNotificationStatus(); |
| 91 | } |
| 92 | expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.'); |
| 93 | }); |
| 94 | |
| 95 | it('should update a synchronizer notification status', () => { |
| 96 | service.setSynchronizerNotificationStatus('second', true); |
| 97 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 98 | expect(service.getSynchronizerNotificationStatus('first')).toEqual(true); |
| 99 | |
| 100 | // should persist the change |
| 101 | expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}}}'); |
| 102 | }); |
| 103 | |
| 104 | it('should handle empty cookies', () => { |
| 105 | cookies.xosUserPrefs = ''; |
| 106 | service.setSynchronizerNotificationStatus('second', true); |
| 107 | expect(service.getSynchronizerNotificationStatus('second')).toEqual(true); |
| 108 | }); |
| 109 | }); |
| 110 | }); |
| 111 | }); |
| 112 | }); |
| 113 | })(); |