SubscriptionHelperTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionHelper'
  5. const plans = {
  6. expensive: {
  7. planCode: 'expensive',
  8. price_in_cents: 1500,
  9. },
  10. cheaper: {
  11. planCode: 'cheaper',
  12. price_in_cents: 500,
  13. },
  14. alsoCheap: {
  15. plancode: 'also-cheap',
  16. price_in_cents: 500,
  17. },
  18. expensiveGroup: {
  19. plancode: 'group_expensive',
  20. price_in_cents: 49500,
  21. groupPlan: true,
  22. },
  23. cheapGroup: {
  24. plancode: 'group_cheap',
  25. price_in_cents: 1000,
  26. groupPlan: true,
  27. },
  28. bad: {},
  29. }
  30. describe('SubscriptionHelper', function () {
  31. beforeEach(function () {
  32. this.INITIAL_LICENSE_SIZE = 2
  33. this.settings = {
  34. groupPlanModalOptions: {},
  35. }
  36. this.GroupPlansData = {
  37. enterprise: {
  38. collaborator: {
  39. CHF: {
  40. [this.INITIAL_LICENSE_SIZE]: {
  41. price_in_cents: 1000,
  42. },
  43. },
  44. DKK: {
  45. [this.INITIAL_LICENSE_SIZE]: {
  46. price_in_cents: 2000,
  47. },
  48. },
  49. SEK: {
  50. [this.INITIAL_LICENSE_SIZE]: {
  51. price_in_cents: 3000,
  52. },
  53. },
  54. NOK: {
  55. [this.INITIAL_LICENSE_SIZE]: {
  56. price_in_cents: 4000,
  57. },
  58. },
  59. USD: {
  60. [this.INITIAL_LICENSE_SIZE]: {
  61. price_in_cents: 5000,
  62. },
  63. },
  64. },
  65. professional: {
  66. CHF: {
  67. [this.INITIAL_LICENSE_SIZE]: {
  68. price_in_cents: 10000,
  69. },
  70. },
  71. DKK: {
  72. [this.INITIAL_LICENSE_SIZE]: {
  73. price_in_cents: 20000,
  74. },
  75. },
  76. SEK: {
  77. [this.INITIAL_LICENSE_SIZE]: {
  78. price_in_cents: 30000,
  79. },
  80. },
  81. NOK: {
  82. [this.INITIAL_LICENSE_SIZE]: {
  83. price_in_cents: 40000,
  84. },
  85. },
  86. USD: {
  87. [this.INITIAL_LICENSE_SIZE]: {
  88. price_in_cents: 50000,
  89. },
  90. },
  91. },
  92. },
  93. }
  94. this.SubscriptionHelper = SandboxedModule.require(modulePath, {
  95. requires: {
  96. '@overleaf/settings': this.settings,
  97. './GroupPlansData': this.GroupPlansData,
  98. },
  99. })
  100. })
  101. describe('shouldPlanChangeAtTermEnd', function () {
  102. it('should return true if the new plan is less expensive', function () {
  103. const changeAtTermEnd = this.SubscriptionHelper.shouldPlanChangeAtTermEnd(
  104. plans.expensive,
  105. plans.cheaper
  106. )
  107. expect(changeAtTermEnd).to.be.true
  108. })
  109. it('should return false if the new plan is more exepensive', function () {
  110. const changeAtTermEnd = this.SubscriptionHelper.shouldPlanChangeAtTermEnd(
  111. plans.cheaper,
  112. plans.expensive
  113. )
  114. expect(changeAtTermEnd).to.be.false
  115. })
  116. it('should return false if the new plan is the same price', function () {
  117. const changeAtTermEnd = this.SubscriptionHelper.shouldPlanChangeAtTermEnd(
  118. plans.cheaper,
  119. plans.alsoCheap
  120. )
  121. expect(changeAtTermEnd).to.be.false
  122. })
  123. it('should return false if the change is from an individual plan to a more expensive group plan', function () {
  124. const changeAtTermEnd = this.SubscriptionHelper.shouldPlanChangeAtTermEnd(
  125. plans.expensive,
  126. plans.expensiveGroup
  127. )
  128. expect(changeAtTermEnd).to.be.false
  129. })
  130. it('should return true if the change is from an individual plan to a cheaper group plan', function () {
  131. const changeAtTermEnd = this.SubscriptionHelper.shouldPlanChangeAtTermEnd(
  132. plans.expensive,
  133. plans.cheapGroup
  134. )
  135. expect(changeAtTermEnd).to.be.true
  136. })
  137. })
  138. describe('generateInitialLocalizedGroupPrice', function () {
  139. describe('CHF currency', function () {
  140. it('should return the correct localized price for every plan', function () {
  141. const localizedPrice =
  142. this.SubscriptionHelper.generateInitialLocalizedGroupPrice(
  143. 'CHF',
  144. 'fr'
  145. )
  146. expect(localizedPrice).to.deep.equal({
  147. price: {
  148. collaborator: '10 CHF',
  149. professional: '100 CHF',
  150. },
  151. pricePerUser: {
  152. collaborator: '5 CHF',
  153. professional: '50 CHF',
  154. },
  155. pricePerUserPerMonth: {
  156. collaborator: '0,45 CHF',
  157. professional: '4,20 CHF',
  158. },
  159. })
  160. })
  161. })
  162. describe('DKK currency', function () {
  163. it('should return the correct localized price for every plan', function () {
  164. const localizedPrice =
  165. this.SubscriptionHelper.generateInitialLocalizedGroupPrice(
  166. 'DKK',
  167. 'da'
  168. )
  169. expect(localizedPrice).to.deep.equal({
  170. price: {
  171. collaborator: '20 kr.',
  172. professional: '200 kr.',
  173. },
  174. pricePerUser: {
  175. collaborator: '10 kr.',
  176. professional: '100 kr.',
  177. },
  178. pricePerUserPerMonth: {
  179. collaborator: '0,85 kr.',
  180. professional: '8,35 kr.',
  181. },
  182. })
  183. })
  184. })
  185. describe('SEK currency', function () {
  186. it('should return the correct localized price for every plan', function () {
  187. const localizedPrice =
  188. this.SubscriptionHelper.generateInitialLocalizedGroupPrice(
  189. 'SEK',
  190. 'sv'
  191. )
  192. expect(localizedPrice).to.deep.equal({
  193. price: {
  194. collaborator: '30 kr',
  195. professional: '300 kr',
  196. },
  197. pricePerUser: {
  198. collaborator: '15 kr',
  199. professional: '150 kr',
  200. },
  201. pricePerUserPerMonth: {
  202. collaborator: '1,25 kr',
  203. professional: '12,50 kr',
  204. },
  205. })
  206. })
  207. })
  208. describe('NOK currency', function () {
  209. it('should return the correct localized price for every plan', function () {
  210. const localizedPrice =
  211. this.SubscriptionHelper.generateInitialLocalizedGroupPrice(
  212. 'NOK',
  213. // there seem to be possible inconsistencies with the CI
  214. // maybe it depends on what languages are installed on the server?
  215. 'en'
  216. )
  217. expect(localizedPrice).to.deep.equal({
  218. price: {
  219. collaborator: 'kr 40',
  220. professional: 'kr 400',
  221. },
  222. pricePerUser: {
  223. collaborator: 'kr 20',
  224. professional: 'kr 200',
  225. },
  226. pricePerUserPerMonth: {
  227. collaborator: 'kr 1.70',
  228. professional: 'kr 16.70',
  229. },
  230. })
  231. })
  232. })
  233. describe('other supported currencies', function () {
  234. it('should return the correct localized price for every plan', function () {
  235. const localizedPrice =
  236. this.SubscriptionHelper.generateInitialLocalizedGroupPrice(
  237. 'USD',
  238. 'en'
  239. )
  240. expect(localizedPrice).to.deep.equal({
  241. price: {
  242. collaborator: '$50',
  243. professional: '$500',
  244. },
  245. pricePerUser: {
  246. collaborator: '$25',
  247. professional: '$250',
  248. },
  249. pricePerUserPerMonth: {
  250. collaborator: '$2.10',
  251. professional: '$20.85',
  252. },
  253. })
  254. })
  255. })
  256. })
  257. })