Skip to content

Commit

Permalink
chore: updating typescript to 5, removing ts-auto-mock
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nicolas Burtey committed Dec 16, 2023
1 parent 98a2974 commit 6b096c6
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 245 deletions.
45 changes: 40 additions & 5 deletions __tests__/components/transaction-date.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import * as React from "react"
import { render } from "@testing-library/react-native"
import { createMock } from "ts-auto-mock"
import * as React from "react"

import { TransactionDate } from "../../app/components/transaction-date"
import {
Transaction,
TxDirection,
TxStatus,
WalletCurrency,
InitiationVia,
SettlementVia,
PriceOfOneSettlementMinorUnitInDisplayMinorUnit,
} from "../../app/graphql/generated"
import { i18nObject } from "../../app/i18n/i18n-util"
import { Transaction } from "../../app/graphql/generated"

jest.mock("@app/i18n/i18n-react", () => ({
useI18nContext: () => {
return i18nObject("en")
},
}))

// Assuming TxDirection, InitiationVia, WalletCurrency, SettlementVia, TxStatus are enums or specific string types
const createMockTransaction = (
partialTransaction: Partial<Transaction>,
): Transaction => ({
__typename: "Transaction",
createdAt: new Date().getTime(),
date: "mockDate",
date_format: "mockDateFormat",

Check failure on line 29 in __tests__/components/transaction-date.spec.tsx

View workflow job for this annotation

GitHub Actions / Check Code

Identifier 'date_format' is not in camel case
date_nice_print: "mockDateNicePrint",

Check failure on line 30 in __tests__/components/transaction-date.spec.tsx

View workflow job for this annotation

GitHub Actions / Check Code

Identifier 'date_nice_print' is not in camel case
direction: TxDirection.Receive, // Replace with a valid value from TxDirection
id: "mockId",
initiationVia: "InitiationVia" as unknown as InitiationVia,
isReceive: false,
memo: "mockMemo",
settlementAmount: 0, // Replace with a valid number
settlementCurrency: WalletCurrency.Btc, // Replace with a valid value from WalletCurrency
settlementDisplayAmount: "mockSettlementDisplayAmount",
settlementDisplayCurrency: "mockSettlementDisplayCurrency",
settlementDisplayFee: "mockSettlementDisplayFee",
settlementFee: 0, // Replace with a valid number
settlementPrice: "price" as unknown as PriceOfOneSettlementMinorUnitInDisplayMinorUnit, // Replace with a valid object of type PriceOfOneSettlementMinorUnitInDisplayMinorUnit
settlementVia: "SettlementVia" as unknown as SettlementVia, // Replace with a valid value from SettlementVia
status: TxStatus.Failure, // Replace with a valid value from TxStatus
text: "mockText",
...partialTransaction,
})

describe("Display the createdAt date for a transaction", () => {
it("Displays pending for a pending onchain transaction", () => {
const mockedTransaction = createMock<Transaction>({
const mockedTransaction = createMockTransaction({
status: "PENDING",
createdAt: new Date().getDate(),
})
Expand All @@ -27,10 +61,11 @@ describe("Display the createdAt date for a transaction", () => {
)
expect(queryAllByText("pending")).not.toBeNull()
})

it("Displays friendly date", () => {
const testTransactionCreatedAtDate = new Date()
testTransactionCreatedAtDate.setDate(testTransactionCreatedAtDate.getDate() - 1)
const mockedTransaction = createMock<Transaction>({
const mockedTransaction = createMockTransaction({
createdAt: Math.floor(testTransactionCreatedAtDate.getTime() / 1000),
})

Expand Down
108 changes: 80 additions & 28 deletions __tests__/payment-destination/lnurl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createLnurlPaymentDetails } from "@app/screens/send-bitcoin-screen/payment-details"
import { createMock } from "ts-auto-mock"

import {
createLnurlPaymentDestination,
Expand All @@ -8,28 +7,22 @@ import {
import { DestinationDirection } from "@app/screens/send-bitcoin-screen/payment-destination/index.types"
import { ZeroBtcMoneyAmount } from "@app/types/amounts"
import { PaymentType } from "@galoymoney/client"
import { LNURLPayParams, LNURLResponse, LNURLWithdrawParams, getParams } from "js-lnurl"
import { LnUrlPayServiceResponse } from "lnurl-pay/dist/types/types"
import { LNURLResponse, LNURLWithdrawParams, getParams } from "js-lnurl"
import { LnUrlPayServiceResponse, Satoshis } from "lnurl-pay/dist/types/types"
import { defaultPaymentDetailParams } from "./helpers"
import { requestPayServiceParams } from "lnurl-pay"

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 @@ -43,6 +36,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 @@ -57,11 +90,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 @@ -92,11 +126,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 @@ -128,7 +162,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 @@ -165,11 +199,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,7 +1,6 @@
import { WalletCurrency } from "@app/graphql/generated"
import * as PaymentDetails from "@app/screens/send-bitcoin-screen/payment-details/lightning"
import { LnUrlPayServiceResponse, Satoshis } from "lnurl-pay/dist/types/types"
import { createMock } from "ts-auto-mock"
import {
btcSendingWalletDescriptor,
btcTestAmount,
Expand All @@ -14,12 +13,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 @@ -33,10 +49,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
Loading

0 comments on commit 6b096c6

Please sign in to comment.