Skip to content

Commit

Permalink
refactor: remove axios dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
nolddor committed Mar 26, 2024
1 parent c9c41f2 commit 814e0fa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 117 deletions.
13 changes: 10 additions & 3 deletions lib/SteamCardExchange.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const HTMLUtils = require('./HTMLUtils')
const axios = require('axios')

const throwIfHttpError = response => {
if (!response.ok) {
throw new Error(`HTTP-${response.status} ${response.statusText}`)
}
return response
}

class SteamCardExchange {
static async getBadges() {
const response = await axios.get('https://www.steamcardexchange.net/api/request.php?GetInventory')
const data = response.data.data
const endpoint = 'https://www.steamcardexchange.net/api/request.php?GetInventory'
const response = await fetch(endpoint).then(throwIfHttpError)
const data = await response.json().then(json => json.data)

if (!data) {
throw new Error('Malformed response')
Expand Down
56 changes: 34 additions & 22 deletions lib/__tests__/SteamCardExchange.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
const SteamCardExchange = require('../SteamCardExchange')
const axios = require('axios')

jest.mock('axios')

beforeEach(() => {
jest.resetAllMocks()
})
const fetchSpy = jest.spyOn(global, 'fetch')

const badgeA = {
112233: {
Expand All @@ -26,35 +20,53 @@ const badgeC = {
}
}

const mockFechSuccess = (badges) => {
let json = {}
if (badges) {
json = { data: Object.entries(badges).map(([appid, badge]) => [[appid, badge.name], null, null, [badge.size]]) }
}

return Promise.resolve({
json: () => Promise.resolve(json), // response content that was provided by the server as json format
ok: true, // whether the response was successful (status in the range 200-299) or not
status: 200, // HTTP status code from the server response
statusText: 'OK' // HTTP status message from the server response
})
}

const mockFechFailure = () => Promise.resolve({
json: () => Promise.resolve(), // response content that was provided by the server as json format
ok: false, // whether the response was successful (status in the range 200-299) or not
status: 404, // HTTP status code from the server response
statusText: 'Not Found' // HTTP status message from the server response
})

describe('SteamCardExchange#getBadges()', () => {
test('Throws "HTTP" error', () => {
fetchSpy.mockImplementationOnce(() => mockFechFailure())
expect(SteamCardExchange.getBadges()).rejects.toThrowError('HTTP-404 Not Found')
})

test('Throws "Malformed Response" error', () => {
const badges = null
axios.get.mockImplementation(() => mockAxiosGet(badges))
fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges))
expect(SteamCardExchange.getBadges()).rejects.toThrowError('Malformed response')
})

test('Get badges successfully', () => {
const badges = { ...badgeA, ...badgeB }
axios.get.mockImplementation(() => mockAxiosGet(badges))
fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges))
expect(SteamCardExchange.getBadges()).resolves.toStrictEqual(badges)
})

test('Skip 0-size badges', () => {
const badges = { ...badgeA, ...badgeC }
axios.get.mockImplementation(() => mockAxiosGet(badges))
fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges))
expect(SteamCardExchange.getBadges()).resolves.toStrictEqual(badgeA)
})
})

const mockAxiosGet = (badges) => {
let body = {}
if (badges) {
body = { data: Object.entries(badges).map(([appid, badge]) => [[appid, badge.name], null, null, [badge.size]]) }
}

return {
data: body, // `data` is the response content that was provided by the server
status: 200, // `status` is the HTTP status code from the server response
statusText: 'OK' // `statusText` is the HTTP status message from the server response
}
}
// Runs after all the tests in this file have completed.
afterAll(() => {
fetchSpy.mockRestore()
})
91 changes: 0 additions & 91 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"npm": ">=9.0.0"
},
"dependencies": {
"axios": "1.6.8",
"html-entities": "2.5.2",
"winston": "3.12.0"
},
Expand Down

0 comments on commit 814e0fa

Please sign in to comment.