Skip to content

Commit

Permalink
feat(stock): add query params to stock-locations
Browse files Browse the repository at this point in the history
  • Loading branch information
qtotuan committed Apr 15, 2019
1 parent 322e22b commit c76ce16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/v0/stocks.ts
Expand Up @@ -10,6 +10,7 @@ export interface StocksOptions {
export interface StocksQuery {
limit?: number
uri?: string
deleted?: boolean
}

export interface StocksBookQuery {
Expand Down Expand Up @@ -57,13 +58,15 @@ export class Stocks {
endpoint: string
http: Client
public options: StocksOptions
public uriHelper: UriHelper

constructor(options: StocksOptions, http: Client) {
this.options = options
this.http = http

this.endpoint = '/api/v0/stock'
this.options.base = this.options.base || 'https://api.tillhub.com'
this.uriHelper = new UriHelper(this.endpoint, this.options)
}

getAll(query?: StocksQuery | undefined): Promise<StocksResponse> {
Expand Down Expand Up @@ -129,12 +132,8 @@ export class Stocks {
getLocations(query?: StocksQuery | undefined): Promise<StocksResponse> {
return new Promise(async (resolve, reject) => {
try {
let uri
if (query && query.uri) {
uri = query.uri
} else {
uri = `${this.options.base}${this.endpoint}/${this.options.user}/locations`
}
const base = this.uriHelper.generateBaseUri(`/locations`)
const uri = this.uriHelper.generateUriWithQuery(base, query)

const response = await this.http.getClient().get(uri)
response.status !== 200 && reject(new errors.StocksLocationsFetchFailed())
Expand Down
15 changes: 11 additions & 4 deletions test/stocks/get-locations.test.ts
@@ -1,4 +1,5 @@
import * as dotenv from 'dotenv'
import qs from 'qs'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
dotenv.config()
Expand All @@ -25,6 +26,12 @@ afterEach(() => {
mock.reset()
})

const query = {
deleted: false
}

const queryString = qs.stringify(query)

describe('v0: Stocks: can get all locations', () => {
it("Tillhub's Stocks are instantiable", async () => {
if (process.env.SYSTEM_TEST !== 'true') {
Expand All @@ -42,7 +49,7 @@ describe('v0: Stocks: can get all locations', () => {
})

mock
.onGet(`https://api.tillhub.com/api/v0/stock/${legacyId}/locations`)
.onGet(`https://api.tillhub.com/api/v0/stock/${legacyId}/locations?${queryString}`)
.reply(function (config) {
return [
200,
Expand Down Expand Up @@ -74,7 +81,7 @@ describe('v0: Stocks: can get all locations', () => {

expect(stocks).toBeInstanceOf(v0.Stocks)

const { data } = await stocks.getLocations()
const { data } = await stocks.getLocations(query)

expect(Array.isArray(data)).toBe(true)
})
Expand All @@ -94,7 +101,7 @@ describe('v0: Stocks: can get all locations', () => {
]
})
mock
.onGet(`https://api.tillhub.com/api/v0/stock/${legacyId}locations`)
.onGet(`https://api.tillhub.com/api/v0/stock/${legacyId}locations?${queryString}`)
.reply(function (config) {
return [205]
})
Expand All @@ -117,7 +124,7 @@ describe('v0: Stocks: can get all locations', () => {
})

try {
await th.stocks().getLocations()
await th.stocks().getLocations(query)
} catch (err) {
expect(err.name).toBe('StocksLocationsFetchFailed')
}
Expand Down

0 comments on commit c76ce16

Please sign in to comment.