Skip to content

Commit 97dd2c8

Browse files
fedykUzlopak
andauthored
Remove redundant check (#895)
* Remove redundant check The `if` clauses already checked if `hiddenTag` is included. Signed-off-by: Andrii Fedyk <[email protected]> * add tests * fix tests * fix test --------- Signed-off-by: Andrii Fedyk <[email protected]> Co-authored-by: Aras Abbasi <[email protected]>
1 parent fe63809 commit 97dd2c8

File tree

2 files changed

+78
-58
lines changed

2 files changed

+78
-58
lines changed

lib/util/should-route-hide.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function shouldRouteHide (schema, opts) {
1414
}
1515

1616
if (tags.includes(hiddenTag)) {
17-
return schema.tags.includes(hiddenTag)
17+
return true
1818
}
1919

2020
return false

test/util.test.js

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

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

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

23-
test('formatParamUrl', async (t) => {
24-
t.plan(cases.length)
25-
24+
describe('formatParamUrl', () => {
2625
for (const kase of cases) {
27-
t.assert.strictEqual(formatParamUrl(kase[0]), kase[1])
26+
test(`formatParamUrl ${kase}`, (t) => {
27+
t.assert.strictEqual(formatParamUrl(kase[0]), kase[1])
28+
})
2829
}
2930
})
3031

31-
test('hasParams function', async (t) => {
32-
await t.test('should return false for empty url', (t) => {
32+
describe('hasParams function', () => {
33+
test('should return false for empty url', (t) => {
3334
const url = ''
3435
const result = hasParams(url)
3536
t.assert.strictEqual(result, false)
3637
})
3738

38-
await t.test('should return true for url with parameters', (t) => {
39+
test('should return true for url with parameters', (t) => {
3940
const url = '/example/{userId}'
4041
const result = hasParams(url)
4142
t.assert.strictEqual(result, true)
4243
})
4344

44-
await t.test('should return true for url with multiple parameters', (t) => {
45+
test('should return true for url with multiple parameters', (t) => {
4546
const url = '/example/{userId}/{secretToken}'
4647
const result = hasParams(url)
4748
t.assert.strictEqual(result, true)
4849
})
4950

50-
await t.test('should return false for url without parameters', (t) => {
51+
test('should return false for url without parameters', (t) => {
5152
const url = '/example/path'
5253
const result = hasParams(url)
5354
t.assert.strictEqual(result, false)
5455
})
5556
})
5657

57-
test('matchParams function', async (t) => {
58-
await t.test('should return an empty array for empty url', (t) => {
58+
describe('matchParams function', (t) => {
59+
test('should return an empty array for empty url', (t) => {
5960
const url = ''
6061
const result = matchParams(url)
6162
t.assert.deepStrictEqual(result, [])
6263
})
6364

64-
await t.test('should return an array of matched parameters', (t) => {
65+
test('should return an array of matched parameters', (t) => {
6566
const url = '/example/{userId}/{secretToken}'
6667
const result = matchParams(url)
6768
t.assert.deepStrictEqual(result, ['{userId}', '{secretToken}'])
6869
})
6970

70-
await t.test('should return an empty array for url without parameters', (t) => {
71+
test('should return an empty array for url without parameters', (t) => {
7172
const url = '/example/path'
7273
const result = matchParams(url)
7374
t.assert.deepStrictEqual(result, [])
7475
})
7576
})
7677

77-
const urlsToShemas = [
78-
[
79-
'/example/{userId}', {
80-
params: {
81-
type: 'object',
82-
properties: {
83-
userId: {
84-
type: 'string'
78+
describe('generateParamsSchema function', (t) => {
79+
const urlsToShemas = [
80+
[
81+
'/example/{userId}', {
82+
params: {
83+
type: 'object',
84+
properties: {
85+
userId: {
86+
type: 'string'
87+
}
8588
}
8689
}
8790
}
88-
}
89-
],
90-
[
91-
'/example/{userId}/{secretToken}', {
92-
params: {
93-
type: 'object',
94-
properties: {
95-
userId: {
96-
type: 'string'
97-
},
98-
secretToken: {
99-
type: 'string'
91+
],
92+
[
93+
'/example/{userId}/{secretToken}', {
94+
params: {
95+
type: 'object',
96+
properties: {
97+
userId: {
98+
type: 'string'
99+
},
100+
secretToken: {
101+
type: 'string'
102+
}
100103
}
101104
}
102105
}
103-
}
104-
],
105-
[
106-
'/example/near/{lat}-{lng}', {
107-
params: {
108-
type: 'object',
109-
properties: {
110-
lat: {
111-
type: 'string'
112-
},
113-
lng: {
114-
type: 'string'
106+
],
107+
[
108+
'/example/near/{lat}-{lng}', {
109+
params: {
110+
type: 'object',
111+
properties: {
112+
lat: {
113+
type: 'string'
114+
},
115+
lng: {
116+
type: 'string'
117+
}
115118
}
116119
}
117120
}
118-
}
121+
]
119122
]
120-
]
121123

122-
test('generateParamsSchema function', (t) => {
123-
t.plan(urlsToShemas.length)
124-
for (const [url, expectedSchema] of urlsToShemas) {
125-
const result = generateParamsSchema(url)
124+
test('generateParamsSchema', (t) => {
125+
for (const [url, expectedSchema] of urlsToShemas) {
126+
const result = generateParamsSchema(url)
126127

127-
t.assert.deepStrictEqual(result, expectedSchema)
128-
}
128+
t.assert.deepStrictEqual(result, expectedSchema)
129+
}
130+
})
129131
})
130132

131-
test('paramName function', async (t) => {
132-
await t.test('should return the captured value from the param', (t) => {
133+
describe('paramName function', () => {
134+
test('should return the captured value from the param', (t) => {
133135
const param = '{userId}'
134136
const result = paramName(param)
135137
t.assert.strictEqual(result, 'userId')
136138
})
137139

138-
await t.test('should return the same value if there are no captures', (t) => {
140+
test('should return the same value if there are no captures', (t) => {
139141
const param = 'userId'
140142
const result = paramName(param)
141143
t.assert.strictEqual(result, 'userId')
142144
})
143145
})
146+
147+
describe('shouldRouteHide', () => {
148+
test('shouldRouteHide should return true for hidden route', (t) => {
149+
t.assert.ok(shouldRouteHide({ hide: true }, {}))
150+
})
151+
152+
test('shouldRouteHide should return true for hideUntagged', (t) => {
153+
t.assert.ok(shouldRouteHide({ tags: [] }, { hideUntagged: true }))
154+
})
155+
156+
test('shouldRouteHide should return true for hiddenTag', (t) => {
157+
t.assert.ok(shouldRouteHide({ tags: ['x-test'] }, { hiddenTag: 'x-test' }))
158+
})
159+
160+
test('shouldRouteHide should return false for non hidden route', (t) => {
161+
t.assert.equal(shouldRouteHide({}, {}), false)
162+
})
163+
})

0 commit comments

Comments
 (0)