Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Apr 17, 2024
1 parent 9fa7b00 commit 2f9fdda
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 191 deletions.
33 changes: 13 additions & 20 deletions __tests__/payment-details/amount-lightning-payment-details.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
createGetFeeMocks,
createSendPaymentMocks,
expectDestinationSpecifiedMemoCannotSetMemo,
getTestSetMemo,
getTestSetSendingWalletDescriptor,
usdSendingWalletDescriptor,
} from "./helpers"

Expand All @@ -21,15 +19,9 @@ const defaultParams: PaymentDetails.CreateAmountLightningPaymentDetailsParams<Wa
sendingWalletDescriptor: btcSendingWalletDescriptor,
}

const spy = jest.spyOn(PaymentDetails, "createAmountLightningPaymentDetails")

describe("amount lightning payment details", () => {
const { createAmountLightningPaymentDetails } = PaymentDetails

beforeEach(() => {
spy.mockClear()
})

it("properly sets fields with all arguments provided", () => {
const paymentDetails = createAmountLightningPaymentDetails(defaultParams)
expect(paymentDetails).toEqual(
Expand Down Expand Up @@ -168,20 +160,21 @@ describe("amount lightning payment details", () => {
})

it("can set memo if no memo provided", () => {
const testSetMemo = getTestSetMemo()
testSetMemo({
defaultParams,
spy,
creatorFunction: createAmountLightningPaymentDetails,
})
const paymentDetails = createAmountLightningPaymentDetails(defaultParams)
const senderSpecifiedMemo = "sender memo"
if (!paymentDetails.canSetMemo) throw new Error("Memo is unable to be set")

const newPaymentDetails = paymentDetails.setMemo(senderSpecifiedMemo)
expect(newPaymentDetails.memo).toEqual(senderSpecifiedMemo)
})

it("can set sending wallet descriptor", () => {
const testSetSendingWalletDescriptor = getTestSetSendingWalletDescriptor()
testSetSendingWalletDescriptor({
defaultParams,
spy,
creatorFunction: createAmountLightningPaymentDetails,
})
const paymentDetails = createAmountLightningPaymentDetails(defaultParams)
const sendingWalletDescriptor = {
currency: WalletCurrency.Btc,
id: "newtestwallet",
}
const newPaymentDetails = paymentDetails.setSendingWalletDescriptor(sendingWalletDescriptor)
expect(newPaymentDetails.sendingWalletDescriptor).toEqual(sendingWalletDescriptor)
})
})
33 changes: 13 additions & 20 deletions __tests__/payment-details/amount-onchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
createGetFeeMocks,
createSendPaymentMocks,
expectDestinationSpecifiedMemoCannotSetMemo,
getTestSetMemo,
getTestSetSendingWalletDescriptor,
testAmount,
usdSendingWalletDescriptor,
} from "./helpers"
Expand All @@ -21,15 +19,9 @@ const defaultParams: PaymentDetails.CreateAmountOnchainPaymentDetailsParams<Wall
sendingWalletDescriptor: btcSendingWalletDescriptor,
}

const spy = jest.spyOn(PaymentDetails, "createAmountOnchainPaymentDetails")

describe("no amount onchain payment details", () => {
const { createAmountOnchainPaymentDetails } = PaymentDetails

beforeEach(() => {
spy.mockClear()
})

it("properly sets fields with all arguments provided", () => {
const paymentDetails = createAmountOnchainPaymentDetails(defaultParams)
expect(paymentDetails).toEqual(
Expand Down Expand Up @@ -174,20 +166,21 @@ describe("no amount onchain payment details", () => {
})

it("can set memo if no memo provided", () => {
const testSetMemo = getTestSetMemo()
testSetMemo({
defaultParams,
spy,
creatorFunction: createAmountOnchainPaymentDetails,
})
const paymentDetails = createAmountOnchainPaymentDetails(defaultParams)
const senderSpecifiedMemo = "sender memo"
if (!paymentDetails.canSetMemo) throw new Error("Memo is unable to be set")

const newPaymentDetails = paymentDetails.setMemo(senderSpecifiedMemo)
expect(newPaymentDetails.memo).toEqual(senderSpecifiedMemo)
})

it("can set sending wallet descriptor", () => {
const testSetSendingWalletDescriptor = getTestSetSendingWalletDescriptor()
testSetSendingWalletDescriptor({
defaultParams,
spy,
creatorFunction: createAmountOnchainPaymentDetails,
})
const paymentDetails = createAmountOnchainPaymentDetails(defaultParams)
const sendingWalletDescriptor = {
currency: WalletCurrency.Btc,
id: "newtestwallet",
}
const newPaymentDetails = paymentDetails.setSendingWalletDescriptor(sendingWalletDescriptor)
expect(newPaymentDetails.sendingWalletDescriptor).toEqual(sendingWalletDescriptor)
})
})
50 changes: 0 additions & 50 deletions __tests__/payment-details/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ export const usdSendingWalletDescriptor = {
id: "testwallet",
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type CreateFunctionWithSpyParams<T extends (...args: any) => any> = {
spy: jest.SpyInstance<ReturnType<T>, Parameters<T>>
defaultParams: Parameters<T>
creatorFunction: T
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type CreateFunctionWithSpy = <T extends (...args: any) => any>() => (
params: CreateFunctionWithSpyParams<T>,
) => void

export const expectDestinationSpecifiedMemoCannotSetMemo = <T extends WalletCurrency>(
paymentDetails: PaymentDetail<T>,
destinationSpecifiedMemo: string,
Expand All @@ -70,44 +58,6 @@ export const expectCannotSendPayment = (
expect(paymentDetails.sendPaymentMutation).toBeUndefined()
}

export const getTestSetMemo: CreateFunctionWithSpy = () => (params) => {
const { defaultParams, creatorFunction, spy } = params
const senderSpecifiedMemo = "sender memo"
const paymentDetails = creatorFunction(defaultParams)

if (!paymentDetails.canSetMemo) throw new Error("Memo is unable to be set")
paymentDetails.setMemo(senderSpecifiedMemo)

const lastCall = spy.mock.lastCall && spy.mock.lastCall[0]
expect(lastCall).toEqual({ ...defaultParams, senderSpecifiedMemo })
}

export const getTestSetAmount: CreateFunctionWithSpy = () => (params) => {
const { defaultParams, creatorFunction, spy } = params
const paymentDetails = creatorFunction(defaultParams)
const unitOfAccountAmount = {
amount: 100,
currency: WalletCurrency.Btc,
}
if (!paymentDetails.canSetAmount) throw new Error("Amount is unable to be set")
paymentDetails.setAmount(unitOfAccountAmount)
const lastCall = spy.mock.lastCall && spy.mock.lastCall[0]
expect(lastCall).toEqual({ ...defaultParams, unitOfAccountAmount })
}

export const getTestSetSendingWalletDescriptor: CreateFunctionWithSpy =
() => (params) => {
const { defaultParams, creatorFunction, spy } = params
const paymentDetails = creatorFunction(defaultParams)
const sendingWalletDescriptor = {
currency: WalletCurrency.Btc,
id: "newtestwallet",
}
paymentDetails.setSendingWalletDescriptor(sendingWalletDescriptor)
const lastCall = spy.mock.lastCall && spy.mock.lastCall[0]
expect(lastCall).toEqual({ ...defaultParams, sendingWalletDescriptor })
}

export const createGetFeeMocks = (): GetFeeParams => {
return {
lnInvoiceFeeProbe: jest.fn(),
Expand Down
50 changes: 23 additions & 27 deletions __tests__/payment-details/intraledger-payment-details.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {
expectCannotGetFee,
expectCannotSendPayment,
expectDestinationSpecifiedMemoCannotSetMemo,
getTestSetAmount,
getTestSetMemo,
getTestSetSendingWalletDescriptor,
testAmount,
usdSendingWalletDescriptor,
zeroAmount,
Expand All @@ -25,15 +22,9 @@ const defaultParams: PaymentDetails.CreateIntraledgerPaymentDetailsParams<Wallet
unitOfAccountAmount: testAmount,
}

const spy = jest.spyOn(PaymentDetails, "createIntraledgerPaymentDetails")

describe("intraledger payment details", () => {
const { createIntraledgerPaymentDetails } = PaymentDetails

beforeEach(() => {
spy.mockClear()
})

it("properly sets fields with all arguments provided", () => {
const paymentDetails = createIntraledgerPaymentDetails(defaultParams)
expect(paymentDetails).toEqual(
Expand Down Expand Up @@ -150,29 +141,34 @@ describe("intraledger payment details", () => {
})

it("can set memo if no memo provided", () => {
const testSetMemo = getTestSetMemo()
testSetMemo({
defaultParams,
spy,
creatorFunction: createIntraledgerPaymentDetails,
})
const paymentDetails = createIntraledgerPaymentDetails(defaultParams)
const senderSpecifiedMemo = "sender memo"
if (!paymentDetails.canSetMemo) throw new Error("Memo is unable to be set")

const newPaymentDetails = paymentDetails.setMemo(senderSpecifiedMemo)
expect(newPaymentDetails.memo).toEqual(senderSpecifiedMemo)
})

it("can set amount", () => {
const testSetAmount = getTestSetAmount()
testSetAmount({
defaultParams,
spy,
creatorFunction: createIntraledgerPaymentDetails,
})
const paymentDetails = createIntraledgerPaymentDetails(defaultParams)
const unitOfAccountAmount = {
amount: 100,
currency: WalletCurrency.Btc,
currencyCode: "BTC",
}
if (!paymentDetails.canSetAmount) throw new Error("Amount is unable to be set")
const newPaymentDetails = paymentDetails.setAmount(unitOfAccountAmount)

expect(newPaymentDetails.unitOfAccountAmount).toEqual(unitOfAccountAmount)
})

it("can set sending wallet descriptor", () => {
const testSetSendingWalletDescriptor = getTestSetSendingWalletDescriptor()
testSetSendingWalletDescriptor({
defaultParams,
spy,
creatorFunction: createIntraledgerPaymentDetails,
})
const paymentDetails = createIntraledgerPaymentDetails(defaultParams)
const sendingWalletDescriptor = {
currency: WalletCurrency.Btc,
id: "newtestwallet",
}
const newPaymentDetails = paymentDetails.setSendingWalletDescriptor(sendingWalletDescriptor)
expect(newPaymentDetails.sendingWalletDescriptor).toEqual(sendingWalletDescriptor)
})
})
37 changes: 17 additions & 20 deletions __tests__/payment-details/lnurl-payment-details.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
convertMoneyAmountMock,
createGetFeeMocks,
createSendPaymentMocks,
getTestSetAmount,
getTestSetSendingWalletDescriptor,
testAmount,
usdSendingWalletDescriptor,
} from "./helpers"
Expand Down Expand Up @@ -54,15 +52,9 @@ const defaultParamsWithEqualMinMaxAmount = {
lnurlParams: mockLnUrlPayServiceResponse(100 as Satoshis, 100 as Satoshis),
}

const spy = jest.spyOn(PaymentDetails, "createLnurlPaymentDetails")

describe("lnurl payment details", () => {
const { createLnurlPaymentDetails } = PaymentDetails

beforeEach(() => {
spy.mockClear()
})

it("properly sets fields if min and max amount is equal", () => {
const paymentDetails = createLnurlPaymentDetails(defaultParamsWithEqualMinMaxAmount)
expect(paymentDetails).toEqual(
Expand Down Expand Up @@ -229,20 +221,25 @@ describe("lnurl payment details", () => {
})

it("can set amount", () => {
const testSetAmount = getTestSetAmount()
testSetAmount({
defaultParams: defaultParamsWithoutInvoice,
spy,
creatorFunction: createLnurlPaymentDetails,
})
const paymentDetails = createLnurlPaymentDetails(defaultParamsWithoutInvoice)
const unitOfAccountAmount = {
amount: 100,
currency: WalletCurrency.Btc,
currencyCode: "BTC",
}
if (!paymentDetails.canSetAmount) throw new Error("Amount is unable to be set")
const newPaymentDetails = paymentDetails.setAmount(unitOfAccountAmount)

expect(newPaymentDetails.unitOfAccountAmount).toEqual(unitOfAccountAmount)
})

it("can set sending wallet descriptor", () => {
const testSetSendingWalletDescriptor = getTestSetSendingWalletDescriptor()
testSetSendingWalletDescriptor({
defaultParams: defaultParamsWithoutInvoice,
spy,
creatorFunction: createLnurlPaymentDetails,
})
const paymentDetails = createLnurlPaymentDetails(defaultParamsWithoutInvoice)
const sendingWalletDescriptor = {
currency: WalletCurrency.Btc,
id: "newtestwallet",
}
const newPaymentDetails = paymentDetails.setSendingWalletDescriptor(sendingWalletDescriptor)
expect(newPaymentDetails.sendingWalletDescriptor).toEqual(sendingWalletDescriptor)
})
})
Loading

0 comments on commit 2f9fdda

Please sign in to comment.