Skip to content

Commit

Permalink
feat(api): initial API stub
Browse files Browse the repository at this point in the history
  • Loading branch information
reegodev committed Apr 12, 2021
1 parent ec6369f commit 18adefa
Show file tree
Hide file tree
Showing 16 changed files with 844 additions and 136 deletions.
149 changes: 23 additions & 126 deletions __tests__/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import { ResourceError } from '../src/errors'

const _originalCreateRequest = createRequest

const dummyConfig = {
type: 'skus',
attributes: [] as any,
relationships: {},
}

const mockCreateRequest = () => {
;(createRequest as any) = jest.fn(
(url: string, method: string, query: any, data: any) => {
Expand Down Expand Up @@ -226,11 +232,7 @@ describe('api', () => {
include: ['123', '456'],
}

await find('12345', query, {
type: 'skus',
attributes: [],
relationships: {},
})
await find('12345', query, dummyConfig)

expect(createRequest).toHaveBeenLastCalledWith(
'/api/skus/12345',
Expand All @@ -241,15 +243,7 @@ describe('api', () => {

it('deserializes data', async () => {
mockRequestWithResponse(singleSku)
const res = await find(
'12345',
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await find('12345', {}, dummyConfig)

const attributes = Object.keys(singleSku.data.attributes)
const relationships = ['prices']
Expand All @@ -272,15 +266,7 @@ describe('api', () => {

const fn = async () => {
try {
await find(
'12345',
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
await find('12345', {}, dummyConfig)
return null
} catch (error) {
return error
Expand All @@ -303,26 +289,15 @@ describe('api', () => {
include: ['123', '456'],
}

await findAll(query, {
type: 'skus',
attributes: [],
relationships: {},
})
await findAll(query, dummyConfig)

expect(createRequest).toHaveBeenLastCalledWith('/api/skus', 'get', query)
})

it('returns all results from the response', async () => {
mockRequestWithResponse(multipleSkus)

const res = await findAll(
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await findAll({}, dummyConfig)

expect(Object.keys(res).sort()).toEqual(
[
Expand All @@ -349,14 +324,7 @@ describe('api', () => {

const fn = async () => {
try {
await findAll(
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
await findAll({}, dummyConfig)
return null
} catch (error) {
return error
Expand All @@ -379,26 +347,15 @@ describe('api', () => {
include: ['123', '456'],
}

await findBy(query, {
type: 'skus',
attributes: [],
relationships: {},
})
await findBy(query, dummyConfig)

expect(createRequest).toHaveBeenLastCalledWith('/api/skus', 'get', query)
})

it('returns the first result from the response', async () => {
mockRequestWithResponse(multipleSkus)

const res = await findBy(
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await findBy({}, dummyConfig)

const attributes = Object.keys(multipleSkus.data[0].attributes)
const relationships = ['prices']
Expand All @@ -421,14 +378,7 @@ describe('api', () => {
data: [],
})

const res = await findBy(
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await findBy({}, dummyConfig)

expect(res).toBeNull()
})
Expand All @@ -438,14 +388,7 @@ describe('api', () => {

const fn = async () => {
try {
await findBy(
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
await findBy({}, dummyConfig)
return null
} catch (error) {
return error
Expand All @@ -465,23 +408,15 @@ describe('api', () => {
it('passes parameters to request', async () => {
mockCreateRequest()

await destroy('1234', {
type: 'skus',
attributes: [],
relationships: {},
})
await destroy('1234', dummyConfig)

expect(createRequest).toHaveBeenLastCalledWith('/api/skus/1234', 'delete')
})

it('resolves with an empty result', async () => {
mockRequestWithResponse({}, 204)

const res = await destroy('1234', {
type: 'skus',
attributes: [],
relationships: {},
})
const res = await destroy('1234', dummyConfig)

expect(res).toBeUndefined()
})
Expand All @@ -491,11 +426,7 @@ describe('api', () => {

const fn = async () => {
try {
await destroy('12345', {
type: 'skus',
attributes: [],
relationships: {},
})
await destroy('12345', dummyConfig)
return null
} catch (error) {
return error
Expand Down Expand Up @@ -656,15 +587,7 @@ describe('api', () => {

it('deserializes data', async () => {
mockRequestWithResponse(singleSku)
const res = await create(
{},
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await create({}, {}, dummyConfig)

const attributes = Object.keys(singleSku.data.attributes)
const relationships = ['prices']
Expand All @@ -687,15 +610,7 @@ describe('api', () => {

const fn = async () => {
try {
await create(
{},
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
await create({}, {}, dummyConfig)
return null
} catch (error) {
return error
Expand Down Expand Up @@ -862,16 +777,7 @@ describe('api', () => {

it('deserializes data', async () => {
mockRequestWithResponse(singleSku)
const res = await update(
'12345',
{},
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
const res = await update('12345', {}, {}, dummyConfig)

const attributes = Object.keys(singleSku.data.attributes)
const relationships = ['prices']
Expand All @@ -894,16 +800,7 @@ describe('api', () => {

const fn = async () => {
try {
await update(
'12345',
{},
{},
{
type: 'skus',
attributes: [],
relationships: {},
},
)
await update('12345', {}, {}, dummyConfig)
return null
} catch (error) {
return error
Expand Down
44 changes: 44 additions & 0 deletions __tests__/auth/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { __resetMarket, setMarket } from '../../src/auth/market'
import {
cacheKey,
tokenCache,
cacheToken,
getToken,
} from '../../src/auth/cache'

describe('auth:cache', () => {
beforeEach(() => {
__resetMarket()
})

it('uses market as cache key', () => {
setMarket(123)
expect(cacheKey()).toBe('123')

setMarket([456, 123])
expect(cacheKey()).toBe('456,123')
})

it('caches a token based on current market', () => {
setMarket(987)
cacheToken('test', 0)
expect(tokenCache.has(cacheKey())).toBe(true)
expect(tokenCache.get(cacheKey())).toEqual({
token: 'test',
expires: 0,
})
})

it('returns an empty string with an expired token', () => {
setMarket(987)
cacheToken('test', 0)
expect(getToken()).toEqual({ token: '', expires: 0 })
})

it('returns current token', () => {
setMarket(123)
const expires = Date.now() + 1000
cacheToken('test', expires)
expect(getToken()).toEqual({ token: 'test', expires })
})
})
Loading

0 comments on commit 18adefa

Please sign in to comment.