index-ios.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import inAppPurchase from '../www/index-ios';
  2. import assert from 'assert';
  3. describe('iOS purchases', () => {
  4. before(() => {
  5. GLOBAL.window = {};
  6. GLOBAL.window.cordova = {};
  7. });
  8. describe('#getProducts()', () => {
  9. it('should call the iOS requestProducts() function with the correct args', async (done) => {
  10. try {
  11. const productIds = ['com.test.prod1', 'com.test.prod2'];
  12. let requestProductsCalled = false;
  13. GLOBAL.window.cordova.exec = (success, err, pluginName, name, args) => {
  14. assert(typeof success === 'function', 'should define a success callback');
  15. assert(typeof err === 'function', 'should define an error callback');
  16. assert(pluginName === 'PaymentsPlugin', 'invalid iOS plugin name');
  17. assert(args[0] === productIds, 'should get productIds as args');
  18. requestProductsCalled = true;
  19. success([]);
  20. };
  21. await inAppPurchase.getProducts(productIds);
  22. assert(requestProductsCalled, 'requestProducts() should be called');
  23. done();
  24. } catch (err) {
  25. done(err);
  26. }
  27. });
  28. it('should return an array of objects', async (done) => {
  29. try {
  30. const products = [
  31. { productId: 'com.test.prod1', title: 'prod1 title', description: 'prod1 description', price: '$0.99', currency: 'USD', priceAsDecimal: 0.99 },
  32. { productId: 'com.test.prod2', title: 'prod2 title', description: 'prod2 description', price: '$1.99', currency: 'USD', priceAsDecimal: 1.99 }
  33. ];
  34. const productIds = products.map(i => i.productId );
  35. GLOBAL.window.cordova.exec = (success) => {
  36. success({ products });
  37. };
  38. const resProducts = await inAppPurchase.getProducts(productIds);
  39. assert.deepEqual(resProducts, products);
  40. done();
  41. } catch (err) {
  42. done(err);
  43. }
  44. });
  45. it('should throw an error on an invalid argument', async (done) => {
  46. let err1 = false;
  47. let err2 = false;
  48. let err3 = false;
  49. try {
  50. await inAppPurchase.getProducts();
  51. } catch (err) {
  52. if (err.message === inAppPurchase.utils.errors[101]) {
  53. err1 = true;
  54. } else {
  55. done(err);
  56. }
  57. }
  58. try {
  59. await inAppPurchase.getProducts([1]);
  60. } catch (err) {
  61. if (err.message === inAppPurchase.utils.errors[101]) {
  62. err2 = true;
  63. } else {
  64. done(err);
  65. }
  66. }
  67. try {
  68. await inAppPurchase.getProducts('test');
  69. } catch (err) {
  70. if (err.message === inAppPurchase.utils.errors[101]) {
  71. err3 = true;
  72. } else {
  73. done(err);
  74. }
  75. }
  76. assert(err1, 'should throw an error for no arguments');
  77. assert(err2, 'should throw an error for an array of int');
  78. assert(err3, 'should throw an error for a string');
  79. done();
  80. });
  81. });
  82. describe('#buy()', () => {
  83. it('should call the iOS buy() function with the correct args ', async (done) => {
  84. try {
  85. const productId = 'com.test.prod1';
  86. const transactionId = '111111111';
  87. const receipt = '222222222';
  88. GLOBAL.window.cordova.exec = (success, err, pluginName, name, args) => {
  89. assert(typeof success === 'function', 'should define a success callback');
  90. assert(typeof err === 'function', 'should define an error callback');
  91. assert(pluginName === 'PaymentsPlugin', 'invalid iOS plugin name');
  92. assert(name === 'buy', 'invalid function name');
  93. assert(args[0] === productId, 'should get productId as args');
  94. success({ productId, transactionId, receipt });
  95. };
  96. await inAppPurchase.buy(productId);
  97. done();
  98. } catch (err) {
  99. done(err);
  100. }
  101. });
  102. it('should throw an error on an invalid argument', async (done) => {
  103. let err1 = false;
  104. let err2 = false;
  105. let err3 = false;
  106. try {
  107. await inAppPurchase.buy();
  108. } catch (err) {
  109. if (err.message === inAppPurchase.utils.errors[102]) {
  110. err1 = true;
  111. } else {
  112. done(err);
  113. }
  114. }
  115. try {
  116. await inAppPurchase.buy([1]);
  117. } catch (err) {
  118. if (err.message === inAppPurchase.utils.errors[102]) {
  119. err2 = true;
  120. } else {
  121. done(err);
  122. }
  123. }
  124. try {
  125. await inAppPurchase.buy(1);
  126. } catch (err) {
  127. if (err.message === inAppPurchase.utils.errors[102]) {
  128. err3 = true;
  129. } else {
  130. done(err);
  131. }
  132. }
  133. assert(err1, 'should throw an error for no arguments');
  134. assert(err2, 'should throw an error for an array of int');
  135. assert(err3, 'should throw an error for an int');
  136. done();
  137. });
  138. it('should return an object with the correct attributes', async (done) => {
  139. try {
  140. const productId = 'com.test.prod1';
  141. const transactionId = '111111111';
  142. const receipt = '222222222';
  143. GLOBAL.window.cordova.exec = (success) => {
  144. success({ transactionId, receipt });
  145. };
  146. const res = await inAppPurchase.buy(productId);
  147. assert(res.transactionId === transactionId);
  148. assert(res.receipt === receipt);
  149. done();
  150. } catch (err) {
  151. done(err);
  152. }
  153. });
  154. });
  155. describe('#subscribe()', () => {
  156. it('should call the iOS buy() function with the correct args ', async (done) => {
  157. try {
  158. const productId = 'com.test.prod1';
  159. const transactionId = '111111111';
  160. const receipt = '222222222';
  161. GLOBAL.window.cordova.exec = (success, err, pluginName, name, args) => {
  162. assert(typeof success === 'function', 'should define a success callback');
  163. assert(typeof err === 'function', 'should define an error callback');
  164. assert(pluginName === 'PaymentsPlugin', 'invalid iOS plugin name');
  165. assert(name === 'buy', 'invalid function name');
  166. assert(args[0] === productId, 'should get productId as args');
  167. success({ productId, transactionId, receipt });
  168. };
  169. await inAppPurchase.subscribe(productId);
  170. done();
  171. } catch (err) {
  172. done(err);
  173. }
  174. });
  175. });
  176. describe('#consume()', () => {
  177. it('should always successfully resolve without doing anything', async (done) => {
  178. try {
  179. await inAppPurchase.consume();
  180. done();
  181. } catch (err) {
  182. done(err);
  183. }
  184. });
  185. });
  186. describe('#restorePurchases()', () => {
  187. it('should call the iOS restoreTransactions() function with the correct args ', async (done) => {
  188. try {
  189. GLOBAL.window.cordova.exec = (success, err, pluginName, name) => {
  190. assert(typeof success === 'function', 'should define a success callback');
  191. assert(typeof err === 'function', 'should define an error callback');
  192. assert(pluginName === 'PaymentsPlugin', 'invalid iOS plugin name');
  193. assert(name === 'restorePurchases', 'invalid function name');
  194. success([{}]);
  195. };
  196. await inAppPurchase.restorePurchases();
  197. done();
  198. } catch (err) {
  199. done(err);
  200. }
  201. });
  202. it('should return an array of objects with the correct attributes', async (done) => {
  203. const transactionId = '111111111';
  204. const productId = '111111111';
  205. const transactionState = 'REFUNDED';
  206. const date = new Date();
  207. try {
  208. GLOBAL.window.cordova.exec = (success) => {
  209. success({ transactions : [{ transactionId, productId, transactionState, date }]});
  210. };
  211. const res = await inAppPurchase.restorePurchases();
  212. assert(res[0].transactionId === transactionId);
  213. assert(res[0].productId === productId);
  214. assert(res[0].state === transactionState);
  215. assert(res[0].date === date);
  216. done();
  217. } catch (err) {
  218. done(err);
  219. }
  220. });
  221. });
  222. describe('#getReceipt()', () => {
  223. it('should call the iOS getReceipt() function', async (done) => {
  224. try {
  225. GLOBAL.window.cordova.exec = (success, err, pluginName, name) => {
  226. assert(typeof success === 'function', 'should define a success callback');
  227. assert(typeof err === 'function', 'should define an error callback');
  228. assert(pluginName === 'PaymentsPlugin', 'invalid iOS plugin name');
  229. assert(name === 'getReceipt', 'invalid function name');
  230. success('');
  231. };
  232. await inAppPurchase.getReceipt();
  233. done();
  234. } catch (err) {
  235. done(err);
  236. }
  237. });
  238. });
  239. });