Add test for StoryRoutes

This commit is contained in:
Jerko Steiner 2019-01-27 16:19:35 +01:00
parent 52dc876845
commit 4cc03fe611
2 changed files with 36 additions and 16 deletions

View File

@ -18,22 +18,24 @@ export class URLFormatter {
params?: IRequestParams, params?: IRequestParams,
query?: IRequestQuery, query?: IRequestQuery,
) { ) {
if (!params) { let formattedUrl = url
return url if (params) {
formattedUrl = url.replace(this.params.regex, match => {
const key = match.substring(1)
assert(params.hasOwnProperty(key))
return String(params![key])
})
} }
const formattedUrl = url.replace(this.params.regex, match => { let qs = ''
const key = match.substring(1)
assert(params.hasOwnProperty(key))
return String(params![key])
})
if (query) { if (query) {
Object.keys(query).reduce((queryString, key) => { qs = Object.keys(query).reduce((queryString, key) => {
return queryString + return queryString +
encodeURIComponent(key) + '=' + encodeURIComponent(key) + '=' +
encodeURIComponent(String(query[key])) + '&' encodeURIComponent(String(query[key])) + '&'
}, '?') }, '?')
.replace(/&$/, '')
} }
return this.params.baseURL + formattedUrl return this.params.baseURL + formattedUrl + qs
} }
} }

View File

@ -1,4 +1,4 @@
import {ISite/*, IStory*/} from '@rondo/common' import {ISite} from '@rondo/common'
import {createSite} from '../site/SiteTestUtils' import {createSite} from '../site/SiteTestUtils'
import {test} from '../test' import {test} from '../test'
@ -9,18 +9,27 @@ describe('story', () => {
let cookie!: string let cookie!: string
let token!: string let token!: string
let team!: ISite let site!: ISite
beforeEach(async () => { beforeEach(async () => {
const session = await test.registerAccount() const session = await test.registerAccount()
cookie = session.cookie cookie = session.cookie
token = session.token token = session.token
t.setHeaders({ cookie, 'x-csrf-token': token }) t.setHeaders({ cookie, 'x-csrf-token': token })
team = await createSite(t, 'test.example.com') site = await createSite(t, 'test.example.com')
}) })
const invalidUrl = 'https://invalid.example.com/test' const invalidUrl = 'https://invalid.example.com/test'
// const validUrl = 'https://test.example.com/test' const validUrl = 'https://test.example.com/test'
async function getStory() {
const response = await t
.get('/stories/by-url', {
query: { url: validUrl },
})
.expect(200)
return response.body!
}
describe('/stories/by-url', () => { describe('/stories/by-url', () => {
it('returns undefined when a site is not configured', async () => { it('returns undefined when a site is not configured', async () => {
@ -33,15 +42,24 @@ describe('story', () => {
}) })
it('creates a story when it does not exist', async () => { it('creates a story when it does not exist', async () => {
const story = await getStory()
expect(story.id).toBeTruthy()
expect(story.siteId).toEqual(site.id)
}) })
it('retrieves existing story after it is created', async () => { it('retrieves existing story after it is created', async () => {
const story1 = await getStory()
const story2 = await getStory()
expect(story1.id).toBeTruthy()
expect(story1).toEqual(story2)
}) })
it('prevents unique exceptions', async () => { it('prevents unique exceptions', async () => {
const p1 = getStory()
const p2 = getStory()
const [story1, story2] = await Promise.all([p1, p2])
expect(story1.id).toBeTruthy()
expect(story1).toEqual(story2)
}) })
}) })