Skip to content

Commit

Permalink
feat(messages): get messages with query params for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsob committed Feb 21, 2019
1 parent 4bac2e8 commit 2a84275
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/v0/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export interface MessagesOptions {
base?: string
}

export interface MessagesQueryOptions {
read?: boolean
ignored?: boolean
}

export interface MessagesResponse {
data: Message[]
metadata: object
Expand Down Expand Up @@ -46,10 +51,11 @@ export class Messages {
this.options.base = this.options.base || 'https://api.tillhub.com'
}

getAll(): Promise<MessagesResponse> {
getAll(query?: MessagesQueryOptions | undefined): Promise<MessagesResponse> {
return new Promise(async (resolve, reject) => {
try {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}`
const queryString = qs.stringify(query, { addQueryPrefix: true })
const uri = `${this.options.base}${this.endpoint}/${this.options.user}${queryString}`

const response = await this.http.getClient().get(uri)
response.status !== 200 && reject(new errors.MessagesFetchFailed())
Expand Down
45 changes: 45 additions & 0 deletions test/messages/get-all.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as dotenv from 'dotenv'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import qs from 'qs'
dotenv.config()
import { v0 } from '../../src/tillhub-js'
import { initThInstance } from '../util'
Expand All @@ -12,6 +13,11 @@ afterEach(() => {
mock.reset()
})

const queryParams = {
read: false,
ignored: false
}

describe('v0: Messages: can get all messages', () => {
it("Tillhub's messages are instantiable", async () => {
if (process.env.SYSTEM_TEST !== 'true') {
Expand Down Expand Up @@ -50,6 +56,45 @@ describe('v0: Messages: can get all messages', () => {
expect(Array.isArray(data)).toBe(true)
})

it('Can get all messges with filters', async () => {
if (process.env.SYSTEM_TEST !== 'true') {
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) {
return [
200,
{
token: '',
user: {
id: '123',
legacy_id: legacyId
}
}
]
})

mock
.onGet(`https://api.tillhub.com/api/v0/messages/${legacyId}?${qs.stringify(queryParams)}`)
.reply(function (config) {
return [
200,
{
count: 1,
results: [{}]
}
]
})
}

const th = await initThInstance()

const Messages = th.messages()

expect(Messages).toBeInstanceOf(v0.Messages)

const { data } = await Messages.getAll(queryParams)

expect(Array.isArray(data)).toBe(true)
})

it('rejects on status codes that are not 200', async () => {
if (process.env.SYSTEM_TEST !== 'true') {
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) {
Expand Down

0 comments on commit 2a84275

Please sign in to comment.