Added Tests :D

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-06-22 19:46:00 +02:00
parent babd4e5f85
commit b951f6d2d1
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
4 changed files with 81 additions and 21 deletions

66
__tests__/basic.test.js Normal file
View File

@ -0,0 +1,66 @@
const TCGdex = require("../dist/cjs/tcgdex.js").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()
}
})

View File

@ -2,21 +2,23 @@
"name": "@tcgdex/sdk", "name": "@tcgdex/sdk",
"version": "2.3.0-beta.2", "version": "2.3.0-beta.2",
"main": "./dist/cjs/tcgdex.node.js", "main": "./dist/cjs/tcgdex.node.js",
"module": "./dist/modules/tcgdex.node.js",
"types": "./dist/types/tcgdex.d.ts", "types": "./dist/types/tcgdex.d.ts",
"repository": "https://github.com/tcgdex/javascript-sdk.git", "repository": "https://github.com/tcgdex/javascript-sdk.git",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@parcel/packager-ts": "2.0.0-beta.2",
"@types/node-fetch": "^2.5.10", "@types/node-fetch": "^2.5.10",
"awesome-typescript-loader": "^5.2.1", "jest": "^27.0.5",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0", "ts-node": "^10.0.0",
"typescript": "^4.1.3", "typescript": "^4.1.3",
"webpack": "^5.40.0", "webpack": "^5.40.0",
"webpack-nano": "^1.1.1" "webpack-nano": "^1.1.1"
}, },
"engines": {
"node": ">=12"
},
"dependencies": { "dependencies": {
"node-fetch": "^2.6.1", "isomorphic-unfetch": "^3.1.0",
"unfetch": "^4.2.0" "unfetch": "^4.2.0"
}, },
"scripts": { "scripts": {
@ -29,16 +31,5 @@
"files": [ "files": [
"dist" "dist"
], ],
"sideEffects": false, "sideEffects": false
"targets": {
"browser": {
"context": "browser",
"engines": {
"browsers": "since 2017-06"
},
"includeNodeModules": true,
"optimize": false,
"scopeHoist": false
}
}
} }

View File

@ -1,5 +1,5 @@
import TCGdex from './tcgdex' import TCGdex from './tcgdex'
import fetch from 'node-fetch' import fetch from 'isomorphic-unfetch'
TCGdex.fetch = fetch as any TCGdex.fetch = fetch as any

View File

@ -19,12 +19,15 @@ module.exports = {
module: { module: {
rules: [{ rules: [{
test: /\.ts$/, test: /\.ts$/,
loader: 'awesome-typescript-loader', loader: 'ts-loader',
exclude: /node_modules/, exclude: /node_modules/,
options: { options: {
sourceMap: false, compilerOptions: {
declarationDir: false, sourceMap: false,
target: 'ES2016' declaration: false,
declarationDir: null,
target: 'ES2016'
}
} }
}] }]
} }