Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Paddle.Update feature #29

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paddle/paddle-js",
"version": "1.0.1",
"version": "1.0.2",
"description": "Wrapper to load Paddle.js as a module and use TypeScript definitions when working with methods.",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
27 changes: 22 additions & 5 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,50 @@ const mockedPaddleInstance: Paddle = {
hide: jest.fn(),
show: jest.fn(),
},
Initialized: false,
Initialize: jest.fn(),
Update: jest.fn(),
};
describe('initializePaddle', () => {
afterEach(() => {
jest.clearAllMocks();
});
test('Should initialize Paddle with Seller ID', async () => {
jest.spyOn(shared, 'loadFromCDN').mockResolvedValue(Promise.resolve(mockedPaddleInstance));
const paddle = await initializePaddle({ seller: 1 });
expect(paddle?.Status.libraryVersion).toBe('2.0');
expect(paddle?.Setup).toBeCalledWith({ seller: 1 });
expect(paddle?.Initialize).toBeCalledWith({ seller: 1 });
});

test('Should initialize Paddle with Token', async () => {
jest.spyOn(shared, 'loadFromCDN').mockResolvedValue(Promise.resolve(mockedPaddleInstance));
const paddle = await initializePaddle({ token: 'TOKEN' });
expect(paddle?.Status.libraryVersion).toBe('2.0');
expect(paddle?.Setup).toBeCalledWith({ token: 'TOKEN' });
expect(paddle?.Initialize).toBeCalledWith({ token: 'TOKEN' });
});

test('Should initialize Paddle with Sandbox Environment', async () => {
jest.spyOn(shared, 'loadFromCDN').mockResolvedValue(Promise.resolve(mockedPaddleInstance));
const paddle = await initializePaddle({ seller: 1, environment: 'sandbox' });
expect(paddle?.Environment.set).toBeCalledWith('sandbox');
expect(paddle?.Setup).toBeCalledWith({ seller: 1 });
expect(paddle?.Initialize).toBeCalledWith({ seller: 1 });
});

test('Should call update if Paddle is already initialized', async () => {
const updatedMockedPaddleInstance = { ...mockedPaddleInstance, Initialized: true };
jest.spyOn(shared, 'loadFromCDN').mockResolvedValue(Promise.resolve(updatedMockedPaddleInstance));
const paddle = await initializePaddle({ seller: 1, environment: 'sandbox' });
expect(paddle?.Environment.set).toBeCalledWith('sandbox');
expect(paddle?.Initialize).not.toBeCalled();
expect(paddle?.Update).toBeCalledWith({ seller: 1 });
});

test('Should return error when initialization fails', async () => {
const consoleWarn = jest.spyOn(console, 'warn');
// @ts-expect-error - undefined is not a valid value
jest.spyOn(shared, 'loadFromCDN').mockResolvedValue(Promise.resolve({ ...mockedPaddleInstance, Setup: undefined }));
jest
.spyOn(shared, 'loadFromCDN')
// @ts-expect-error - undefined is not a valid value for Initialize
.mockResolvedValue(Promise.resolve({ ...mockedPaddleInstance, Initialize: undefined }));
await initializePaddle({ seller: 1, environment: 'sandbox' });
expect(consoleWarn).toBeCalledTimes(1);
});
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const mockedPaddleInstance: Paddle = {
hide: jest.fn(),
show: jest.fn(),
},
Initialized: false,
Initialize: jest.fn(),
Update: jest.fn(),
};

interface SharedModule {
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export async function initializePaddle(options?: InitializePaddleOptions): Promi
if (environment) {
paddle.Environment.set(environment);
}
paddle.Setup({
...rest,
});
if (paddle.Initialized) {
paddle.Update({ ...rest });
} else {
paddle.Initialize({ ...rest });
}
} catch (e) {
console.warn('Paddle Initialization failed. Please check the inputs', e);
}
Expand Down
10 changes: 6 additions & 4 deletions types/checkout/checkout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export interface CheckoutSettings {
allowedPaymentMethods?: AvailablePaymentMethod[];
}

export interface PaddleSetupPwCustomer {
id?: string;
email?: string;
}

interface PaddleSetupBaseOptions {
pwAuth?: string;
pwCustomer?: {
id?: string;
email?: string;
};
pwCustomer?: PaddleSetupPwCustomer;
debug?: boolean;
eventCallback?: (event: PaddleEventData) => void;
checkout?: {
Expand Down
10 changes: 9 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PaddleSetupOptions,
CheckoutSettings,
PaddleEventData,
PaddleSetupPwCustomer,
} from './checkout/checkout';
import { CheckoutCustomer, CheckoutCustomerAddress, CheckoutCustomerBusiness } from './checkout/customer';
import { PricePreviewItem, PricePreviewParams, PricePreviewResponse } from './price-preview/price-preview';
Expand Down Expand Up @@ -47,6 +48,7 @@ export type Environments = 'production' | 'sandbox';
export type Theme = 'light' | 'dark';

export {
PaddleSetupPwCustomer,
AvailablePaymentMethod,
CheckoutOpenOptions,
PaddleSetupOptions,
Expand Down Expand Up @@ -84,6 +86,9 @@ export interface Paddle {
};
PricePreview: (params: PricePreviewParams) => Promise<PricePreviewResponse>;
TransactionPreview: (params: TransactionPreviewParams) => Promise<TransactionPreviewResponse>;
/**
@deprecated. Use `Paddle.Initialize` instead.
*/
Setup(options: PaddleSetupOptions): void;
Spinner: {
show(): void;
Expand All @@ -92,12 +97,15 @@ export interface Paddle {
Status: {
libraryVersion: string;
};
Initialized: boolean;
Initialize(options: PaddleSetupOptions): void;
Update(options: Partial<PaddleSetupOptions>): void;
}

declare global {
interface Window {
// Paddle.JS will be downloaded directly from our CDN and added to global variable
Paddle?: Paddle;
Paddle?: Paddle | undefined;
}
}

Expand Down
Loading