1 import type { ChargebeeInstanceConfiguration } from '../lib';
2 import { createChargebee, getChargebeeInstance, isChargebeeLoaded, pollUntilLoaded, resetChargebee } from './chargebee';
5 (global as any).Chargebee = {
6 init: jest.fn().mockReturnValue({
14 it('should create instance', () => {
15 const config: ChargebeeInstanceConfiguration = {
16 publishableKey: 'pk_test_123',
20 const result = createChargebee(config);
21 expect(result).toEqual({ chargebeeMock: true });
22 expect((global as any).Chargebee.init).toHaveBeenCalledWith(config);
25 it('should save instance', () => {
26 const config: ChargebeeInstanceConfiguration = {
27 publishableKey: 'pk_test_123',
31 const result = createChargebee(config);
33 expect(getChargebeeInstance()).toEqual(result);
34 expect(getChargebeeInstance()).toEqual({
39 it('should throw error if not initialized', () => {
40 expect(() => getChargebeeInstance()).toThrow();
43 describe('isChargebeeLoaded', () => {
44 it('should return true when Chargebee is loaded', () => {
45 (global as any).Chargebee = {};
46 expect(isChargebeeLoaded()).toBe(true);
49 it('should return false when Chargebee is not loaded', () => {
50 (global as any).Chargebee = undefined;
51 expect(isChargebeeLoaded()).toBe(false);
55 describe('pollUntilLoaded', () => {
65 it('should resolve when Chargebee is loaded', async () => {
66 (global as any).Chargebee = {};
68 const promise = pollUntilLoaded();
69 await jest.runAllTimersAsync();
71 await expect(promise).resolves.toBeUndefined();
74 it('should wait and resolve when Chargebee is loaded after some time', async () => {
75 delete (global as any).Chargebee;
76 let resolved = false; // Flag to track promise resolution
77 const promise = pollUntilLoaded().then(() => {
79 }); // Set flag when resolved
81 await jest.advanceTimersByTimeAsync(20000);
83 // Check that the promise has not resolved yet
84 expect(resolved).toBe(false); // This line checks that the promise is still pending
86 (global as any).Chargebee = {};
87 await jest.advanceTimersByTimeAsync(1000);
89 await expect(promise).resolves.toBeUndefined();
92 it('should throw an error when Chargebee did not load', async () => {
93 delete (global as any).Chargebee;
94 const promise = pollUntilLoaded();
97 promise.catch((error) => {
101 await jest.runAllTimersAsync();
103 expect(reason).toEqual(new Error('Chargebee did not load'));