Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/util/should-route-hide.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function shouldRouteHide (schema, opts) {
}

if (tags.includes(hiddenTag)) {
return schema.tags.includes(hiddenTag)
return true
}

return false
Expand Down
134 changes: 77 additions & 57 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { test } = require('node:test')
const { test, describe } = require('node:test')
const { formatParamUrl } = require('../lib/util/format-param-url')
const { hasParams, matchParams } = require('../lib/util/match-params')
const { generateParamsSchema, paramName } = require('../lib/util/generate-params-schema')
const { shouldRouteHide } = require('../lib/util/should-route-hide')

const cases = [
['/example/:userId', '/example/{userId}'],
Expand All @@ -20,124 +21,143 @@ const cases = [
['/api/v1/postalcode-jp/(^[0-9]{7}$)', '/api/v1/postalcode-jp/{regexp1}']
]

test('formatParamUrl', async (t) => {
t.plan(cases.length)

describe('formatParamUrl', () => {
for (const kase of cases) {
t.assert.strictEqual(formatParamUrl(kase[0]), kase[1])
test(`formatParamUrl ${kase}`, (t) => {
t.assert.strictEqual(formatParamUrl(kase[0]), kase[1])
})
}
})

test('hasParams function', async (t) => {
await t.test('should return false for empty url', (t) => {
describe('hasParams function', () => {
test('should return false for empty url', (t) => {
const url = ''
const result = hasParams(url)
t.assert.strictEqual(result, false)
})

await t.test('should return true for url with parameters', (t) => {
test('should return true for url with parameters', (t) => {
const url = '/example/{userId}'
const result = hasParams(url)
t.assert.strictEqual(result, true)
})

await t.test('should return true for url with multiple parameters', (t) => {
test('should return true for url with multiple parameters', (t) => {
const url = '/example/{userId}/{secretToken}'
const result = hasParams(url)
t.assert.strictEqual(result, true)
})

await t.test('should return false for url without parameters', (t) => {
test('should return false for url without parameters', (t) => {
const url = '/example/path'
const result = hasParams(url)
t.assert.strictEqual(result, false)
})
})

test('matchParams function', async (t) => {
await t.test('should return an empty array for empty url', (t) => {
describe('matchParams function', (t) => {
test('should return an empty array for empty url', (t) => {
const url = ''
const result = matchParams(url)
t.assert.deepStrictEqual(result, [])
})

await t.test('should return an array of matched parameters', (t) => {
test('should return an array of matched parameters', (t) => {
const url = '/example/{userId}/{secretToken}'
const result = matchParams(url)
t.assert.deepStrictEqual(result, ['{userId}', '{secretToken}'])
})

await t.test('should return an empty array for url without parameters', (t) => {
test('should return an empty array for url without parameters', (t) => {
const url = '/example/path'
const result = matchParams(url)
t.assert.deepStrictEqual(result, [])
})
})

const urlsToShemas = [
[
'/example/{userId}', {
params: {
type: 'object',
properties: {
userId: {
type: 'string'
describe('generateParamsSchema function', (t) => {
const urlsToShemas = [
[
'/example/{userId}', {
params: {
type: 'object',
properties: {
userId: {
type: 'string'
}
}
}
}
}
],
[
'/example/{userId}/{secretToken}', {
params: {
type: 'object',
properties: {
userId: {
type: 'string'
},
secretToken: {
type: 'string'
],
[
'/example/{userId}/{secretToken}', {
params: {
type: 'object',
properties: {
userId: {
type: 'string'
},
secretToken: {
type: 'string'
}
}
}
}
}
],
[
'/example/near/{lat}-{lng}', {
params: {
type: 'object',
properties: {
lat: {
type: 'string'
},
lng: {
type: 'string'
],
[
'/example/near/{lat}-{lng}', {
params: {
type: 'object',
properties: {
lat: {
type: 'string'
},
lng: {
type: 'string'
}
}
}
}
}
]
]
]

test('generateParamsSchema function', (t) => {
t.plan(urlsToShemas.length)
for (const [url, expectedSchema] of urlsToShemas) {
const result = generateParamsSchema(url)
test('generateParamsSchema', (t) => {
for (const [url, expectedSchema] of urlsToShemas) {
const result = generateParamsSchema(url)

t.assert.deepStrictEqual(result, expectedSchema)
}
t.assert.deepStrictEqual(result, expectedSchema)
}
})
})

test('paramName function', async (t) => {
await t.test('should return the captured value from the param', (t) => {
describe('paramName function', () => {
test('should return the captured value from the param', (t) => {
const param = '{userId}'
const result = paramName(param)
t.assert.strictEqual(result, 'userId')
})

await t.test('should return the same value if there are no captures', (t) => {
test('should return the same value if there are no captures', (t) => {
const param = 'userId'
const result = paramName(param)
t.assert.strictEqual(result, 'userId')
})
})

describe('shouldRouteHide', () => {
test('shouldRouteHide should return true for hidden route', (t) => {
t.assert.ok(shouldRouteHide({ hide: true }, {}))
})

test('shouldRouteHide should return true for hideUntagged', (t) => {
t.assert.ok(shouldRouteHide({ tags: [] }, { hideUntagged: true }))
})

test('shouldRouteHide should return true for hiddenTag', (t) => {
t.assert.ok(shouldRouteHide({ tags: ['x-test'] }, { hiddenTag: 'x-test' }))
})

test('shouldRouteHide should return false for non hidden route', (t) => {
t.assert.equal(shouldRouteHide({}, {}), false)
})
})
Loading