Skip to content

Commit

Permalink
feat(api): implement all features
Browse files Browse the repository at this point in the history
  • Loading branch information
reegodev committed Apr 10, 2021
1 parent 83382ba commit c07d651
Show file tree
Hide file tree
Showing 13 changed files with 1,522 additions and 34 deletions.
400 changes: 381 additions & 19 deletions __tests__/api.spec.ts

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions __tests__/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { handleApiErrors, ResourceError } from '../src/errors'

describe('errors', () => {
describe('handleApiErrors', () => {
it('returns a ResourceError if error has a response property', () => {
const res = handleApiErrors({
response: {},
})

expect(res).toBeInstanceOf(ResourceError)
})

it('throws the original error if it has no response property', () => {
expect(() => handleApiErrors(new Error('test'))).toThrow('test')
})
})

describe('ResourceError', () => {
it('extends Error class', () => {
const err = new ResourceError({} as any)
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('CommerceLayer API error')
})

it('creates an error with a request property', () => {
let err = new ResourceError({} as any)
expect(err.request).toBeUndefined()

err = new ResourceError({ request: {} } as any)
expect(err.request).toEqual({})
})

it('creates an error with a response property', () => {
let err = new ResourceError({} as any)
expect(err.response).toBeUndefined()

err = new ResourceError({ response: {} } as any)
expect(err.response).toEqual({})
})

it('creates an error with a config property', () => {
let err = new ResourceError({} as any)
expect(err.config).toBeUndefined()

err = new ResourceError({ config: {} } as any)
expect(err.config).toEqual({})
})

it('creates an error with a messages property', () => {
let err = new ResourceError({} as any)
expect(err.messages).toEqual([])

err = new ResourceError({ response: {} } as any)
expect(err.messages).toEqual([])

err = new ResourceError({ response: { data: {} } } as any)
expect(err.messages).toEqual([])

err = new ResourceError({ response: { data: { errors: null } } } as any)
expect(err.messages).toEqual([])

err = new ResourceError({ response: { data: { errors: ['1'] } } } as any)
expect(err.messages).toEqual(['1'])
})

it('creates an error with a firstMessage property', () => {
let err = new ResourceError({} as any)
expect(err.firstMessage).toBeUndefined()

err = new ResourceError({ response: {} } as any)
expect(err.firstMessage).toBeUndefined()

err = new ResourceError({ response: { data: { errors: 'asd' } } } as any)
expect(err.firstMessage).toBeUndefined()

err = new ResourceError({ response: { data: { errors: [] } } } as any)
expect(err.firstMessage).toBeUndefined()

err = new ResourceError({ response: { data: { errors: ['1'] } } } as any)
expect(err.firstMessage).toEqual('1')
})

it('creates an error with a status property', () => {
let err = new ResourceError({} as any)
expect(err.status).toBeUndefined()

err = new ResourceError({ response: {} } as any)
expect(err.status).toBeUndefined()

err = new ResourceError({ response: { status: 200 } } as any)
expect(err.status).toEqual(200)
})
})
})
Loading

0 comments on commit c07d651

Please sign in to comment.