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

test(core): move mongoose integration tests #3822

Merged
merged 3 commits into from
Jan 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { randomUserId, randomPhone } from "test/helpers"

const users = UsersRepository()

describe("Testing Users Repository", () => {
describe("Users Repository", () => {
it("return default value if userId doesn't exist", async () => {
const userId = randomUserId()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { WalletCurrency } from "@/domain/shared"
import { WalletInvoicesRepository } from "@/services/mongoose"
import { createMockWalletInvoice } from "test/helpers/wallet-invoices"

const walletInvoices = WalletInvoicesRepository()

const recipientWalletDescriptor = {
currency: WalletCurrency.Btc,
id: crypto.randomUUID() as WalletId,
Expand All @@ -11,11 +13,9 @@ const recipientWalletDescriptor = {
let createdInvoices: WalletInvoice[] = []

beforeAll(async () => {
const walletInvoicesRepository = WalletInvoicesRepository()

// Create 5 invoices for the same wallet
for (let i = 0; i < 5; i++) {
const invoice = await walletInvoicesRepository.persistNew(
const invoice = await walletInvoices.persistNew(
createMockWalletInvoice(recipientWalletDescriptor),
)
if (invoice instanceof Error) throw invoice
Expand All @@ -28,7 +28,7 @@ beforeAll(async () => {
})

// Create an invoice for a different wallet
await walletInvoicesRepository.persistNew(
await walletInvoices.persistNew(
createMockWalletInvoice({
currency: WalletCurrency.Btc,
id: crypto.randomUUID() as WalletId,
Expand All @@ -37,11 +37,11 @@ beforeAll(async () => {
})

describe("WalletInvoicesRepository", () => {
describe("getInvoicesForWallet", () => {
const walletInvoicesRepository = WalletInvoicesRepository()
describe("findInvoicesForWallet", () => {
const walletInvoices = WalletInvoicesRepository()

it("gets first page of invoices", async () => {
const result = await walletInvoicesRepository.findInvoicesForWallets({
const result = await walletInvoices.findInvoicesForWallets({
walletIds: [recipientWalletDescriptor.id],
paginationArgs: {
first: 100,
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("WalletInvoicesRepository", () => {
})

it("gets page after cursor", async () => {
const result = await walletInvoicesRepository.findInvoicesForWallets({
const result = await walletInvoices.findInvoicesForWallets({
walletIds: [recipientWalletDescriptor.id],
paginationArgs: {
first: 2,
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("WalletInvoicesRepository", () => {
})

it("get last page of invoices", async () => {
const result = await walletInvoicesRepository.findInvoicesForWallets({
const result = await walletInvoices.findInvoicesForWallets({
walletIds: [recipientWalletDescriptor.id],
paginationArgs: {
last: 100,
Expand All @@ -120,7 +120,7 @@ describe("WalletInvoicesRepository", () => {
})

it("get page before cursor", async () => {
const result = await walletInvoicesRepository.findInvoicesForWallets({
const result = await walletInvoices.findInvoicesForWallets({
walletIds: [recipientWalletDescriptor.id],
paginationArgs: {
last: 2,
Expand Down Expand Up @@ -154,7 +154,7 @@ describe("WalletInvoicesRepository", () => {
})

it("returns empty edges for wallet without invoices", async () => {
const result = await walletInvoicesRepository.findInvoicesForWallets({
const result = await walletInvoices.findInvoicesForWallets({
walletIds: [crypto.randomUUID() as WalletId],
paginationArgs: {
first: 100,
Expand All @@ -173,4 +173,63 @@ describe("WalletInvoicesRepository", () => {
)
})
})

describe("findByPaymentHash", () => {
it("finds an invoice", async () => {
const walletInvoice = createdInvoices[0]
const { paymentHash } = walletInvoice
const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash)
if (lookedUpInvoice instanceof Error) throw lookedUpInvoice

const dateDifference = Math.abs(
lookedUpInvoice.createdAt.getTime() - walletInvoice.createdAt.getTime(),
)
expect(dateDifference).toBeLessThanOrEqual(10) // 10ms

lookedUpInvoice.createdAt = walletInvoice.createdAt = new Date()
expect(lookedUpInvoice).toEqual(walletInvoice)
})
})

describe("markAsPaid", () => {
it("marks an invoice as paid", async () => {
const { paymentHash } = createdInvoices[0]

const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash)
expect(lookedUpInvoice).toHaveProperty("paid", false)
expect(lookedUpInvoice).toHaveProperty("processingCompleted", false)

const updatedResult = await walletInvoices.markAsPaid(paymentHash)
expect(updatedResult).not.toBeInstanceOf(Error)
expect(updatedResult).toHaveProperty("paid", true)
expect(updatedResult).toHaveProperty("processingCompleted", true)

const lookedUpInvoiceAfter = await walletInvoices.findByPaymentHash(paymentHash)
expect(lookedUpInvoiceAfter).not.toBeInstanceOf(Error)
expect(lookedUpInvoiceAfter).toEqual(updatedResult)
expect(lookedUpInvoiceAfter).toHaveProperty("paid", true)
expect(updatedResult).toHaveProperty("processingCompleted", true)
})
})

describe("markAsProcessingCompleted", () => {
it("marks an invoice as processing completed", async () => {
const { paymentHash } = createdInvoices[1]

const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash)
expect(lookedUpInvoice).toHaveProperty("paid", false)
expect(lookedUpInvoice).toHaveProperty("processingCompleted", false)

const updatedResult = await walletInvoices.markAsProcessingCompleted(paymentHash)
expect(updatedResult).not.toBeInstanceOf(Error)
expect(updatedResult).toHaveProperty("processingCompleted", true)
expect(updatedResult).toHaveProperty("paid", false)

const lookedUpInvoiceAfter = await walletInvoices.findByPaymentHash(paymentHash)
expect(lookedUpInvoiceAfter).not.toBeInstanceOf(Error)
expect(lookedUpInvoiceAfter).toEqual(updatedResult)
expect(updatedResult).toHaveProperty("processingCompleted", true)
expect(lookedUpInvoiceAfter).toHaveProperty("paid", false)
})
})
})

This file was deleted.

Loading