blob: 63b55c97314e887b8aac460c41a60de1b771f3b3 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 5/25/16.
5 */
6
7(function () {
8 'use strict';
9
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 let service, deffered, rootScope;
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
40 beforeEach(inject(($q, $rootScope) => {
41 rootScope = $rootScope;
42 }));
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();
49 expect(service.setUserDetailsCookie).toBeDefined();
50 expect(service.getUserDetailsCookie).toBeDefined();
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 }
76 },
77 userData: {
78 userId: 1
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
110 expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}');
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 });
120
121 describe('the userdetails status', () => {
122 let userdata, meMock;
123 const userData = {
124 current_user_id: 2
125 };
126 beforeEach(inject(($q, Me) => {
127 userdata = {
128 userData: {
129 current_user_id: 1
130 }
131 }
132 cookies.xosUserPrefs = JSON.stringify(userdata);
133 deffered= $q.defer();
134 meMock = Me;
135 spyOn(meMock, 'get').and.callFake(function(){
136 return deffered.promise;
137 });
138 }));
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();
150 service.getUserDetailsCookie().$promise.then((res) => {
151 expect(res.userId).toEqual(userData.userId);
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}}');
174 done();
175 });
176 deffered.resolve(userData);
177 rootScope.$apply();
178 });
179 });
180 });
181 });
182 });
183})();