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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<img src="https://img.shields.io/github/stars/tcgdex/javascript-sdk?style=flat-square" alt="Github stars">
</a>
<a href="https://github.com/tcgdex/javascript-sdk/actions/workflows/build.yml">
<img src="https://img.shields.io/github/workflow/status/tcgdex/javascript-sdk/Build%20&%20Test?style=flat-square" alt="the TCGdex JAvascript SDK is released under the MIT license." />
<img src="https://img.shields.io/github/actions/workflow/status/tcgdex/javascript-sdk/build.yml?style=flat-square" alt="the TCGdex JAvascript SDK is released under the MIT license." />
</a>
<a href="https://discord.gg/NehYTAhsZE">
<img src="https://img.shields.io/discord/857231041261076491?color=%235865F2&label=Discord&style=flat-square" alt="Discord Link">
Expand Down
152 changes: 113 additions & 39 deletions __tests__/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const TCGdex = require("../src/tcgdex").default
const fetch = require('node-fetch')
/// <reference types="jest" />

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

const fakeFetch = (response, status = 200) => jest.fn(() =>
Promise.resolve({
Expand All @@ -8,59 +10,131 @@ const fakeFetch = (response, status = 200) => jest.fn(() =>
})
);



test('Basic test', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ok: true})
TCGdex.fetch = fakeFetch({ ok: true })
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'})
const res1 = await tcgdex.fetch('cards', 'cache-test')
expect(res1).toEqual({ok: 'a'})
TCGdex.fetch = fakeFetch({ok: 'b'})
const res2 = await tcgdex.fetch('cards', 'cache-test')
expect(res2).toEqual({ok: 'a'})
})

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

test('404 test', async () => {
test(`404 error`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch(undefined, 404)
TCGdex.fetch = fetch

expect(
await tcgdex.fetch('cards', '404-test')
).not.toBeDefined()
await tcgdex.card.get('404-error')
).toBeNull()
})

test('test real endpoints', async () => {
test(`test getting full set from list`, 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']},
]

for await (const item of endpoints) {
expect(
await tcgdex[item.endpoint](...item.params)
).toBeDefined()
}

expect(
await (await tcgdex.set.list())[0].getSet()
).toBeTruthy()
})

test(`test getting full serie from list`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

expect(
await (await tcgdex.serie.list())[0].getSerie()
).toBeTruthy()
})

test(`test getting full card from list`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

expect(
await (await tcgdex.card.list())[0].getCard()
).toBeTruthy()
})


test(`test get set from card`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

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

test(`test get serie from set`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

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

test(`advanced query system`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

expect(
(await tcgdex.card.list(
Query.create()
.equal('name', 'Pikachu')
.greaterOrEqualThan('hp', 60)
.lesserThan('hp', 70)
.contains('localId', '5')
.not.contains('localId', 'tg')
.not.equal('id', 'cel25-5')
.sort('localId', 'ASC')
.paginate(3, 2)
)).length
).toBe(2)
})

const endpoints = [
{ endpoint: 'card', params: ['swsh1-136'] },
{ endpoint: 'set', params: ['swsh1'] },
{ endpoint: 'serie', params: ['swsh'] },
{ endpoint: 'type', params: ['fire'] },
{ endpoint: 'retreat', params: ['1'] },
{ endpoint: 'rarity', params: ['common'] },
{ endpoint: 'illustrator', params: [''] },
{ endpoint: 'hp', params: ['30'] },
{ endpoint: 'categorie', params: ['pokemon'] },
{ endpoint: 'dexID', params: ['1'] },
{ endpoint: 'energyType', params: ['normal'] },
{ endpoint: 'regulationMark', params: ['f'] },
{ endpoint: 'stage', params: ['basic'] },
{ endpoint: 'suffixe', params: ['ex'] },
{ endpoint: 'trainerType', params: ['item'] },
{ endpoint: 'variant', params: ['normal'] },
]

for (const endpoint of endpoints) {
test(`test real ${endpoint.endpoint} endpoint list`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

expect(
await (tcgdex[endpoint.endpoint]).list()
).toBeTruthy()
})

test(`test real ${endpoint.endpoint} endpoint item`, async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fetch

expect(
await (tcgdex[endpoint.endpoint]).get(endpoint.params[0])
).toBeTruthy()
})

}
54 changes: 54 additions & 0 deletions __tests__/cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// <reference types="jest" />

const { default: MemoryCache } = require("../src/Psr/SimpleCache/MemoryCache")

const TCGdex = require("../src/tcgdex").default

test('that cache store and get one element', async () => {
const cache = new MemoryCache()
cache.set('a', 'b')
expect(cache.get('a')).toBe('b')
})

test('that cache store and get multiple elements', async () => {
const cache = new MemoryCache()
cache.setMultiple({
'a': 'b',
'c': 'd'
})
expect(cache.getMultiple(['a', 'c'])).toStrictEqual({
a: 'b',
c: 'd'
})
})

test('cache expiration', async () => {
const cache = new MemoryCache()
cache.set('a', 'b', 1)
// wait 2 secs
await new Promise((res) => setTimeout(res, 2000))
expect(cache.get('a')).toBeUndefined()
})

test('cache deletion', async () => {
const cache = new MemoryCache()
cache.set('a', 'b')
expect(cache.get('a')).toBe('b')
cache.delete('a')
expect(cache.get('a')).toBeUndefined()
})

test('cache cleared', async () => {
const cache = new MemoryCache()
cache.set('a', 'b')
expect(cache.get('a')).toBe('b')
cache.clear()
expect(cache.get('a')).toBeUndefined()
})

test('cache exists', async () => {
const cache = new MemoryCache()
expect(cache.has('a')).toBe(false)
cache.set('a', 'b')
expect(cache.has('a')).toBe(true)
})
66 changes: 66 additions & 0 deletions __tests__/deprecated.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const TCGdex = require("../src/tcgdex").default
const fetch = require('node-fetch')

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



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

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

test('endpoint errors', async () => {
const tcgdex = new TCGdex('en')
TCGdex.fetch = fakeFetch({ok: 'a'})
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)
expect(
await tcgdex.fetch('cards', '404-test')
).not.toBeDefined()
})

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']},
]

for await (const item of endpoints) {
expect(
await tcgdex[item.endpoint](...item.params)
).toBeDefined()
}
})
Loading
Loading