import { IUnlockOptionType, IGem } from '../type'; import { canUseJolicoin, joliCoinIsEnough, canUseGem, gemIsEnough } from '../registers/utils'; describe('canUseJolicoin', () => { it('should return true when joliCoin balance is sufficient and autoDeduct is enabled', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 100 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true); }); it('should return true when joliCoin balance is more than required', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 50 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true); }); it('should return false when joliCoin balance is insufficient', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 150 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false); }); it('should return false when autoDeduct is disabled', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 50 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: false }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false); }); it('should return false when there is no JOLI_COIN option', () => { const unlockOptions = [ { type: 'ADS' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false); }); it('should return false with empty unlockOptions', () => { const unlockOptions: any[] = []; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false); }); it('should return false when joliCoin is undefined', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 50 }], joliGemChoice: null } ]; expect(canUseJolicoin(unlockOptions, undefined)).toBe(false); }); it('should return true when at least one choice is valid', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 200 }, { joliCoinQuantity: 50 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true); }); }); describe('joliCoinIsEnough', () => { it('should return true when joliCoin balance is sufficient', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 100 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: false }; // autoDeduct disabled but should still return true expect(joliCoinIsEnough(unlockOptions, joliCoin)).toBe(true); }); it('should return false when joliCoin balance is insufficient', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 150 }], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(joliCoinIsEnough(unlockOptions, joliCoin)).toBe(false); }); it('should return false when there is no JOLI_COIN option', () => { const unlockOptions = [ { type: 'ADS' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: null } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(joliCoinIsEnough(unlockOptions, joliCoin)).toBe(false); }); }); describe('canUseGem', () => { it('should return true when gem balance is sufficient and autoDeduct is enabled', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 50 } } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(true); }); it('should return true when gem balance is more than required', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 25 } } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(true); }); it('should return false when gem balance is insufficient', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 75 } } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(false); }); it('should return false when autoDeduct is disabled', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 25 } } ]; const gem = { balance: 50, enableAutoDeduct: false }; expect(canUseGem(unlockOptions, gem)).toBe(false); }); it('should return false when there is no JOLI_GEM option', () => { const unlockOptions = [ { type: 'ADS' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: null } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(false); }); it('should return false with empty unlockOptions', () => { const unlockOptions: any[] = []; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(false); }); it('should return false when gem is undefined', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 25 } } ]; expect(canUseGem(unlockOptions, undefined)).toBe(false); }); it('should return true when at least one gem choice is valid', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 25 } } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(true); }); }); describe('gemIsEnough', () => { it('should return true when gem balance is sufficient', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 50 } } ]; const gem = { balance: 50, enableAutoDeduct: false }; // autoDeduct disabled but should still return true expect(gemIsEnough(unlockOptions, gem)).toBe(true); }); it('should return false when gem balance is insufficient', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 75 } } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(gemIsEnough(unlockOptions, gem)).toBe(false); }); it('should return false when there is no JOLI_GEM option', () => { const unlockOptions = [ { type: 'ADS' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: null } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(gemIsEnough(unlockOptions, gem)).toBe(false); }); it('should return true when at least one gem choice has sufficient balance', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 24 } } ]; const gem = { balance: 50, enableAutoDeduct: false }; expect(gemIsEnough(unlockOptions, gem)).toBe(true); }); }); describe('Mixed currency options', () => { it('should work correctly with both JOLI_COIN and JOLI_GEM options', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 100 }], joliGemChoice: null }, { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 50 } } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(true); expect(canUseGem(unlockOptions, gem)).toBe(true); expect(joliCoinIsEnough(unlockOptions, joliCoin)).toBe(true); expect(gemIsEnough(unlockOptions, gem)).toBe(true); }); it('should return false for coin when only gem options available', () => { const unlockOptions = [ { type: 'JOLI_GEM' as IUnlockOptionType, joliCoinChoices: [], joliGemChoice: { joliGemQuantity: 50 } } ]; const joliCoin = { balance: 100, enableAutoDeduct: true }; expect(canUseJolicoin(unlockOptions, joliCoin)).toBe(false); expect(joliCoinIsEnough(unlockOptions, joliCoin)).toBe(false); }); it('should return false for gem when only coin options available', () => { const unlockOptions = [ { type: 'JOLI_COIN' as IUnlockOptionType, joliCoinChoices: [{ joliCoinQuantity: 100 }], joliGemChoice: null } ]; const gem = { balance: 50, enableAutoDeduct: true }; expect(canUseGem(unlockOptions, gem)).toBe(false); expect(gemIsEnough(unlockOptions, gem)).toBe(false); }); });