Skip to content

Commit

Permalink
chore: updating typescript to 5, removing ts-auto-mock (#2905)
Browse files Browse the repository at this point in the history
* chore: updating typescript to 5, removing ts-auto-mock

ts-auto-mock is been deprecated

https://github.com/Typescript-TDD/ts-auto-mock/

and it was what prevented us to go to typescript 5

ts-auto-mock relies on ttypescript and ttypescript doesn't work with typescript 5

I tried ttsc unsuccesfully

I'm not sure if this is the best way to go forward, this PR adds quite a bit of not-very-useful code, mostly generated by GPT.

but otherwise not been able to update typescript is not ideal either

* chore: fix check code

---------

Co-authored-by: Nicolas Burtey <[email protected]>
Co-authored-by: Sam Peters <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent e2817fa commit caf4712
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 261 deletions.
108 changes: 80 additions & 28 deletions __tests__/payment-destination/lnurl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LNURLPayParams, LNURLResponse, LNURLWithdrawParams, getParams } from "js-lnurl"
import { LNURLResponse, LNURLWithdrawParams, getParams } from "js-lnurl"
import { requestPayServiceParams } from "lnurl-pay"
import { LnUrlPayServiceResponse } from "lnurl-pay/dist/types/types"
import { createMock } from "ts-auto-mock"
import { LnUrlPayServiceResponse, Satoshis } from "lnurl-pay/dist/types/types"

import {
createLnurlPaymentDestination,
Expand All @@ -14,23 +13,17 @@ import { PaymentType } from "@galoymoney/client"

import { defaultPaymentDetailParams } from "./helpers"

jest.mock("lnurl-pay", () => {
return {
requestPayServiceParams: jest.fn(),
}
})
jest.mock("lnurl-pay", () => ({
requestPayServiceParams: jest.fn(),
}))

jest.mock("js-lnurl", () => {
return {
getParams: jest.fn(),
}
})
jest.mock("js-lnurl", () => ({
getParams: jest.fn(),
}))

jest.mock("@app/screens/send-bitcoin-screen/payment-details", () => {
return {
createLnurlPaymentDetails: jest.fn(),
}
})
jest.mock("@app/screens/send-bitcoin-screen/payment-details", () => ({
createLnurlPaymentDetails: jest.fn(),
}))

const mockRequestPayServiceParams = requestPayServiceParams as jest.MockedFunction<
typeof requestPayServiceParams
Expand All @@ -44,6 +37,46 @@ const throwError = () => {
throw new Error("test error")
}

// Manual mocks for LnUrlPayServiceResponse and LNURLResponse
const manualMockLnUrlPayServiceResponse = (
identifier: string,
): LnUrlPayServiceResponse => ({
callback: "mocked_callback",
fixed: true,
min: 0 as Satoshis,
max: 2000 as Satoshis,
domain: "example.com",
metadata: [
["text/plain", "description"],
["image/png;base64", "base64EncodedImage"],
],
metadataHash: "mocked_metadata_hash",
identifier,
description: "mocked_description",
image: "mocked_image_url",
commentAllowed: 140,
rawData: {},
})

const manualMockLNURLResponse = (): LNURLResponse => ({
status: "string",
reason: "string",
domain: "string",
url: "string",
})

const manualMockLNURLWithdrawParams = (): LNURLWithdrawParams => ({
// Example structure. Adjust according to your actual LNURLWithdrawParams type
tag: "withdrawRequest",
k1: "some_random_string",
callback: "http://example.com/callback",
domain: "example.com",
maxWithdrawable: 2000,
minWithdrawable: 0,
defaultDescription: "Test withdraw",
// ... add other required properties
})

describe("resolve lnurl destination", () => {
describe("with ln address", () => {
const lnurlPaymentDestinationParams = {
Expand All @@ -58,11 +91,12 @@ describe("resolve lnurl destination", () => {
}

it("creates lnurl pay destination", async () => {
const lnurlPayParams = createMock<LnUrlPayServiceResponse>({
identifier: lnurlPaymentDestinationParams.parsedLnurlDestination.lnurl,
})
const lnurlPayParams = manualMockLnUrlPayServiceResponse(
lnurlPaymentDestinationParams.parsedLnurlDestination.lnurl,
)

mockRequestPayServiceParams.mockResolvedValue(lnurlPayParams)
mockGetParams.mockResolvedValue(createMock<LNURLResponse>())
mockGetParams.mockResolvedValue(manualMockLNURLResponse())

const destination = await resolveLnurlDestination(lnurlPaymentDestinationParams)

Expand Down Expand Up @@ -93,11 +127,11 @@ describe("resolve lnurl destination", () => {
}

it("creates lnurl pay destination", async () => {
const lnurlPayParams = createMock<LnUrlPayServiceResponse>({
identifier: lnurlPaymentDestinationParams.parsedLnurlDestination.lnurl,
})
const lnurlPayParams = manualMockLnUrlPayServiceResponse(
lnurlPaymentDestinationParams.parsedLnurlDestination.lnurl,
)
mockRequestPayServiceParams.mockResolvedValue(lnurlPayParams)
mockGetParams.mockResolvedValue(createMock<LNURLPayParams>())
mockGetParams.mockResolvedValue(manualMockLNURLResponse())

const destination = await resolveLnurlDestination(lnurlPaymentDestinationParams)

Expand Down Expand Up @@ -129,7 +163,7 @@ describe("resolve lnurl destination", () => {

it("creates lnurl withdraw destination", async () => {
mockRequestPayServiceParams.mockImplementation(throwError)
const mockLnurlWithdrawParams = createMock<LNURLWithdrawParams>()
const mockLnurlWithdrawParams = manualMockLNURLWithdrawParams()
mockGetParams.mockResolvedValue(mockLnurlWithdrawParams)

const destination = await resolveLnurlDestination(lnurlPaymentDestinationParams)
Expand Down Expand Up @@ -166,11 +200,29 @@ describe("resolve lnurl destination", () => {

describe("create lnurl destination", () => {
it("correctly creates payment detail", () => {
const manualMockLnUrlPayServiceResponse = {
callback: "mocked_callback",
fixed: true,
min: 0 as Satoshis,
max: 2000 as Satoshis,
domain: "example.com",
metadata: [
["text/plain", "description"],
["image/png;base64", "base64EncodedImage"],
],
metadataHash: "mocked_metadata_hash",
identifier: "testlnurl",
description: "mocked_description",
image: "mocked_image_url",
commentAllowed: 140,
rawData: {},
}

const lnurlPaymentDestinationParams = {
paymentType: "lnurl",
valid: true,
lnurl: "testlnurl",
lnurlParams: createMock<LnUrlPayServiceResponse>(),
lnurlParams: manualMockLnUrlPayServiceResponse,
} as const

const lnurlPayDestination = createLnurlPaymentDestination(
Expand Down
31 changes: 22 additions & 9 deletions __tests__/payment-details/lnurl-payment-details.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LnUrlPayServiceResponse, Satoshis } from "lnurl-pay/dist/types/types"
import { createMock } from "ts-auto-mock"

import { WalletCurrency } from "@app/graphql/generated"
import * as PaymentDetails from "@app/screens/send-bitcoin-screen/payment-details/lightning"
Expand All @@ -16,12 +15,29 @@ import {
usdSendingWalletDescriptor,
} from "./helpers"

const mockLnUrlPayServiceResponse = (
min: Satoshis,
max: Satoshis,
): LnUrlPayServiceResponse => ({
callback: "mockCallbackUrl",
fixed: false,
min,
max,
domain: "mockDomain",
metadata: [["mockMetadata"]],
metadataHash: "mockMetadataHash",
identifier: "mockIdentifier",
description: "mockDescription",
image: "mockImageUrl",
commentAllowed: 0,
rawData: {
mockKey: "mockValue",
},
})

const defaultParamsWithoutInvoice = {
lnurl: "testlnurl",
lnurlParams: createMock<LnUrlPayServiceResponse>({
min: 1 as Satoshis,
max: 1000 as Satoshis,
}),
lnurlParams: mockLnUrlPayServiceResponse(1 as Satoshis, 1000 as Satoshis),
convertMoneyAmount: convertMoneyAmountMock,
sendingWalletDescriptor: btcSendingWalletDescriptor,
unitOfAccountAmount: testAmount,
Expand All @@ -35,10 +51,7 @@ const defaultParamsWithInvoice = {

const defaultParamsWithEqualMinMaxAmount = {
...defaultParamsWithoutInvoice,
lnurlParams: createMock<LnUrlPayServiceResponse>({
min: 100 as Satoshis,
max: 100 as Satoshis,
}),
lnurlParams: mockLnUrlPayServiceResponse(100 as Satoshis, 100 as Satoshis),
}

const spy = jest.spyOn(PaymentDetails, "createLnurlPaymentDetails")
Expand Down
120 changes: 74 additions & 46 deletions __tests__/payment-request/payment-request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { createMock } from "ts-auto-mock"

import { LnInvoice } from "@app/graphql/generated"
import {
GeneratePaymentRequestMutations,
Invoice,
Expand All @@ -20,62 +17,93 @@ const btcAmountInvoice =
"lnbc23690n1p3l2qugpp5jeflfqjpxhe0hg3tzttc325j5l6czs9vq9zqx5edpt0yf7k6cypsdqqcqzpuxqyz5vqsp5lteanmnwddszwut839etrgjenfr3dv5tnvz2d2ww2mvggq7zn46q9qyyssqzcz0rvt7r30q7jul79xqqwpr4k2e8mgd23fkjm422sdgpndwql93d4wh3lap9yfwahue9n7ju80ynkqly0lrqqd2978dr8srkrlrjvcq2v5s6k"
const mockOnChainAddress = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx"

const mockLnInvoice = createMock<LnInvoice>({
// Manually created mock objects
const mockLnInvoice = {
__typename: "LnInvoice",
paymentRequest: btcAmountInvoice,
})

const mockLnInvoiceCreate = jest.fn().mockResolvedValue({
data: {
lnInvoiceCreate: {
invoice: mockLnInvoice,
errors: [],
},
},
errors: [],
})
// Add other necessary properties here
}

const mockLnUsdInvoice = createMock<LnInvoice>({
const mockLnUsdInvoice = {
__typename: "LnInvoice",
paymentRequest: usdAmountInvoice,
})

const mockLnUsdInvoiceCreate = jest.fn().mockResolvedValue({
data: {
lnUsdInvoiceCreate: {
invoice: mockLnUsdInvoice,
errors: [],
},
},
errors: [],
})
// Add other necessary properties here
}

const mockLnNoAmountInvoice = createMock<LnInvoice>({
const mockLnNoAmountInvoice = {
__typename: "LnInvoice",
paymentRequest: noAmountInvoice,
})
// Add other necessary properties here
}

const mockLnNoAmountInvoiceCreate = jest.fn().mockResolvedValue({
data: {
lnNoAmountInvoiceCreate: {
invoice: mockLnNoAmountInvoice,
errors: [],
const mockLnInvoiceCreate = jest.fn(() =>
Promise.resolve({
data: {
__typename: "Mutation", // Correct placement according to your schema
lnInvoiceCreate: {
__typename: "LnInvoicePayload",
invoice: mockLnInvoice,
errors: [],
},
},
},
errors: [],
})

const mockOnChainAddressCurrent = jest.fn().mockResolvedValue({
data: {
onChainAddressCurrent: {
address: mockOnChainAddress,
errors: [],
errors: [],
}),
)

const mockLnUsdInvoiceCreate = jest.fn(() =>
Promise.resolve({
data: {
__typename: "Mutation", // Correct placement according to your schema
lnUsdInvoiceCreate: {
__typename: "LnInvoicePayload",
invoice: mockLnUsdInvoice,
errors: [],
},
},
},
errors: [],
})
errors: [],
}),
)

const mockLnNoAmountInvoiceCreate = jest.fn(() =>
Promise.resolve({
data: {
__typename: "Mutation", // Correct placement according to your schema
lnNoAmountInvoiceCreate: {
__typename: "LnNoAmountInvoicePayload",
invoice: mockLnNoAmountInvoice,
errors: [],
},
},
errors: [],
}),
)

const mockOnChainAddressCurrent = jest.fn(() =>
Promise.resolve({
data: {
__typename: "Mutation", // Correct placement according to your schema
onChainAddressCurrent: {
__typename: "OnChainAddressPayload",
address: mockOnChainAddress,
errors: [],
},
},
errors: [],
}),
)

export const mutations: GeneratePaymentRequestMutations = {
// eslint-disable-next-line
// @ts-ignore type mismatch, but we don't care because it's a mock
lnInvoiceCreate: mockLnInvoiceCreate,
// eslint-disable-next-line
// @ts-ignore type mismatch, but we don't care because it's a mock
lnUsdInvoiceCreate: mockLnUsdInvoiceCreate,
// eslint-disable-next-line
// @ts-ignore type mismatch, but we don't care because it's a mock
lnNoAmountInvoiceCreate: mockLnNoAmountInvoiceCreate,
// eslint-disable-next-line
// @ts-ignore type mismatch, but we don't care because it's a mock
onChainAddressCurrent: mockOnChainAddressCurrent,
}

Expand Down
Loading

0 comments on commit caf4712

Please sign in to comment.