|
| 1 | +import assert from 'assert'; |
| 2 | +import { SP2MapConfigFactory } from '../factories'; |
| 3 | +import { getSP2Params } from './getSP2Params'; |
| 4 | + |
| 5 | +describe('getSP2Params', () => { |
| 6 | + const reportErrorMock = jest.fn(); |
| 7 | + const sp2mapConfig = SP2MapConfigFactory(); |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + reportErrorMock.mockClear(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('success', () => { |
| 14 | + it('successfully returns productId and planId', () => { |
| 15 | + const { productId, priceId } = getSP2Params( |
| 16 | + sp2mapConfig, |
| 17 | + reportErrorMock, |
| 18 | + 'vpn', |
| 19 | + 'monthly', |
| 20 | + 'USD' |
| 21 | + ); |
| 22 | + expect(reportErrorMock).not.toHaveBeenCalled(); |
| 23 | + expect(productId).toBe('prod_productid'); |
| 24 | + expect(priceId).toBe('price_priceId'); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('success - with default fallbacks', () => { |
| 29 | + it('successfully returns productId and planId if no currency is provided', () => { |
| 30 | + const { productId, priceId } = getSP2Params( |
| 31 | + sp2mapConfig, |
| 32 | + reportErrorMock, |
| 33 | + 'vpn', |
| 34 | + 'monthly' |
| 35 | + ); |
| 36 | + expect(reportErrorMock).toHaveBeenCalledWith('Currency is missing'); |
| 37 | + expect(productId).toBe('prod_productid'); |
| 38 | + expect(priceId).toBe('price_priceId'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('successfully returns productId and planId if invalid interval is provided', () => { |
| 42 | + const { productId, priceId } = getSP2Params( |
| 43 | + sp2mapConfig, |
| 44 | + reportErrorMock, |
| 45 | + 'vpn', |
| 46 | + 'invalid', |
| 47 | + 'USD' |
| 48 | + ); |
| 49 | + expect(reportErrorMock).toHaveBeenCalledWith( |
| 50 | + 'Interval is not supported', |
| 51 | + { interval: 'invalid' } |
| 52 | + ); |
| 53 | + expect(productId).toBe('prod_productid'); |
| 54 | + expect(priceId).toBe('price_priceId'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('failure', () => { |
| 59 | + it('throws an error if offering could not be found in config', () => { |
| 60 | + try { |
| 61 | + getSP2Params( |
| 62 | + sp2mapConfig, |
| 63 | + reportErrorMock, |
| 64 | + 'invalid', |
| 65 | + 'monthly', |
| 66 | + 'USD' |
| 67 | + ); |
| 68 | + assert('should have thrown an error'); |
| 69 | + } catch (err) { |
| 70 | + expect(reportErrorMock).toHaveBeenCalledWith( |
| 71 | + 'Missing or invalid offering', |
| 72 | + { offeringId: 'invalid' } |
| 73 | + ); |
| 74 | + expect(err).toBeInstanceOf(Error); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('throws an error if interval could not be found in config', () => { |
| 79 | + try { |
| 80 | + getSP2Params(sp2mapConfig, reportErrorMock, 'vpn', 'daily', 'USD'); |
| 81 | + assert('should have thrown an error'); |
| 82 | + } catch (err) { |
| 83 | + expect(reportErrorMock).toHaveBeenCalledWith( |
| 84 | + 'Invalid interval for offering', |
| 85 | + { offeringId: 'vpn', interval: 'daily' } |
| 86 | + ); |
| 87 | + expect(err).toBeInstanceOf(Error); |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments