blob: 464184b5eda2f67ff69017c922fc411eaace0927 [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();
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070051 });
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 Agarwal711b1ec2016-06-27 13:28:54 -070076 },
77 userData: {
78 userId:1
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070079 }
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 Agarwal711b1ec2016-06-27 13:28:54 -0700110 expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}');
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700111 });
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 Agarwale3041d02016-07-07 16:55:45 -0700120
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700121 describe('the userdetails status', () => {
Arpit Agarwale3041d02016-07-07 16:55:45 -0700122 let userdata, meMock;
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700123 const userData = {
Arpit Agarwale3041d02016-07-07 16:55:45 -0700124 current_user_id: 2
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700125 };
126 beforeEach(inject(($q,Me) => {
Arpit Agarwale3041d02016-07-07 16:55:45 -0700127 userdata = {
128 userData: {
129 current_user_id:1
130 }
131 }
132 cookies.xosUserPrefs = JSON.stringify(userdata);
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700133 deffered= $q.defer();
Arpit Agarwale3041d02016-07-07 16:55:45 -0700134 meMock = Me;
135 spyOn(meMock, 'get').and.callFake(function(){
136 return deffered.promise;
Arpit Agarwal711b1ec2016-06-27 13:28:54 -0700137 });
138 }));
Arpit Agarwale3041d02016-07-07 16:55:45 -0700139 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 Agarwal711b1ec2016-06-27 13:28:54 -0700150 service.getUserDetailsCookie().$promise.then((res) => {
151 expect(res.userId).toEqual(userData.userId);
Arpit Agarwale3041d02016-07-07 16:55:45 -0700152 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 Agarwal711b1ec2016-06-27 13:28:54 -0700174 done();
175 });
176 deffered.resolve(userData);
177 rootScope.$apply();
178 });
179 });
180 });
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700181 });
182 });
183})();