File size: 4,683 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import {
PLAN_PREMIUM_2_YEARS,
TYPE_FREE,
TYPE_PREMIUM,
TERM_ANNUALLY,
TERM_BIENNIALLY,
} from '@automattic/calypso-products';
import { activeDiscounts } from 'calypso/lib/discounts';
import { hasActivePromotion } from 'calypso/state/active-promotions/selectors';
import getActiveDiscount, { isDiscountActive } from 'calypso/state/selectors/get-active-discount';
import { getSitePlanSlug } from 'calypso/state/sites/selectors';
jest.mock( 'calypso/state/active-promotions/selectors', () => ( {
hasActivePromotion: jest.fn( () => null ),
} ) );
jest.mock( 'calypso/lib/discounts', () => ( {
activeDiscounts: [],
} ) );
jest.mock( 'calypso/state/sites/selectors', () => ( {
getSitePlanSlug: jest.fn( () => null ),
} ) );
jest.mock( 'calypso/state/ui/selectors', () => ( {
getSelectedSiteId: jest.fn( () => 1 ),
} ) );
const DatePlusTime = ( time ) => {
const now = new Date();
now.setTime( now.getTime() + time );
return now;
};
describe( 'isDiscountActive()', () => {
describe( 'General', () => {
beforeEach( () => {
hasActivePromotion.mockImplementation( () => true );
} );
test( 'should return false for an empty discount', () => {
expect( isDiscountActive( {} ) ).toBe( false );
} );
} );
describe( 'Date ranges', () => {
beforeEach( () => {
hasActivePromotion.mockImplementation( () => true );
} );
test( 'should return null if available discounts did not start yet', () => {
expect(
isDiscountActive( {
startsAt: DatePlusTime( 10 ),
endsAt: DatePlusTime( 100 ),
} )
).toBe( false );
} );
test( 'should return null if available discounts is already finished', () => {
expect(
isDiscountActive( {
startsAt: new Date( 2018, 1, 1 ),
endsAt: DatePlusTime( -10 ),
} )
).toBe( false );
} );
test( 'should return discount if available discounts is currently running', () => {
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
} )
).toBe( true );
} );
} );
describe( 'State checks', () => {
beforeEach( () => {
hasActivePromotion.mockImplementation( () => true );
} );
test( 'should return false if hasActivePromotion returns false', () => {
hasActivePromotion.mockImplementation( () => false );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
} )
).toBe( false );
} );
test( 'should return true if hasActivePromotion returns true', () => {
hasActivePromotion.mockImplementation( () => true );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
} )
).toBe( true );
} );
} );
describe( 'Target plans check', () => {
beforeEach( () => {
hasActivePromotion.mockImplementation( () => true );
} );
test( 'should return true if there are no target plans', () => {
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
} )
).toBe( true );
} );
test( 'should return true if target plan matches the selected site plan', () => {
getSitePlanSlug.mockImplementation( () => PLAN_PREMIUM_2_YEARS );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
targetPlans: [ { type: TYPE_PREMIUM } ],
} )
).toBe( true );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
targetPlans: [ { type: TYPE_PREMIUM, term: TERM_BIENNIALLY } ],
} )
).toBe( true );
} );
test( 'should return false if target plans do not match the selected site plan', () => {
getSitePlanSlug.mockImplementation( () => PLAN_PREMIUM_2_YEARS );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
targetPlans: [ { type: TYPE_FREE } ],
} )
).toBe( false );
expect(
isDiscountActive( {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
targetPlans: [ { type: TYPE_PREMIUM, term: TERM_ANNUALLY } ],
} )
).toBe( false );
} );
} );
} );
describe( 'getActiveDiscount()', () => {
beforeEach( () => {
hasActivePromotion.mockImplementation( () => true );
} );
afterEach( () => {
activeDiscounts.length = 0;
} );
test( 'should return null when there are no discounts', () => {
expect( getActiveDiscount( {} ) ).toBe( null );
} );
test( 'should return an active discount when there is one', () => {
const promo = {
startsAt: DatePlusTime( -10 ),
endsAt: DatePlusTime( 100 ),
};
activeDiscounts.push( promo );
expect( getActiveDiscount( {} ) ).toEqual( promo );
} );
} );
|