blob: 7a00cc76c9d2f14246118b252b8bd3a8f36865a9 [file] [log] [blame]
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -07001/**
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 Agarwal711b1ec2016-06-27 13:28:54 -070026 let service, toscaBase, deffered, SiteSpy, rootScope;
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070027
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 Agarwal711b1ec2016-06-27 13:28:54 -070040 beforeEach(inject(($q, $rootScope) => {
41 rootScope = $rootScope;
42 }));
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070043 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 Agarwal711b1ec2016-06-27 13:28:54 -070049 expect(service.setUserDetailsCookie).toBeDefined();
50 expect(service.getUserDetailsCookie).toBeDefined();
51 expect(service.setDataUser).toBeDefined()
52
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070053 });
54
55 describe('the getAll method', () => {
56 it('should return all the stored prefs', () => {
57 let prefs = service.getAll();
58 expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs));
59 });
60 });
61
62 describe('the setAll method', () => {
63 it('should override all preferences', () => {
64 service.setAll({test: true, updated: true});
65 expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true});
66 });
67 });
68
69 describe('the synchronizers status', () => {
70 let syncNotification;
71 beforeEach(() => {
72 syncNotification = {
73 synchronizers: {
74 notification: {
75 first: true,
76 second: false
77 }
Arpit Agarwal711b1ec2016-06-27 13:28:54 -070078 },
79 userData: {
80 userId:1
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070081 }
82 }
83 cookies.xosUserPrefs = JSON.stringify(syncNotification);
84 });
85
86 describe('the getSynchronizerNotificationStatus method', () => {
87 it('should return notification status for all synchronizers', () => {
88 expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification);
89 });
90
91 it('should return notification status for a single synchronizers', () => {
92 expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first);
93 expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second);
94 });
95 });
96
Arpit Agarwal711b1ec2016-06-27 13:28:54 -070097 describe('the getUserDetailsCookie method', () => {
98 it('should return current user id', (done) => {
99 service.getUserDetailsCookie().$promise.then((res) => {
100 expect(res.userId).toEqual(syncNotification.userData.userId);
101 done();
102 });
103 rootScope.$apply();
104 });
105 });
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700106 describe('the setSynchronizerNotificationStatus', () => {
107
108 it('should throw an error if called without synchronizer name', () => {
109 function wrapper (){
110 service.setSynchronizerNotificationStatus();
111 }
112 expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.');
113 });
114
115 it('should update a synchronizer notification status', () => {
116 service.setSynchronizerNotificationStatus('second', true);
117 expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
118 expect(service.getSynchronizerNotificationStatus('first')).toEqual(true);
119
120 // should persist the change
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700121 expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}');
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700122 });
123
124 it('should handle empty cookies', () => {
125 cookies.xosUserPrefs = '';
126 service.setSynchronizerNotificationStatus('second', true);
127 expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
128 });
129 });
130 });
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700131 describe('the userdetails status', () => {
132 let syncNotification;
133 beforeEach(() => {
134 syncNotification = {
135 synchronizers: {
136 notification: {
137 first: true,
138 second: false
139 }
140 }
141 }
142 cookies.xosUserPrefs = JSON.stringify(syncNotification);
143 });
144 const userData = {
145 userId: 1
146 };
147 beforeEach(inject(($q,Me) => {
148 deffered= $q.defer();
149 spyOn(Me, 'get').and.callFake(function(){
150 return {$promise: deffered.promise};
151 });
152 }));
153 describe('the getUserDetailsCookie method', () => {
154 it('should return current user id', (done) => {
155 service.getUserDetailsCookie().$promise.then((res) => {
156 expect(res.userId).toEqual(userData.userId);
157 done();
158 });
159 deffered.resolve(userData);
160 rootScope.$apply();
161 });
162 });
163 });
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700164 });
165 });
166})();