Skip to content

Commit

Permalink
Merge pull request #6 from tosspayments/feat/src-option
Browse files Browse the repository at this point in the history
feat: `src` 지정 옵션 구현
  • Loading branch information
HyunSeob authored Jun 21, 2021
2 parents 54db534 + 6709817 commit c30d661
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ describe('loadTossPayments', () => {

expect(scripts).toHaveLength(1);
});

test(`src를 지정하면 주어진 URL로 script를 로드한다`, async () => {
const testSource = `https://test.tosspayments.com/sdk`;
const { loadTossPayments } = await import('./index');

const loadPromise = loadTossPayments('test_key', {
src: `https://test.tosspayments.com/sdk`,
});

dispatchLoadEvent();

await loadPromise;

const script = document.querySelector(`script[src="${testSource}"]`);

expect(script).not.toBeNull();
});
});
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const SCRIPT_URL = 'https://js.tosspayments.com/v1';

let cachedPromise: Promise<TossPaymentsInstance> | undefined;

export function loadTossPayments(clientKey: string): Promise<TossPaymentsInstance> {
export function loadTossPayments(
clientKey: string,
{ src = SCRIPT_URL }: { src?: string } = {}
): Promise<TossPaymentsInstance> {
// SSR 지원
if (typeof window === 'undefined') {
return Promise.resolve({
Expand All @@ -17,7 +20,7 @@ export function loadTossPayments(clientKey: string): Promise<TossPaymentsInstanc
});
}

const selectedScript = document.querySelector(`script[src="${SCRIPT_URL}"]`);
const selectedScript = document.querySelector(`script[src="${src}"]`);

if (selectedScript != null && cachedPromise !== undefined) {
return cachedPromise;
Expand All @@ -28,7 +31,7 @@ export function loadTossPayments(clientKey: string): Promise<TossPaymentsInstanc
}

const script = document.createElement('script');
script.src = SCRIPT_URL;
script.src = src;

cachedPromise = new Promise((resolve, reject) => {
document.head.appendChild(script);
Expand Down

0 comments on commit c30d661

Please sign in to comment.