arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-x-socialsharing/tests/test.js
plugins/cordova-plugin-x-socialsharing/tests/test.jsRaw
  1/**
  2 * Jasmine Based test suites
  3 *
  4 * Several of SocialSharing APIs cannot be automatically tested, because
  5 * they depend on user interaction in order to call success or fail
  6 * handlers. For most of them, there is a basic test that assert the presence of
  7 * the API.
  8 *
  9 * There are some cases that test automation can be applied, i.e in "canShareVia",
 10 * "canShareViaEmail" and "available" methods. For those cases, there is some level
 11 * of automatic test coverage.
 12 */
 13exports.defineAutoTests = function () {
 14  'use strict';
 15
 16  describe('socialsharing', function () {
 17    it('should be defined', function () {
 18      expect(window.plugins.socialsharing).toBeDefined();
 19    });
 20
 21    describe('share', function () {
 22      it('should be defined', function () {
 23        expect(window.plugins.socialsharing.share).toBeDefined();
 24      });
 25
 26      it('should be a function', function () {
 27        expect(typeof window.plugins.socialsharing.share).toEqual('function');
 28      });
 29    });
 30
 31    describe('shareVia', function () {
 32      it('should be defined', function () {
 33        expect(window.plugins.socialsharing.shareVia).toBeDefined();
 34      });
 35
 36      it('should be a function', function () {
 37        expect(typeof window.plugins.socialsharing.shareVia).toEqual('function');
 38      });
 39    });
 40
 41    describe('shareViaTwitter', function () {
 42      it('should be defined', function () {
 43        expect(window.plugins.socialsharing.shareViaTwitter).toBeDefined();
 44      });
 45
 46      it('should be a function', function () {
 47        expect(typeof window.plugins.socialsharing.shareViaTwitter).toEqual('function');
 48      });
 49    });
 50
 51    describe('shareViaFacebook', function () {
 52      it('should be defined', function () {
 53        expect(window.plugins.socialsharing.shareViaFacebook).toBeDefined();
 54      });
 55
 56      it('should be a function', function () {
 57        expect(typeof window.plugins.socialsharing.shareViaFacebook).toEqual('function');
 58      });
 59    });
 60
 61    describe('shareViaFacebookWithPasteMessageHint', function () {
 62      it('should be defined', function () {
 63        expect(window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toBeDefined();
 64      });
 65
 66      it('should be a function', function () {
 67        expect(typeof window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toEqual('function');
 68      });
 69    });
 70
 71    describe('shareViaWhatsApp', function () {
 72      it('should be defined', function () {
 73        expect(window.plugins.socialsharing.shareViaWhatsApp).toBeDefined();
 74      });
 75
 76      it('should be a function', function () {
 77        expect(typeof window.plugins.socialsharing.shareViaWhatsApp).toEqual('function');
 78      });
 79    });
 80
 81    describe('shareViaSMS', function () {
 82      it('should be defined', function () {
 83        expect(window.plugins.socialsharing.shareViaSMS).toBeDefined();
 84      });
 85
 86      it('should be a function', function () {
 87        expect(typeof window.plugins.socialsharing.shareViaSMS).toEqual('function');
 88      });
 89    });
 90
 91    describe('shareViaEmail', function () {
 92      it('should be defined', function () {
 93        expect(window.plugins.socialsharing.shareViaEmail).toBeDefined();
 94      });
 95
 96      it('should be a function', function () {
 97        expect(typeof window.plugins.socialsharing.shareViaEmail).toEqual('function');
 98      });
 99    });
100
101    describe('canShareVia', function () {
102      it('should be defined', function () {
103        expect(window.plugins.socialsharing.canShareVia).toBeDefined();
104      });
105
106      it('should be a function', function () {
107        expect(typeof window.plugins.socialsharing.canShareVia).toEqual('function');
108      });
109
110      it('should always call callback or error function', function (done) {
111        function onSuccess(data){
112          expect(data).not.toEqual(null);
113          done();
114        }
115
116        function onError(error){
117          expect(error).not.toEqual(null);
118          done();
119        }
120
121        window.plugins.socialsharing.canShareVia('dummytarget','dummymessage', null, null, null, onSuccess, onError);
122      });
123    });
124
125    describe('canshareViaEmail', function(){
126      it('should be defined', function () {
127        expect(window.plugins.socialsharing.canShareViaEmail).toBeDefined();
128      });
129
130      it('should be a function', function () {
131        expect(typeof window.plugins.socialsharing.canShareViaEmail).toEqual('function');
132      });
133
134      it('should always call callback or error function', function (done) {
135        function onSuccess(data){
136          expect(data).not.toEqual(null);
137          done();
138        }
139
140        function onError(error){
141          expect(error).not.toEqual(null);
142          done();
143        }
144
145        window.plugins.socialsharing.canShareViaEmail(onSuccess, onError);
146      });
147    });
148
149    describe('availabe', function(){
150      it('should be defined', function () {
151        expect(window.plugins.socialsharing.available).toBeDefined();
152      });
153
154      it('should be a function', function () {
155        expect(typeof window.plugins.socialsharing.available).toEqual('function');
156      });
157
158      it('should return a boolean when called', function(done){
159        window.plugins.socialsharing.available(function(isAvailable) {
160          expect(typeof isAvailable).toEqual('boolean');
161          done();
162        });
163      });
164    });
165  });
166};
167
168/**
169 * Manual tests suites
170 *
171 * Some actions buttons to execute SocialSharing plugin methods
172 */
173exports.defineManualTests = function (contentEl, createActionButton) {
174  'use strict';
175
176  /** helper function to log messages in the log div element */
177  function logMessage(message, color) {
178    var log = document.getElementById('info'),
179        logLine = document.createElement('div');
180
181    if (color) {
182      logLine.style.color = color;
183    }
184
185    logLine.innerHTML = message;
186    log.appendChild(logLine);
187  }
188
189  /** helper function to clear the log div element */
190  function clearLog() {
191    var log = document.getElementById('info');
192    log.innerHTML = '';
193  }
194
195  /** helper function to declare a not implemented test */
196  function testNotImplemented(testName) {
197    return function () {
198      console.error(testName, 'test not implemented');
199    };
200  }
201
202  /** init method called on deviceready event */
203  function init() {}
204
205  /** object to hold properties and configs */
206  var TestSuite = {};
207
208  TestSuite.getCanShareViaTarget = function(){
209    return document.getElementById('inputCanShareVia').value;
210  };
211
212  TestSuite.$markup = '' +
213  '<fieldset>' +
214  '<legend>Available Tests</legend>' +
215
216  '<h3>Available</h3>' +
217  '<div id="buttonIsAvailable"></div>' +
218  'Expected result: Should log if the plugin is available or not' +
219  '</fieldset>' +
220
221  '<fieldset>' +
222  '<legend>Share Tests</legend>' +
223
224  '<h3>Share Message</h3>' +
225  '<div id="buttonShareMessage"></div>' +
226  'Expected result: Should display share widget, and the message to share should contain "Message body"' +
227
228  '<h3>Share Message with Subject</h3>' +
229  '<div id="buttonShareMessageWithSubject"></div>' +
230  'Expected result: Should display share widget, and the message to share should contain "Message body", and the subject should be "Message subject"' +
231
232  '<h3>Share Link</h3>' +
233  '<div id="buttonShareLink"></div>' +
234  'Expected result: Should display share widget, and the message to share should contain "http://www.x-services.nl"' +
235
236  '<h3>Share Message with Link</h3>' +
237  '<div id="buttonShareMessageAndLink"></div>' +
238  'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl"' +
239
240  '<h3>Share Image</h3>' +
241  '<div id="buttonShareImage"></div>' +
242  'Expected result: Should display share widget, and the message to share should contain an image' +
243
244  '<h3>Share Image in base 64</h3>' +
245  '<div id="buttonShareImageBase64"></div>' +
246  'Expected result: Should display share widget, and the message to share should contain an image. The image is encoded in base 64' +
247
248  '<h3>Share Image with Message</h3>' +
249  '<div id="buttonShareMessageImage"></div>' +
250  'Expected result: Should display share widget, and the message to share should contain "Message body" and an image' +
251
252  '<h3>Share Image with Message and Link</h3>' +
253  '<div id="buttonShareMessageImageLink"></div>' +
254  'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl" and an image' +
255
256  '<h3>Share Image with Message, Subject and Link</h3>' +
257  '<div id="buttonShareMessageSubjectImageLink"></div>' +
258  'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl", "Message subject" as subject, and an image' +
259  '</fieldset>' +
260
261  '<fieldset>' +
262  '<legend>Can Share Tests</legend>' +
263
264  'Target: <input id="inputCanShareVia" type="text"/><br>' +
265
266  '<h3>Can Share via</h3>' +
267  '<div id="buttonCanShareVia"></div>' +
268  'Expected result: should log OK if can share, or should log a list of available share targets' +
269
270  '<h3>Can Share via Email</h3>' +
271  '<div id="buttonCanShareViaEmail"></div>' +
272  'Expected result: should log OK if can share' +
273  '</fieldset>' +
274  '';
275
276  contentEl.innerHTML = '<div id="info"></div>' + TestSuite.$markup;
277
278  createActionButton('availabe', function () {
279    clearLog();
280    window.plugins.socialsharing.available(function(isAvailable) {
281      var message = 'is this plugin available? ';
282      message += isAvailable? 'Yes' : 'No';
283
284      logMessage(message, isAvailable? 'green' : 'red');
285    });
286  }, 'buttonIsAvailable');
287
288  createActionButton('share message', function () {
289    window.plugins.socialsharing.share('Message body');
290  }, 'buttonShareMessage');
291
292  createActionButton('share message and subject', function () {
293    window.plugins.socialsharing.share('Message body', 'Message subject');
294  }, 'buttonShareMessageWithSubject');
295
296  createActionButton('share link', function () {
297    window.plugins.socialsharing.share(null, null, null, 'http://www.x-services.nl');
298  }, 'buttonShareLink');
299
300  createActionButton('share message and link', function () {
301    window.plugins.socialsharing.share('Message body', null, null, 'http://www.x-services.nl');
302  }, 'buttonShareMessageAndLink');
303
304  createActionButton('share image', function () {
305    window.plugins.socialsharing.share(null, null, 'https://www.google.nl/images/srpr/logo4w.png', null);
306  }, 'buttonShareImage');
307
308  createActionButton('share image base 64', function () {
309    window.plugins.socialsharing.share(null, 'Android filename', 'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7', null);
310  }, 'buttonShareImageBase64');
311
312  createActionButton('share message with image', function () {
313    window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', null);
314  }, 'buttonShareMessageImage');
315
316  createActionButton('share message, image, and link', function () {
317    window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
318  }, 'buttonShareMessageImageLink');
319
320  createActionButton('share message, subject, image, and link', function () {
321    window.plugins.socialsharing.share('Message body', 'Message subject', 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
322  }, 'buttonShareMessageSubjectImageLink');
323
324  createActionButton('can share via', function () {
325    var target = TestSuite.getCanShareViaTarget();
326
327    if(!target){
328      console.error('must have a canShareVia target');
329    }
330    else {
331      clearLog();
332      window.plugins.socialsharing.canShareVia(target, 'msg', null, null, null, function(e){
333        console.log('canShareVia success, see log for more information');
334        logMessage('canShareVia: ' + e,'green');
335      }, function(e){
336        console.error('canShareVia fail, see log for more information');
337        var message = "Share targets<br>";
338
339        message += "<ul>";
340
341        e.forEach(function(target){
342          message += "<li>" + target + "</li>";
343        });
344
345        message += "</ul>";
346        logMessage(message,'red');
347      });
348    }
349  }, 'buttonCanShareVia');
350
351  createActionButton('can share via email', function () {
352    clearLog();
353    window.plugins.socialsharing.canShareViaEmail(function(e){
354      console.log('canShareViaEmail success, see log for more information');
355      logMessage('canShareViaEmail: ' + e,'green');
356    }, function(e){
357      console.error('canShareViaEmail fail, see log for more information');
358      logMessage('canShareViaEmail: ' + e,'red');
359    });
360  }, 'buttonCanShareViaEmail');
361
362  document.addEventListener('deviceready', init, false);
363};