Skip to content
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
22 changes: 10 additions & 12 deletions __tests__/basic.test.js → __tests__/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
/// <reference types="jest" />

const { default: TCGdex, Query } = require("../src/tcgdex")
import fetch from 'node-fetch'
import { expect, test, vi } from 'vitest'
import TCGdex, { Query } from '../src/tcgdex'

// change timeout of execution
jest.setTimeout(120000)
vi.setConfig({ testTimeout: 120000 })

const fakeFetch = (response, status = 200) => jest.fn(() =>
const fakeFetch = (response: any, status = 200) => vi.fn(() =>
Promise.resolve({
status: status,
json: () => Promise.resolve(response),
json: () => Promise.resolve(response)
})
);
)

test('Basic test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ ok: true })
TCGdex.fetch = fakeFetch({ ok: true }) as any
const res = await tcgdex.fetch('cards', 'basic-test')
expect(res).toEqual({ ok: true })
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
})

test('endpoint errors', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ ok: 'a' })
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
await expect(tcgdex.fetch()).rejects.toThrow()
})
Expand Down Expand Up @@ -70,7 +68,7 @@ test(`test get set from card`, async () => {
TCGdex.fetch = fetch

expect(
await (await tcgdex.card.get('swsh1-136')).getSet()
await (await tcgdex.card.get('swsh1-136'))!.getSet()
).toBeTruthy()
})

Expand All @@ -79,7 +77,7 @@ test(`test get serie from set`, async () => {
TCGdex.fetch = fetch

expect(
await (await tcgdex.set.get('swsh1')).getSerie()
await (await tcgdex.set.get('swsh1'))!.getSerie()
).toBeTruthy()
})

Expand Down
47 changes: 24 additions & 23 deletions __tests__/deprecated.test.js → __tests__/deprecated.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
const TCGdex = require("../src/tcgdex").default
const fetch = require('node-fetch')
import { expect, test, vi } from 'vitest'
import TCGdex from '../src/tcgdex'

const fakeFetch = (response, status = 200) => jest.fn(() =>
// change timeout of execution
vi.setConfig({ testTimeout: 120000 })

const fakeFetch = (response, status = 200) => vi.fn(() =>
Promise.resolve({
status: status,
json: () => Promise.resolve(response),
})
);


)

test('Basic test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ok: true})
TCGdex.fetch = fakeFetch({ ok: true }) as any
const res = await tcgdex.fetch('cards', 'basic-test')
expect(res).toEqual({ok: true})
expect(res).toEqual({ ok: true })
expect(TCGdex.fetch).toHaveBeenCalledTimes(1)
})

test('Cache test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ok: 'a'})
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
const res1 = await tcgdex.fetch('cards', 'cache-test')
expect(res1).toEqual({ok: 'a'})
TCGdex.fetch = fakeFetch({ok: 'b'})
expect(res1).toEqual({ ok: 'a' })
TCGdex.fetch = fakeFetch({ ok: 'b' }) as any
const res2 = await tcgdex.fetch('cards', 'cache-test')
expect(res2).toEqual({ok: 'a'})
expect(res2).toEqual({ ok: 'a' })
})

test('endpoint errors', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ok: 'a'})
TCGdex.fetch = fakeFetch({ ok: 'a' }) as any
await expect(tcgdex.fetch('non existing endpoint')).rejects.toThrow()
await expect(tcgdex.fetch()).rejects.toThrow()
})

test('404 test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch(undefined, 404)
TCGdex.fetch = fakeFetch(undefined, 404) as any
expect(
await tcgdex.fetch('cards', '404-test')
).not.toBeDefined()
Expand All @@ -47,15 +48,15 @@ test('test real endpoints', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch
const endpoints = [
{endpoint: 'fetchCard', params: ['swsh1-1']},
{endpoint: 'fetchCard', params: ['1', 'Sword & Shield']},
{endpoint: 'fetchCards', params: ['swsh1']},
{endpoint: 'fetchCards', params: []},
{endpoint: 'fetchSet', params: ['swsh1']},
{endpoint: 'fetchSets', params: ['swsh']},
{endpoint: 'fetchSets', params: []},
{endpoint: 'fetchSeries', params: []},
{endpoint: 'fetchSerie', params: ['swsh']},
{ endpoint: 'fetchCard', params: ['swsh1-1'] },
{ endpoint: 'fetchCard', params: ['1', 'Sword & Shield'] },
{ endpoint: 'fetchCards', params: ['swsh1'] },
{ endpoint: 'fetchCards', params: [] },
{ endpoint: 'fetchSet', params: ['swsh1'] },
{ endpoint: 'fetchSets', params: ['swsh'] },
{ endpoint: 'fetchSets', params: [] },
{ endpoint: 'fetchSeries', params: [] },
{ endpoint: 'fetchSerie', params: ['swsh'] },
]

for await (const item of endpoints) {
Expand Down
Loading